Skip to content

Commit

Permalink
dist: update
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Apr 12, 2024
1 parent 10d49f5 commit adb5f99
Show file tree
Hide file tree
Showing 22 changed files with 113 additions and 107 deletions.
2 changes: 1 addition & 1 deletion dist/jsonSchemaLibrary.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/lib/draft/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ export declare class Draft {
getTemplate(data?: unknown, schema?: JsonSchema, opts?: TemplateOptions): any;
isValid(data: unknown, schema?: JsonSchema, pointer?: JsonPointer): boolean;
resolveAnyOf(data: any, schema: JsonSchema, pointer?: JsonPointer): SchemaNode | JsonError;
resolveAllOf(data: any, schema: JsonSchema): JsonSchema;
resolveAllOf(data: any, schema: JsonSchema): SchemaNode | JsonError;
resolveRef(node: SchemaNode): SchemaNode;
resolveOneOf(data: any, schema: JsonSchema, pointer?: JsonPointer): SchemaNode | JsonError;
resolveOneOf(node: SchemaNode, data: unknown): SchemaNode | JsonError;
setSchema(schema: JsonSchema): void;
step(node: SchemaNode, key: string | number, data: any): SchemaNode | JsonError;
/**
Expand Down
5 changes: 2 additions & 3 deletions dist/lib/each.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Draft } from "./draft";
import { JsonSchema, JsonPointer } from "./types";
import { JsonSchema, JsonPointer, SchemaNode } from "./types";
export type EachCallback = (schema: JsonSchema, data: unknown, pointer: JsonPointer) => void;
/**
* Iterates over data, retrieving its schema
Expand All @@ -10,4 +9,4 @@ export type EachCallback = (schema: JsonSchema, data: unknown, pointer: JsonPoin
* @param [schema] - the schema matching the data. Defaults to rootSchema
* @param [pointer] - pointer to current data. Default to rootPointer
*/
export declare function each(draft: Draft, data: any, callback: EachCallback, schema?: JsonSchema, pointer?: JsonPointer): void;
export declare function each(schemaNode: SchemaNode, data: any, callback: EachCallback): void;
2 changes: 1 addition & 1 deletion dist/lib/features/allOf.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Draft } from "../draft";
* when complete this will have much duplication to step.object etc
*/
export declare function resolveSchema(node: SchemaNode, data: unknown): SchemaNode | JsonError;
export declare function resolveAllOf(draft: Draft, data: any, schema?: JsonSchema): JsonSchema | JsonError;
export declare function resolveAllOf(node: SchemaNode, data: any): SchemaNode | JsonError;
/**
* @attention: subschemas have to be resolved upfront (e.g. if-else that do not apply)
* Merge all allOf sub schema into a single schema. Returns undefined for
Expand Down
5 changes: 2 additions & 3 deletions dist/lib/features/anyOf.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { JsonSchema, JsonValidator, JsonError, SchemaNode } from "../types";
import { Draft } from "../draft";
import { JsonValidator, JsonError, SchemaNode } from "../types";
/**
* returns merged schema of all valid anyOf subschemas for the given input data.
* Does not merge with rest input schema.
*
* @returns merged anyOf subschemas which are valid to the given input data.
*/
export declare function mergeValidAnyOfSchema(draft: Draft, schema: JsonSchema, data: unknown): JsonSchema;
export declare function mergeValidAnyOfSchema(node: SchemaNode, data: unknown): SchemaNode;
/**
* @unused this function is only exposed via draft and not used otherwise
* @returns extended input schema with valid anyOf subschemas or JsonError if
Expand Down
2 changes: 1 addition & 1 deletion dist/lib/resolveDynamicSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export declare function isDynamicSchema(schema: JsonData): boolean;
* @returns static schema from resolved dynamic schema definitions for this
* specific input data
*/
export declare function resolveDynamicSchema(_node: SchemaNode, data: unknown): SchemaNode | JsonError<import("./types").ErrorData<{
export declare function resolveDynamicSchema(schemaNode: SchemaNode, data: unknown): SchemaNode | JsonError<import("./types").ErrorData<{
[p: string]: unknown;
}>>;
14 changes: 8 additions & 6 deletions dist/module/lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ export default function compileSchema(draft, schemaToCompile, rootSchema = schem
const context = { ids: {}, remotes: draft.remotes };
const rootSchemaAsString = JSON.stringify(schemaToCompile);
const compiledSchema = JSON.parse(rootSchemaAsString);
// flag this schema as compiled
Object.defineProperty(compiledSchema, COMPILED, { enumerable: false, value: true });
// add getRef-helper to this object
Object.defineProperty(compiledSchema, GET_REF, {
enumerable: false,
value: getRef.bind(null, context, compiledSchema)
Object.defineProperties(compiledSchema, {
// flag this schema as compiled
[COMPILED]: { enumerable: false, value: true },
// add getRef-helper to this object
[GET_REF]: {
enumerable: false,
value: getRef.bind(null, context, compiledSchema)
}
});
// bail early, when no $refs are defined
if (force === false && rootSchemaAsString.includes("$ref") === false) {
Expand Down
9 changes: 5 additions & 4 deletions dist/module/lib/draft/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export class Draft {
* @param [pointer] - pointer to current data. Default to rootPointer
*/
each(data, callback, schema, pointer) {
return this.config.each(this, data, callback, schema, pointer);
const node = createNode(this, schema !== null && schema !== void 0 ? schema : this.rootSchema, pointer);
return this.config.each(node, data, callback);
}
eachSchema(callback, schema = this.rootSchema) {
return this.config.eachSchema(schema, callback);
Expand Down Expand Up @@ -106,13 +107,13 @@ export class Draft {
return this.config.resolveAnyOf(node, data);
}
resolveAllOf(data, schema) {
return this.config.resolveAllOf(this, data, schema);
const node = createNode(this, schema, data);
return this.config.resolveAllOf(node, data);
}
resolveRef(node) {
return this.config.resolveRef(node);
}
resolveOneOf(data, schema, pointer) {
const node = createNode(this, schema, pointer);
resolveOneOf(node, data) {
return this.config.resolveOneOf(node, data);
}
setSchema(schema) {
Expand Down
12 changes: 7 additions & 5 deletions dist/module/lib/draft06/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export default function compileSchema(draft, schemaToCompile, rootSchema = schem
const context = { ids: {}, anchors: {}, remotes: draft.remotes };
const rootSchemaAsString = JSON.stringify(schemaToCompile);
const compiledSchema = JSON.parse(rootSchemaAsString);
Object.defineProperty(compiledSchema, COMPILED, { enumerable: false, value: true });
Object.defineProperty(compiledSchema, GET_CONTEXT, { enumerable: false, value: () => context });
Object.defineProperty(compiledSchema, GET_REF, {
enumerable: false,
value: getRef.bind(null, context, compiledSchema)
Object.defineProperties(compiledSchema, {
[COMPILED]: { enumerable: false, value: true },
[GET_CONTEXT]: { enumerable: false, value: () => context },
[GET_REF]: {
enumerable: false,
value: getRef.bind(null, context, compiledSchema)
}
});
// bail early, when no $refs are defined
if (force === false && rootSchemaAsString.includes("$ref") === false) {
Expand Down
2 changes: 1 addition & 1 deletion dist/module/lib/draft2019/step.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const stepType = {
if (targetNode && Array.isArray(targetNode.schema.oneOf)) {
// @special case: this is a mix of a schema and optional definitions
// we resolve the schema here and add the original schema to `oneOfSchema`
return draft.resolveOneOf(data[key], targetNode.schema, `${pointer}/${key}`);
return draft.resolveOneOf(targetNode, data[key]);
}
return targetNode;
}
Expand Down
26 changes: 13 additions & 13 deletions dist/module/lib/draft2019/validation/keyword.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Keywords from "../../draft06/validation/keyword";
import { createNode } from "../../types";
import { isObject } from "../../utils/isObject";
import { reduceSchema } from "../../reduceSchema";
import { validateDependentSchemas, validateDependentRequired } from "../../features/dependencies";
Expand All @@ -10,17 +9,17 @@ const getPatternTests = (patternProperties) => isObject(patternProperties) ?
Object.keys(patternProperties).map((pattern) => new RegExp(pattern))
: [];
/** tests if a property is evaluated by the given schema */
function isPropertyEvaluated(draft, objectSchema, propertyName, value) {
function isPropertyEvaluated(schemaNode, propertyName, value) {
var _a, _b;
const node = createNode(draft, objectSchema, propertyName);
const schema = draft.resolveRef(node).schema;
const node = schemaNode.draft.resolveRef(schemaNode);
const { schema } = node;
if (schema.additionalProperties === true) {
return true;
}
// PROPERTIES
if ((_a = schema.properties) === null || _a === void 0 ? void 0 : _a[propertyName]) {
const nextSchema = (_b = schema.properties) === null || _b === void 0 ? void 0 : _b[propertyName];
if (draft.isValid(value, nextSchema)) {
if (node.draft.isValid(value, nextSchema)) {
return true;
}
}
Expand All @@ -32,7 +31,7 @@ function isPropertyEvaluated(draft, objectSchema, propertyName, value) {
// ADDITIONAL-PROPERTIES
if (isObject(schema.additionalProperties)) {
const nextSchema = schema.additionalProperties;
return draft.validate(value, nextSchema);
return node.draft.validate(value, nextSchema);
}
return false;
}
Expand Down Expand Up @@ -73,7 +72,7 @@ const KeywordValidation = {
}
// special case: an evaluation in if statement counts too
// we have an unevaluated prop only if the if-schema does not match
if (isObject(schema.if) && isPropertyEvaluated(draft, { type: "object", ...schema.if }, key, value[key])) {
if (isObject(schema.if) && isPropertyEvaluated(node.next({ type: "object", ...schema.if }), key, value[key])) {
return false;
}
if (testPatterns.find(pattern => pattern.test(key))) {
Expand All @@ -100,10 +99,11 @@ const KeywordValidation = {
return errors;
}
unevaluated.forEach(key => {
// note: only key changes
const nextSchemaNode = resolvedSchema.unevaluatedProperties;
const keyErrors = draft.validate(value[key], nextSchemaNode, `${pointer}/${key}`);
errors.push(...keyErrors);
if (isObject(resolvedSchema.unevaluatedProperties)) {
// note: only key changes
const keyErrors = draft.validate(node.next(resolvedSchema.unevaluatedProperties, key), value[key]);
errors.push(...keyErrors);
}
});
return errors;
},
Expand Down Expand Up @@ -137,14 +137,14 @@ const KeywordValidation = {
}
if (isObject(resolvedSchema.items)) {
const nextSchemaNode = { ...resolvedSchema, unevaluatedItems: undefined };
const errors = draft.validate(value, nextSchemaNode, pointer);
const errors = draft.validate(node.next(nextSchemaNode), value);
return errors.map(e => draft.errors.unevaluatedItemsError({ ...e.data }));
}
if (Array.isArray(resolvedSchema.items)) {
const items = [];
for (let i = resolvedSchema.items.length; i < value.length; i += 1) {
if (i < resolvedSchema.items.length) {
if (!draft.isValid(value[i], resolvedSchema.items[i])) {
if (draft.validate(node.next(resolvedSchema.items[i], i), value[i]).length > 0) {
items.push({ index: i, value: value[i] });
}
}
Expand Down
21 changes: 12 additions & 9 deletions dist/module/lib/each.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getTypeOf from "./getTypeOf";
import { createNode } from "./types";
import { isSchemaNode } from "./types";
/**
* Iterates over data, retrieving its schema
*
Expand All @@ -9,22 +9,25 @@ import { createNode } from "./types";
* @param [schema] - the schema matching the data. Defaults to rootSchema
* @param [pointer] - pointer to current data. Default to rootPointer
*/
export function each(draft, data, callback, schema = draft.rootSchema, pointer = "#") {
const node = createNode(draft, schema, pointer);
schema = draft.resolveRef(node).schema;
export function each(schemaNode, data, callback) {
const node = schemaNode.draft.resolveRef(schemaNode);
const { draft, schema, pointer } = node;
callback(schema, data, pointer);
const dataType = getTypeOf(data);
if (dataType === "object") {
Object.keys(data).forEach((key) => {
const nextNode = draft.step(key, schema, data, pointer); // not save
const next = data[key]; // save
draft.each(next, callback, nextNode.schema, `${pointer}/${key}`);
const nextNode = draft.step(node, key, data);
if (isSchemaNode(nextNode)) {
each(nextNode, data[key], callback);
}
});
}
else if (dataType === "array") {
data.forEach((next, key) => {
const nextNode = draft.step(key, schema, data, pointer);
draft.each(next, callback, nextNode.schema, `${pointer}/${key}`);
const nextNode = draft.step(node, key, data);
if (isSchemaNode(nextNode)) {
each(nextNode, data[key], callback);
}
});
}
}
11 changes: 6 additions & 5 deletions dist/module/lib/features/allOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ export function resolveSchema(node, data) {
}
return node.next(omit(schema, "if", "then", "else"));
}
export function resolveAllOf(draft, data, schema = draft.rootSchema) {
export function resolveAllOf(node, data) {
const { schema, draft } = node;
let mergedSchema = shallowCloneSchemaNode(schema);
for (let i = 0; i < schema.allOf.length; i += 1) {
const allOfNode = draft.resolveRef(createNode(draft, schema.allOf[i]));
const allOfNode = draft.resolveRef(node.next(schema.allOf[i]));
// @todo introduce draft.resolveSchema to iteratively resolve
const allOfSchema = resolveSchema(allOfNode, data).schema;
mergedSchema = mergeSchema(mergedSchema, allOfSchema);
}
delete mergedSchema.allOf;
return mergedSchema;
return node.next(mergedSchema);
}
/**
* @attention: subschemas have to be resolved upfront (e.g. if-else that do not apply)
Expand Down Expand Up @@ -55,14 +56,14 @@ export function mergeAllOfSchema(draft, schema) {
* validate allOf definition for given input data
*/
const validateAllOf = (node, value) => {
const { draft, schema, pointer } = node;
const { draft, schema } = node;
const { allOf } = schema;
if (!Array.isArray(allOf) || allOf.length === 0) {
return;
}
const errors = [];
schema.allOf.forEach((subSchema) => {
errors.push(...draft.validate(value, subSchema, pointer));
errors.push(...draft.validate(node.next(subSchema), value));
});
return errors;
};
Expand Down
22 changes: 12 additions & 10 deletions dist/module/lib/features/anyOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
* @draft-04
*/
import { mergeSchema } from "../mergeSchema";
import { createNode } from "../types";
import { omit } from "../utils/omit";
/**
* returns merged schema of all valid anyOf subschemas for the given input data.
* Does not merge with rest input schema.
*
* @returns merged anyOf subschemas which are valid to the given input data.
*/
export function mergeValidAnyOfSchema(draft, schema, data) {
export function mergeValidAnyOfSchema(node, data) {
const { draft, schema } = node;
if (!Array.isArray(schema.anyOf) || schema.anyOf.length === 0) {
return;
}
let resolvedSchema;
schema.anyOf.forEach((anySchema) => {
const node = createNode(draft, anySchema);
anySchema = draft.resolveRef(node).schema;
if (draft.isValid(data, anySchema)) {
resolvedSchema = resolvedSchema ? mergeSchema(resolvedSchema, anySchema) : anySchema;
const anyNode = draft.resolveRef(node.next(anySchema));
if (draft.validate(anyNode, data).length === 0) {
resolvedSchema = resolvedSchema ? mergeSchema(resolvedSchema, anyNode.schema) : anyNode.schema;
}
});
return resolvedSchema;
if (resolvedSchema) {
return node.next(resolvedSchema);
}
}
/**
* @unused this function is only exposed via draft and not used otherwise
Expand All @@ -34,12 +35,13 @@ export function resolveAnyOf(node, data) {
if (!Array.isArray(anyOf) || anyOf.length === 0) {
return node;
}
const resolvedSchema = mergeValidAnyOfSchema(node.draft, node.schema, data);
if (resolvedSchema == null) {
const resolvedNode = mergeValidAnyOfSchema(node, data);
if (resolvedNode) {
const { pointer, schema } = node;
return node.draft.errors.anyOfError({ pointer, schema, value: data, anyOf: JSON.stringify(anyOf) });
}
const mergedSchema = mergeSchema(node.schema, resolvedSchema);
// node.merge(resolvedNode.schema, "anyOf")
const mergedSchema = mergeSchema(node.schema, resolvedNode.schema);
return node.next(omit(mergedSchema, "anyOf"));
}
/**
Expand Down
6 changes: 3 additions & 3 deletions dist/module/lib/features/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export function resolveIfSchema(node, data) {
* @returns validation result of it-then-else schema
*/
const validateIf = (node, value) => {
const resolvedSchema = resolveIfSchema(node, value);
if (resolvedSchema) {
const resolvedNode = resolveIfSchema(node, value);
if (resolvedNode) {
// @recursiveRef ok, we not just add per pointer, but any evlauation to dynamic scope / validation path
return node.draft.validate(resolvedSchema, value);
return node.draft.validate(resolvedNode, value);
}
};
export { validateIf };
Loading

0 comments on commit adb5f99

Please sign in to comment.