Skip to content

Commit c7a6906

Browse files
fix: Improve field validation errors (#2937)
Improves validation errors for `Field` by rewriting the struct using `selectiveUnion`. Fixes #2927 --------- Co-authored-by: Maarten Zuidhoorn <[email protected]>
1 parent 90b2e39 commit c7a6906

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "lyy+HECzOFQzuzjyD1nas6GPc4kMt340ChbqQZl7uEo=",
10+
"shasum": "DYLBW3jdovQYu6jwACo/3xlujGXb0YxJ19UEpUKX1qE=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "k7NYoT8jI2E4u785PN5PoruwtjF18KNOSagNY9fS44U=",
10+
"shasum": "dFTqb94KtIZ44IaoMYhGunl9c9Of9j67TNYobl+CwYg=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-rpc-methods/src/permitted/createInterface.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ describe('snap_createInterface', () => {
191191
error: {
192192
code: -32602,
193193
message:
194-
'Invalid params: At path: ui.props.children.props.children -- Expected the value to satisfy a union of `tuple | tuple | tuple | object | object | object | object | object | object`, but received: [object Object].',
194+
'Invalid params: At path: ui.props.children.props.children -- Expected type to be one of: "Input", "Dropdown", "RadioGroup", "FileInput", "Checkbox", "Selector", but received: "Copyable".',
195195
stack: expect.any(String),
196196
},
197197
id: 1,

packages/snaps-sdk/src/jsx/validation.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,20 @@ export const FieldChildUnionStruct = nullUnion([
481481
/**
482482
* A subset of JSX elements that are allowed as children of the Field component.
483483
*/
484-
const FieldChildStruct = nullUnion([
485-
tuple(BOX_INPUT_LEFT),
486-
tuple(BOX_INPUT_RIGHT),
487-
tuple(BOX_INPUT_BOTH),
488-
...FIELD_CHILDREN_ARRAY,
489-
]) as unknown as Struct<
484+
const FieldChildStruct = selectiveUnion((value) => {
485+
const isArray = Array.isArray(value);
486+
if (isArray && value.length === 3) {
487+
return tuple(BOX_INPUT_BOTH);
488+
}
489+
490+
if (isArray && value.length === 2) {
491+
return value[0]?.type === 'Box'
492+
? tuple(BOX_INPUT_LEFT)
493+
: tuple(BOX_INPUT_RIGHT);
494+
}
495+
496+
return typedUnion(FIELD_CHILDREN_ARRAY);
497+
}) as unknown as Struct<
490498
| [InputElement, GenericSnapChildren]
491499
| [GenericSnapChildren, InputElement]
492500
| [GenericSnapChildren, InputElement, GenericSnapChildren]

0 commit comments

Comments
 (0)