Skip to content

Commit c41da09

Browse files
authored
fix: improve JSON schema validation error messages (#69)
1 parent 8ceac05 commit c41da09

File tree

5 files changed

+477
-6
lines changed

5 files changed

+477
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ node_modules
1010
/.env
1111
/.eslintcache
1212
/.vitepress/.cache
13+
/coverage
1314

1415
/llama/compile_commands.json
1516
/llama/llama.cpp

package-lock.json

Lines changed: 218 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"@types/yargs": "^17.0.24",
111111
"@typescript-eslint/eslint-plugin": "^6.3.0",
112112
"@typescript-eslint/parser": "^6.3.0",
113+
"@vitest/coverage-v8": "^0.34.6",
113114
"eslint": "^8.46.0",
114115
"eslint-plugin-import": "^2.28.0",
115116
"eslint-plugin-node": "github:giladgd/eslint-plugin-node#dev/giladgd/fixImportExtentionFixingInTypeScript",

src/utils/gbnfJson/utils/validateObjectAgainstGbnfSchema.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ function validateObjectWithGbnfSchema<T extends GbnfJsonSchema>(object: any, sch
5252
return true;
5353
}
5454

55-
throw new Error(`Expected one of ${schema.type.join(", ")} but got "${typeof object}"`);
55+
throw new Error(`Expected one type of [${
56+
schema.type.map((type) => JSON.stringify(type)).join(", ")
57+
}] but got type "${object === null ? null : typeof object}"`);
5658
}
5759

5860
if (validateImmutableType(object, schema.type))
5961
return true;
6062

61-
throw new Error(`Expected "${schema.type}" but got "${typeof object}"`);
63+
throw new Error(`Expected "${schema.type}" but got "${object === null ? "null" : typeof object}"`);
6264
}
6365

6466
function validateArray<T extends GbnfJsonArraySchema>(object: any, schema: T): object is GbnfJsonSchemaToType<T> {
@@ -87,7 +89,7 @@ function validateObject<T extends GbnfJsonObjectSchema>(object: any, schema: T):
8789

8890
const missingKeys = schemaKeys.filter((key) => !objectKeysSet.has(key));
8991
if (missingKeys.length > 0)
90-
throw new TechnicalValidationError(`Missing keys: ${extraKeys.map((key) => JSON.stringify(key)).join(", ")}`);
92+
throw new TechnicalValidationError(`Missing keys: ${missingKeys.map((key) => JSON.stringify(key)).join(", ")}`);
9193

9294
let res = true;
9395
for (const key of schemaKeys)
@@ -117,9 +119,9 @@ function validateEnum<T extends GbnfJsonEnumSchema>(object: any, schema: T): obj
117119
return true;
118120
}
119121

120-
throw new TechnicalValidationError(`Expected one of ${
122+
throw new TechnicalValidationError(`Expected one of [${
121123
schema.enum.map((item) => JSON.stringify(item)).join(", ")
122-
} but got ${JSON.stringify(object)}`);
124+
}] but got ${JSON.stringify(object)}`);
123125
}
124126

125127
function validateConst<T extends GbnfJsonConstSchema>(object: any, schema: T): object is GbnfJsonSchemaToType<T> {

0 commit comments

Comments
 (0)