Skip to content

Commit 1df7e01

Browse files
committed
Restore the plugin system and fix the type error properly
1 parent 3f94311 commit 1df7e01

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AST } from "@hyperjump/json-schema/experimental";
1+
import { AST, EvaluationPlugin } from "@hyperjump/json-schema/experimental";
22
import { JsonNode } from "@hyperjump/json-schema/instance/experimental";
33
import { Localization } from "./localization.js";
44

@@ -63,6 +63,7 @@ export type KeywordHandler = {
6363
export type EvaluationContext = {
6464
ast: AST;
6565
errorIndex: ErrorIndex;
66+
plugins: EvaluationPlugin[];
6667
};
6768

6869
export type ErrorIndex = {

src/normalization-handlers/unevaluatedItems.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { evaluateSchema } from "../normalized-output.js";
22
import * as Instance from "@hyperjump/json-schema/instance/experimental";
33

44
/**
5-
* @import { EvaluationContext, KeywordHandler, NormalizedOutput } from "../index.d.ts"
65
* @import { EvaluationPlugin } from "@hyperjump/json-schema/experimental"
6+
* @import { EvaluationContext, KeywordHandler, NormalizedOutput } from "../index.d.ts"
77
*/
88

99
/**

src/normalized-output.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { compile, getSchema } from "@hyperjump/json-schema/experimental";
1+
import { compile, getKeyword, getSchema } from "@hyperjump/json-schema/experimental";
22
import * as Instance from "@hyperjump/json-schema/instance/experimental";
33
import { pointerSegments } from "@hyperjump/json-pointer";
44
import * as Browser from "@hyperjump/browser";
@@ -22,9 +22,14 @@ export const setNormalizationHandler = (uri, handler) => {
2222
export const evaluateSchema = (schemaLocation, instance, context) => {
2323
const instanceLocation = Instance.uri(instance);
2424

25+
let valid = true;
2526
/** @type API.NormalizedOutput */
2627
const output = { [instanceLocation]: {} };
2728

29+
for (const plugin of context.plugins) {
30+
plugin.beforeSchema?.(schemaLocation, instance, context);
31+
}
32+
2833
const schemaNode = context.ast[schemaLocation];
2934
if (typeof schemaNode === "boolean") {
3035
output[instanceLocation] = {
@@ -37,13 +42,22 @@ export const evaluateSchema = (schemaLocation, instance, context) => {
3742
const [keywordUri, keywordLocation, keywordValue] = node;
3843
const keyword = keywordHandlers[keywordUri] ?? {};
3944

45+
const validationKeyword = getKeyword(keywordUri);
46+
4047
const keywordContext = {
4148
ast: context.ast,
42-
errorIndex: context.errorIndex
49+
errorIndex: context.errorIndex,
50+
plugins: context.plugins
4351
};
52+
for (const plugin of context.plugins) {
53+
plugin.beforeKeyword?.(node, instance, keywordContext, context, validationKeyword);
54+
}
4455

4556
const keywordOutput = keyword.evaluate?.(keywordValue, instance, keywordContext);
4657
const isKeywordValid = !context.errorIndex[keywordLocation]?.[instanceLocation];
58+
if (!isKeywordValid) {
59+
valid = false;
60+
}
4761

4862
if (keyword.simpleApplicator) {
4963
for (const suboutput of (keywordOutput ?? [])) {
@@ -56,9 +70,17 @@ export const evaluateSchema = (schemaLocation, instance, context) => {
5670
output[instanceLocation][keywordUri] ??= {};
5771
output[instanceLocation][keywordUri][keywordLocation] = isKeywordValid;
5872
}
73+
74+
for (const plugin of context.plugins) {
75+
plugin.afterKeyword?.(node, instance, keywordContext, isKeywordValid, context, validationKeyword);
76+
}
5977
}
6078
}
6179

80+
for (const plugin of context.plugins) {
81+
plugin.afterSchema?.(schemaLocation, instance, context, valid);
82+
}
83+
6284
return output;
6385
};
6486

@@ -122,6 +144,6 @@ export async function normalizedErrorOuput(instance, errorOutput, subjectUri) {
122144
const { schemaUri, ast } = await compile(schema);
123145
const value = Instance.fromJs(instance);
124146
/** @type API.EvaluationContext */
125-
const context = { ast, errorIndex };
147+
const context = { ast, errorIndex, plugins: [] };
126148
return evaluateSchema(schemaUri, value, context);
127149
}

0 commit comments

Comments
 (0)