From dac81dc823dc3c251c3967bcd7a59097cafd6ca3 Mon Sep 17 00:00:00 2001 From: Martin Minkov Date: Thu, 27 Jun 2024 11:43:39 -0700 Subject: [PATCH 1/4] feat(provable-generic.ts): add error handling for 'Struct' type in createDerivers function to prevent incorrect usage and provide correct usage examples --- lib/provable-generic.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/provable-generic.ts b/lib/provable-generic.ts index f15c5fc1..7cc491e9 100644 --- a/lib/provable-generic.ts +++ b/lib/provable-generic.ts @@ -173,6 +173,21 @@ function createDerivers(): { 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])); From 43c123216f209683f4529a7a962b5441aa6bd5d9 Mon Sep 17 00:00:00 2001 From: Martin Minkov Date: Mon, 1 Jul 2024 11:18:10 -0700 Subject: [PATCH 2/4] refactor(provable-generic.ts): reorder condition checks for efficiency --- lib/provable-generic.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/provable-generic.ts b/lib/provable-generic.ts index 7cc491e9..eff910e3 100644 --- a/lib/provable-generic.ts +++ b/lib/provable-generic.ts @@ -173,6 +173,11 @@ function createDerivers(): { if (!complexTypes.has(typeof typeObj)) throw Error(`provable: unsupported type "${typeObj}"`); + if (Array.isArray(typeObj)) + return typeObj.forEach((t, i) => check(t, obj[i])); + + if (isProvable(typeObj)) return typeObj.check(obj); + if (display(typeObj) === 'Struct') throw Error( `provable: cannot run check() on 'Struct' type. ` + @@ -188,11 +193,6 @@ function createDerivers(): { `}) {}\n` ); - if (Array.isArray(typeObj)) - return typeObj.forEach((t, i) => check(t, obj[i])); - - if (isProvable(typeObj)) return typeObj.check(obj); - return Object.keys(typeObj).forEach((k) => check(typeObj[k], obj[k])); } From 18f878d04717b1da0a4951210677c310bbb88853 Mon Sep 17 00:00:00 2001 From: Martin Minkov Date: Mon, 1 Jul 2024 11:25:13 -0700 Subject: [PATCH 3/4] fix(provable-generic.ts): add error handling for function types --- lib/provable-generic.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/provable-generic.ts b/lib/provable-generic.ts index eff910e3..848dde8d 100644 --- a/lib/provable-generic.ts +++ b/lib/provable-generic.ts @@ -178,8 +178,15 @@ function createDerivers(): { if (isProvable(typeObj)) return typeObj.check(obj); - if (display(typeObj) === 'Struct') - throw Error( + if (typeof typeObj === 'function') { + throw new Error( + `provable: invalid type detected. Functions are not supported as types. ` + + `Ensure you are passing an instance of a supported type or an anonymous object.\n` + ); + } + + if (display(typeObj) === 'Struct') { + throw new Error( `provable: cannot run check() on 'Struct' type. ` + `Instead of using 'Struct' directly, extend 'Struct' to create a specific type.\n\n` + `Example:\n` + @@ -192,7 +199,9 @@ function createDerivers(): { ` fieldA: MySpecificStruct, // Use the specific struct type\n` + `}) {}\n` ); + } + // Only recurse into the object if it's an object and not a function return Object.keys(typeObj).forEach((k) => check(typeObj[k], obj[k])); } From cbdbff3d9c317aa1131667cd56f2a8a374640436 Mon Sep 17 00:00:00 2001 From: Martin Minkov Date: Tue, 2 Jul 2024 09:06:37 -0700 Subject: [PATCH 4/4] refactor(provable-generic.ts): move function type check after Struct type check for better error handling order --- lib/provable-generic.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/provable-generic.ts b/lib/provable-generic.ts index 848dde8d..1265631e 100644 --- a/lib/provable-generic.ts +++ b/lib/provable-generic.ts @@ -178,13 +178,6 @@ function createDerivers(): { if (isProvable(typeObj)) return typeObj.check(obj); - if (typeof typeObj === 'function') { - throw new Error( - `provable: invalid type detected. Functions are not supported as types. ` + - `Ensure you are passing an instance of a supported type or an anonymous object.\n` - ); - } - if (display(typeObj) === 'Struct') { throw new Error( `provable: cannot run check() on 'Struct' type. ` + @@ -201,6 +194,13 @@ function createDerivers(): { ); } + if (typeof typeObj === 'function') { + throw new Error( + `provable: invalid type detected. Functions are not supported as types. ` + + `Ensure you are passing an instance of a supported type or an anonymous object.\n` + ); + } + // Only recurse into the object if it's an object and not a function return Object.keys(typeObj).forEach((k) => check(typeObj[k], obj[k])); }