Skip to content

Commit 159f448

Browse files
fix: Improve field validation errors
1 parent 9d8915b commit 159f448

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

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
@@ -480,12 +480,20 @@ export const FieldChildUnionStruct = nullUnion([
480480
/**
481481
* A subset of JSX elements that are allowed as children of the Field component.
482482
*/
483-
const FieldChildStruct = nullUnion([
484-
tuple(BOX_INPUT_LEFT),
485-
tuple(BOX_INPUT_RIGHT),
486-
tuple(BOX_INPUT_BOTH),
487-
...FIELD_CHILDREN_ARRAY,
488-
]) as unknown as Struct<
483+
const FieldChildStruct = selectiveUnion((value) => {
484+
const isArray = Array.isArray(value);
485+
if (isArray && value.length === 3) {
486+
return tuple(BOX_INPUT_BOTH);
487+
}
488+
489+
if (isArray && value.length === 2) {
490+
return value[0]?.type === 'Box'
491+
? tuple(BOX_INPUT_LEFT)
492+
: tuple(BOX_INPUT_RIGHT);
493+
}
494+
495+
return typedUnion([...FIELD_CHILDREN_ARRAY]);
496+
}) as unknown as Struct<
489497
| [InputElement, GenericSnapChildren]
490498
| [GenericSnapChildren, InputElement]
491499
| [GenericSnapChildren, InputElement, GenericSnapChildren]

0 commit comments

Comments
 (0)