Skip to content

Commit c3003b9

Browse files
committed
add: optional meta object on path-entry and fix async validation type
1 parent 9806d8d commit c3003b9

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed

src/Keyword.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { SchemaNode, JsonError } from "./types";
33
export type ValidationPath = {
44
pointer: string;
55
node: SchemaNode;
6+
meta?: Record<string, any>;
67
}[];
78

89
export type JsonSchemaReducerParams = {
@@ -30,7 +31,7 @@ export interface JsonSchemaResolver {
3031
(options: JsonSchemaResolverParams): SchemaNode | JsonError | undefined;
3132
}
3233

33-
export type ValidationResult = JsonError | Promise<JsonError>;
34+
export type ValidationResult = JsonError | Promise<JsonError | undefined>;
3435

3536
export type JsonSchemaValidatorParams = { pointer?: string; data: unknown; node: SchemaNode; path?: ValidationPath };
3637
export interface JsonSchemaValidator {

src/compileSchema.validate.test.ts

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { compileSchema } from "./compileSchema";
22
import { strict as assert } from "assert";
33
import { Draft, JsonError, SchemaNode } from "./types";
44
import { draft2020 } from "./draft2020";
5+
import { ValidationPath } from "./Keyword";
56

67
describe("compileSchema.validate", () => {
78
describe("integer", () => {
@@ -966,9 +967,9 @@ describe("compileSchema.validate - errorsAsync", () => {
966967
...draft2020.keywords,
967968
{
968969
id: "async",
969-
keyword: "async-error",
970+
keyword: "asyncError",
970971
addValidate: (node) => node.schema.asyncError != null,
971-
validate: async ({ node }): Promise<JsonError> => {
972+
validate: async ({ node }) => {
972973
if (node.schema.asyncError === false) {
973974
return undefined;
974975
}
@@ -981,26 +982,65 @@ describe("compileSchema.validate - errorsAsync", () => {
981982
}
982983
]
983984
};
985+
});
984986

985-
it("should resolve async validation returning no error", async () => {
986-
const { errors, errorsAsync } = compileSchema(
987-
{ type: "number", asyncError: false },
988-
{ drafts: [draft] }
989-
).validate(4);
990-
const asyncErrors = await Promise.all(errorsAsync);
991-
assert.deepEqual(errors.length, 0);
992-
assert.deepEqual(asyncErrors.length, 0);
993-
});
987+
it("should resolve async validation returning no error", async () => {
988+
const { errors, errorsAsync } = compileSchema(
989+
{ type: "number", asyncError: false },
990+
{ drafts: [draft] }
991+
).validate(4);
994992

995-
it("should resolve async validation errors", async () => {
996-
const { errorsAsync } = compileSchema(
997-
{ type: "number", asyncError: true },
998-
{ drafts: [draft] }
999-
).validate(4);
1000-
const asyncErrors = await Promise.all(errorsAsync);
1001-
assert.deepEqual(asyncErrors.length, 1);
1002-
assert.deepEqual(asyncErrors[0].code, "type-error");
1003-
});
993+
const asyncErrors = (await Promise.all(errorsAsync)).filter((e) => e != null);
994+
995+
assert.deepEqual(errors.length, 0);
996+
assert.deepEqual(asyncErrors.length, 0);
997+
});
998+
999+
it("should resolve async validation errors", async () => {
1000+
const { errorsAsync } = compileSchema({ type: "number", asyncError: true }, { drafts: [draft] }).validate(
1001+
4
1002+
);
1003+
1004+
const asyncErrors = (await Promise.all(errorsAsync)).filter((e) => e != null);
1005+
1006+
assert(asyncErrors.length === 1);
1007+
1008+
const error = asyncErrors[0];
1009+
assert(error != null);
1010+
assert.deepEqual(error.code, "type-error");
1011+
});
1012+
});
1013+
1014+
describe("async validation - returned metadata", () => {
1015+
let draft: Draft;
1016+
beforeEach(() => {
1017+
draft = {
1018+
...draft2020,
1019+
keywords: [
1020+
...draft2020.keywords,
1021+
{
1022+
id: "async",
1023+
keyword: "asyncError",
1024+
addValidate: (node) => node.schema.asyncError != null,
1025+
validate: async ({ path }) => {
1026+
return new Promise((resolve) => {
1027+
if (path) {
1028+
path[path.length - 1].meta = {
1029+
test: "yay"
1030+
};
1031+
}
1032+
resolve(undefined);
1033+
});
1034+
}
1035+
}
1036+
]
1037+
};
1038+
});
1039+
1040+
it("should expose meta object on path", async () => {
1041+
const path: ValidationPath = [];
1042+
compileSchema({ type: "number", asyncError: true }, { drafts: [draft] }).validate(4, "#", path);
1043+
assert.deepEqual(path[path.length - 1].meta?.test, "yay");
10041044
});
10051045
});
10061046
});

0 commit comments

Comments
 (0)