Skip to content

Commit

Permalink
feat(provable-generic.ts): add error handling for 'Struct' type in cr…
Browse files Browse the repository at this point in the history
…eateDerivers function to prevent incorrect usage and provide correct usage examples
  • Loading branch information
MartinMinkov committed Jun 27, 2024
1 parent df307f8 commit dac81dc
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/provable-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ function createDerivers<Field>(): {
if (!complexTypes.has(typeof typeObj))
throw Error(`provable: unsupported type "${typeObj}"`);

if (display(typeObj) === 'Struct')
throw Error(
`provable: cannot run check() on 'Struct' type. ` +
`Instead of using 'Struct' directly, extend 'Struct' to create a specific type.\n\n` +
`Example:\n` +
`// Incorrect Usage:\n` +
`class MyStruct extends Struct({\n` +
` fieldA: Struct, // This is incorrect\n` +
`}) {}\n\n` +
`// Correct Usage:\n` +
`class MyStruct extends Struct({\n` +
` fieldA: MySpecificStruct, // Use the specific struct type\n` +
`}) {}\n`
);

if (Array.isArray(typeObj))
return typeObj.forEach((t, i) => check(t, obj[i]));

Expand Down

0 comments on commit dac81dc

Please sign in to comment.