Consensys / starknet-snap

The MetaMask Snap for Starknet
https://snaps.consensys.net/starknet
Apache License 2.0
77 stars 27 forks source link

fix: improve error message in rpc input validation #335

Closed khanti42 closed 2 months ago

khanti42 commented 2 months ago

PR Description: Improved Error Messages for Union Struct Validation

Before:

When testing a union struct with multiple levels, such as:

const multiLevelStruct = union([
  object({
    field1: string(),
  }),
  object({
    field3: boolean(),
  }),
]);

If the input was incorrect, for example:

{
  "field1": true
}

The error message provided was not clear and didn't offer much guidance:

"Expected the value to satisfy a union of `object | object`, but received: [object Object]"

This generic message made it difficult to diagnose the specific issue within the nested structure, particularly when working with complex schemas.

Now:

The error handling has been improved to provide much clearer and more informative messages. With the same incorrect input:

{
  "field1": true
}

The new error message provides detailed information about where the validation failed:

At path: field1 --
    Expected a string, but received: true
    Expected a value of type `never`, but received: `true`

At path: field3 -- 
    Expected a value of type `boolean`, but received: `undefined`

Benefits:

This PR improves the developer experience and reduce time in debugging.

sonarcloud[bot] commented 2 months ago

Quality Gate Passed Quality Gate passed for 'consensys_starknet-snap-wallet-ui'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

sonarcloud[bot] commented 2 months ago

Quality Gate Passed Quality Gate passed for 'consensys_starknet-snap-starknet-snap'

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

khanti42 commented 2 months ago

This is closed, because complex superstruct are implemented as : https://github.com/MetaMask/keyring-api/blob/c7068d1d585c116cd8cd2aa2e838429215800de5/src/api.ts#L63-L76 instead of unions.