Skip to content

Commit d71a75a

Browse files
committed
add AST-based schema value resolver
1 parent 94348fc commit d71a75a

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

src/json-schema-errors.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { JsonSchemaErrorsOutputPlugin } from "./output-plugin.js";
99
/**
1010
* @import * as API from "./index.d.ts"
1111
* @import { Browser } from "@hyperjump/browser";
12-
* @import { SchemaDocument, CompiledSchema } from "@hyperjump/json-schema/experimental";
12+
* @import { AST, SchemaDocument, CompiledSchema } from "@hyperjump/json-schema/experimental";
1313
* @import { JsonNode } from "@hyperjump/json-schema/instance/experimental"
1414
*/
1515

@@ -196,6 +196,53 @@ export const getErrors = async (normalizedErrors, rootInstance, localization) =>
196196
return errors;
197197
};
198198

199+
/**
200+
* @param {AST} ast
201+
* @param {string} schemaLocation
202+
* @returns {unknown}
203+
*/
204+
const getCompiledKeywordValue = (ast, schemaLocation) => {
205+
const parentLocation = schemaLocation.replace(/\/[^/]+$/, "");
206+
const parentNode = ast[parentLocation];
207+
if (!Array.isArray(parentNode)) {
208+
return undefined;
209+
}
210+
211+
for (const [, keywordLocation, keywordValue] of parentNode) {
212+
if (keywordLocation === schemaLocation) {
213+
return keywordValue;
214+
}
215+
}
216+
217+
return undefined;
218+
};
219+
220+
/**
221+
* @param {AST} ast
222+
* @param {string} schemaLocation
223+
* @param {string} siblingKeywordUri
224+
* @returns {{ keywordLocation: string; keywordValue: unknown } | undefined}
225+
*/
226+
const getSiblingKeywordValue = (ast, schemaLocation, siblingKeywordUri) => {
227+
const parentLocation = schemaLocation.replace(/\/[^/]+$/, "");
228+
const parentNode = ast[parentLocation];
229+
230+
if (!Array.isArray(parentNode)) {
231+
return undefined;
232+
}
233+
234+
for (const [keywordUri, keywordLocation, keywordValue] of parentNode) {
235+
if (keywordUri === siblingKeywordUri) {
236+
return {
237+
keywordLocation,
238+
keywordValue
239+
};
240+
}
241+
}
242+
243+
return undefined;
244+
};
245+
199246
/**
200247
* @overload
201248
* @param {string} schemaUri

0 commit comments

Comments
 (0)