Skip to content

Commit 17d253a

Browse files
committed
change: remove name-property from JsonError
1 parent 68362c2 commit 17d253a

8 files changed

+140
-114
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ In _json-schema-library_ all errors are in the format of a `JsonError`:
186186
```ts
187187
type JsonError = {
188188
type: "error";
189-
name: string;
190189
code: string;
191190
message: string;
192191
data?: { [p: string]: any };
@@ -210,7 +209,6 @@ const { errors } = compileSchema(myJsonSchema).validate({ name: "my-data" });
210209
expect(errors).to.deep.equal([
211210
{
212211
type: "error",
213-
name: "NoAdditionalPropertiesError",
214212
code: "no-additional-properties-error",
215213
message: "Additional property `name` in `#` is not allowed",
216214
data: { property: "name", properties: [], pointer: "#" }
@@ -965,15 +963,13 @@ const myDraft = extendDraft(draft2020, {
965963
if (data.minLength === 1) {
966964
return {
967965
type: "error",
968-
name: "MinLengthOneError",
969966
code: dashCase("MinLengthOneError"),
970967
message: "Input is required",
971968
data
972969
};
973970
}
974971
return {
975972
type: "error",
976-
name: "MinLengthError",
977973
code: dashCase("MinLengthError"),
978974
message: render("Value in `{{pointer}}` is `{{length}}`, but should be `{{minimum}}` at minimum", data),
979975
data
@@ -1168,6 +1164,9 @@ The new implementation revolves around compiling schemas into a **SchemaNode** t
11681164

11691165
- **Draft Customization**: Customizing drafts has changed completely. The previous methods of extending drafts are no longer valid, and draft handling is now centered around `SchemaNode`.
11701166

1167+
- **Remove Property**:
1168+
Error property `name` has been removed from `JsonError` in favor of `code`.
1169+
11711170
- **Removed Configuration Option**:
11721171
The `templateDefaultOptions` property has been removed from the global settings object. You should now configure it using the `compileSchema` options:
11731172

src/SchemaNode.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ export type GetSchemaOptions = {
125125
};
126126

127127
export const SchemaNodeMethods = {
128-
/** Compiles a child-schema of this node to its context */
128+
/**
129+
* Compiles a child-schema of this node to its context
130+
* @returns SchemaNode representing the passed JSON Schema
131+
*/
129132
compileSchema(schema: JsonSchema, spointer: string = this.spointer, schemaId?: string): SchemaNode {
130133
const nextFragment = spointer.split("/$ref")[0];
131134
const parentNode = this as SchemaNode;
@@ -156,7 +159,7 @@ export const SchemaNodeMethods = {
156159
}
157160
errorMessage = render(error ?? name, data);
158161
}
159-
return { type: "error", name, code: dashCase(name), message: errorMessage, data };
162+
return { type: "error", code: dashCase(name), message: errorMessage, data };
160163
},
161164

162165
createSchema,
@@ -216,11 +219,17 @@ export const SchemaNodeMethods = {
216219
return isJsonError(result) ? { node: undefined, error: result } : { node: result, error: undefined };
217220
},
218221

222+
/**
223+
* @returns for $ref, the corresponding SchemaNode or undefined
224+
*/
219225
getRef($ref: string): SchemaNode | undefined {
220226
const node = this as SchemaNode;
221227
return node.compileSchema({ $ref }).resolveRef();
222228
},
223229

230+
/**
231+
* @returns child node identified by property as SchemaNode
232+
*/
224233
getChild(key: string | number, data?: unknown, options: GetSchemaOptions = {}): OptionalNodeAndError {
225234
options.path = options.path ?? [];
226235

@@ -271,11 +280,16 @@ export const SchemaNodeMethods = {
271280
return { node: undefined, error: undefined };
272281
},
273282

283+
/**
284+
* @returns draft version this JSON Schema is evaluated by
285+
*/
274286
getDraftVersion() {
275287
return (this as SchemaNode).context.version;
276288
},
277289

278-
/** Creates data that is valid to the schema of this node */
290+
/**
291+
* @returns data that is valid to the schema of this node
292+
*/
279293
getData(data?: unknown, options?: TemplateOptions) {
280294
const node = this as SchemaNode;
281295
const opts = {
@@ -287,6 +301,9 @@ export const SchemaNodeMethods = {
287301
return node.context.methods.getData(node, data, opts);
288302
},
289303

304+
/**
305+
* @returns SchemaNode with a reduced JSON Schema matching the given data
306+
*/
290307
reduceSchema(
291308
data: unknown,
292309
options: { key?: string | number; pointer?: string; path?: ValidationPath } = {}
@@ -343,7 +360,9 @@ export const SchemaNodeMethods = {
343360
return { node: workingNode, error: undefined };
344361
},
345362

346-
/** Creates a new node with all dynamic schema properties merged according to the passed in data */
363+
/**
364+
* @returns validation result of data validated by this node's JSON Schema
365+
*/
347366
validate(data: unknown, pointer = "#", path: ValidationPath = []): { valid: boolean; errors: JsonError[] } {
348367
const errors = validateNode(this, data, pointer, path) ?? [];
349368
const flatErrorList = sanitizeErrors(Array.isArray(errors) ? errors : [errors]).filter(isJsonError);
@@ -353,6 +372,9 @@ export const SchemaNodeMethods = {
353372
};
354373
},
355374

375+
/**
376+
* @returns a promise which resolves to validation-result
377+
*/
356378
async validateAsync(
357379
data: unknown,
358380
pointer = "#",
@@ -401,10 +423,16 @@ export const SchemaNodeMethods = {
401423
return this;
402424
},
403425

426+
/**
427+
* @returns a list of all sub-schema as SchemaNode
428+
*/
404429
toSchemaNodes() {
405430
return toSchemaNodes(this as SchemaNode);
406431
},
407432

433+
/**
434+
* @returns a list of values (including objects and arrays) and their corresponding JSON Schema as SchemaNode
435+
*/
408436
toDataNodes(data: unknown, pointer?: string) {
409437
const node = this as SchemaNode;
410438
return node.context.methods.toDataNodes(node, data, pointer);

src/compileSchema.getSchema.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ describe("compileSchema : getSchema", () => {
310310
additionalProperties: false
311311
}).getSchema("#/beer");
312312

313-
assert.deepEqual(error.name, "NoAdditionalPropertiesError");
313+
assert.deepEqual(error.code, "no-additional-properties-error");
314314
});
315315

316316
describe("dependencies", () => {

0 commit comments

Comments
 (0)