From c7a6906f26499c0570e82eab9a75c916a446b7c1 Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Tue, 10 Dec 2024 10:52:18 +0100 Subject: [PATCH] fix: Improve field validation errors (#2937) Improves validation errors for `Field` by rewriting the struct using `selectiveUnion`. Fixes https://github.com/MetaMask/snaps/issues/2927 --------- Co-authored-by: Maarten Zuidhoorn --- .../browserify-plugin/snap.manifest.json | 2 +- .../packages/browserify/snap.manifest.json | 2 +- .../src/permitted/createInterface.test.tsx | 2 +- packages/snaps-sdk/src/jsx/validation.ts | 20 +++++++++++++------ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/examples/packages/browserify-plugin/snap.manifest.json b/packages/examples/packages/browserify-plugin/snap.manifest.json index 4fa97b9011..aeca81fbc3 100644 --- a/packages/examples/packages/browserify-plugin/snap.manifest.json +++ b/packages/examples/packages/browserify-plugin/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "lyy+HECzOFQzuzjyD1nas6GPc4kMt340ChbqQZl7uEo=", + "shasum": "DYLBW3jdovQYu6jwACo/3xlujGXb0YxJ19UEpUKX1qE=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/browserify/snap.manifest.json b/packages/examples/packages/browserify/snap.manifest.json index f21cb160da..523a7c7139 100644 --- a/packages/examples/packages/browserify/snap.manifest.json +++ b/packages/examples/packages/browserify/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "k7NYoT8jI2E4u785PN5PoruwtjF18KNOSagNY9fS44U=", + "shasum": "dFTqb94KtIZ44IaoMYhGunl9c9Of9j67TNYobl+CwYg=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snaps-rpc-methods/src/permitted/createInterface.test.tsx b/packages/snaps-rpc-methods/src/permitted/createInterface.test.tsx index 15e530e3a1..3e99bf7129 100644 --- a/packages/snaps-rpc-methods/src/permitted/createInterface.test.tsx +++ b/packages/snaps-rpc-methods/src/permitted/createInterface.test.tsx @@ -191,7 +191,7 @@ describe('snap_createInterface', () => { error: { code: -32602, message: - '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].', + 'Invalid params: At path: ui.props.children.props.children -- Expected type to be one of: "Input", "Dropdown", "RadioGroup", "FileInput", "Checkbox", "Selector", but received: "Copyable".', stack: expect.any(String), }, id: 1, diff --git a/packages/snaps-sdk/src/jsx/validation.ts b/packages/snaps-sdk/src/jsx/validation.ts index 129f3fa4e6..a2905fc08b 100644 --- a/packages/snaps-sdk/src/jsx/validation.ts +++ b/packages/snaps-sdk/src/jsx/validation.ts @@ -481,12 +481,20 @@ export const FieldChildUnionStruct = nullUnion([ /** * A subset of JSX elements that are allowed as children of the Field component. */ -const FieldChildStruct = nullUnion([ - tuple(BOX_INPUT_LEFT), - tuple(BOX_INPUT_RIGHT), - tuple(BOX_INPUT_BOTH), - ...FIELD_CHILDREN_ARRAY, -]) as unknown as Struct< +const FieldChildStruct = selectiveUnion((value) => { + const isArray = Array.isArray(value); + if (isArray && value.length === 3) { + return tuple(BOX_INPUT_BOTH); + } + + if (isArray && value.length === 2) { + return value[0]?.type === 'Box' + ? tuple(BOX_INPUT_LEFT) + : tuple(BOX_INPUT_RIGHT); + } + + return typedUnion(FIELD_CHILDREN_ARRAY); +}) as unknown as Struct< | [InputElement, GenericSnapChildren] | [GenericSnapChildren, InputElement] | [GenericSnapChildren, InputElement, GenericSnapChildren]