Skip to content

Commit

Permalink
introduce: SchemaNode
Browse files Browse the repository at this point in the history
commit 93251f84b998dff94a1cd494bd174ca1497d0286
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 16:53:01 2024 +0200

    dist: update

commit 871d707bae496b59ed412e8859cd299f2a881c1e
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 16:27:59 2024 +0200

    refactor: remove Q

commit 1d2c7725896c2cdd69e315aa28e315d478f9007a
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 16:03:18 2024 +0200

    refactor: replace schema.__scope by SchemaNode.path

commit d8bf0e97db9115f275f23d2cc6e8538d9bba3ad3
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 15:52:18 2024 +0200

    update: ensure many validation methods pass through node

commit 5a1741778faff3a98d93d3f255a99cc00508eab1
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 15:26:59 2024 +0200

    change: stepFunctions to work on SchemaNode

commit 13c2984576befd061bbf1691b566581a05c465c0
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 14:43:24 2024 +0200

    change: resolveRef to work on SchemaNode

commit 87710ac99792a77f7c80799dc6d342eef7e5abbf
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 13:08:48 2024 +0200

    change: reduceSchema to use SchemaNode

commit 7c7120bb2cb57f55d021f45cc7187301c79a55f8
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 12:45:54 2024 +0200

    change: resolveDynamicSchema to use SchemaNode

commit f943cd781db653ef0b003c8eac45c75c964162c8
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 12:34:06 2024 +0200

    change: resolveDependencies to use SchemaNode

commit 13c9876592176ee6897a44aeae52a9ce026031e9
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 12:14:04 2024 +0200

    change:  resolveOneOf to use SchemaNode

commit 5bf871dceaa7814040b87e6323872f9c11dfec4f
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 10:04:57 2024 +0200

    change: resolvers to use SchemaNode

commit 5dcd477c9cb63fba7ca6505ed22f23d86732533c
Author: Sascha Goldhofer <[email protected]>
Date:   Thu Apr 11 09:33:14 2024 +0200

    change: replace resolveSchema to use SchemaNode

commit e3897c25ee73c78deca2421e49f6548cc99ed5a8
Author: Sascha Goldhofer <[email protected]>
Date:   Wed Apr 10 23:34:30 2024 +0200

    change: return type of getSchema to node

commit 8196a1daf76f55af7b9d2fa60fc1eabdaa78a3e1
Author: Sascha Goldhofer <[email protected]>
Date:   Wed Apr 10 22:18:13 2024 +0200

    fix: broken omit in mergeSchema

commit 48ced0da032c60cbe1ed702d8b463d699ef0132e
Author: Sascha Goldhofer <[email protected]>
Date:   Wed Apr 10 22:15:48 2024 +0200

    fix: introduced boolean schema validation issue

commit 60c36a4acf40e3f1ffc3c0ce19db41bf1cf7822d
Author: Sascha Goldhofer <[email protected]>
Date:   Wed Apr 10 16:10:50 2024 +0200

    change: return type of step to SchemaNode

commit e113b73ef1dafd7c6b826bb5d263aebe87f50b09
Author: Sascha Goldhofer <[email protected]>
Date:   Wed Apr 10 15:39:30 2024 +0200

    change: step to use SchemaNode

commit 820d7a1a0aa42d485d4bf84f340c14d3dc8d574e
Author: Sascha Goldhofer <[email protected]>
Date:   Wed Apr 10 13:38:31 2024 +0200

    change: validator type to use SchemaNode
  • Loading branch information
sagold committed Apr 11, 2024
1 parent 90898d6 commit 0253b5a
Show file tree
Hide file tree
Showing 98 changed files with 1,413 additions and 1,362 deletions.
2 changes: 1 addition & 1 deletion dist/jsonSchemaLibrary.js

Large diffs are not rendered by default.

43 changes: 0 additions & 43 deletions dist/lib/Q.d.ts

This file was deleted.

16 changes: 9 additions & 7 deletions dist/lib/draft/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import validate from "../validate";
import { CreateError } from "../utils/createCustomError";
import { each, EachCallback } from "../each";
import { eachSchema, EachSchemaCallback } from "../eachSchema";
import { JsonSchema, JsonPointer, JsonValidator, JsonTypeValidator, JsonError } from "../types";
import { JsonSchema, JsonPointer, JsonValidator, JsonTypeValidator, JsonError, SchemaNode } from "../types";
import { resolveAllOf } from "../features/allOf";
import { resolveAnyOf } from "../features/anyOf";
import { resolveOneOf } from "../features/oneOf";
Expand Down Expand Up @@ -80,7 +80,7 @@ export declare class Draft {
*/
each(data: any, callback: EachCallback, schema?: JsonSchema, pointer?: JsonPointer): void;
eachSchema(callback: EachSchemaCallback, schema?: JsonSchema): void;
getChildSchemaSelection(property: string | number, schema?: JsonSchema): JsonError | JsonSchema[];
getChildSchemaSelection(property: string | number, schema?: JsonSchema): JsonSchema[] | JsonError;
/**
* Returns the json-schema of a data-json-pointer.
*
Expand Down Expand Up @@ -112,11 +112,11 @@ export declare class Draft {
* @return created template data
*/
getTemplate(data?: unknown, schema?: JsonSchema, opts?: TemplateOptions): any;
isValid(data: any, schema?: JsonSchema, pointer?: JsonPointer): boolean;
resolveAnyOf(data: any, schema: JsonSchema, pointer?: JsonPointer): JsonSchema;
isValid(data: unknown, schema?: JsonSchema, pointer?: JsonPointer): boolean;
resolveAnyOf(data: any, schema: JsonSchema, pointer?: JsonPointer): SchemaNode | JsonError;
resolveAllOf(data: any, schema: JsonSchema): JsonSchema;
resolveRef(schema: JsonSchema): JsonSchema;
resolveOneOf(data: any, schema: JsonSchema, pointer?: JsonPointer): JsonSchema;
resolveRef(node: SchemaNode): SchemaNode;
resolveOneOf(data: any, schema: JsonSchema, pointer?: JsonPointer): SchemaNode | JsonError;
setSchema(schema: JsonSchema): void;
/**
* Returns the json-schema of the given object property or array item.
Expand All @@ -131,7 +131,8 @@ export declare class Draft {
* @param [pointer] - pointer to schema and data (parent of key)
* @return Schema or Error if failed resolving key
*/
step(key: string | number, schema: JsonSchema, data: any, pointer?: JsonPointer): JsonSchema;
step(node: SchemaNode, key: string | number, data: any): SchemaNode | JsonError;
step(key: string | number, schema: JsonSchema, data: any, pointer?: JsonPointer): SchemaNode | JsonError;
/**
* Validate data by a json schema
*
Expand All @@ -140,5 +141,6 @@ export declare class Draft {
* @param [pointer] - json pointer pointing to value (used for error-messages only)
* @return list of errors or empty
*/
validate(node: SchemaNode, data: unknown): JsonError[];
validate(data: unknown, schema?: JsonSchema, pointer?: JsonPointer): JsonError[];
}
4 changes: 2 additions & 2 deletions dist/lib/draft06/validation/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { JsonTypeValidator } from "../../types";
* or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
* 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
*/
declare const validationType: Record<string, JsonTypeValidator>;
export default validationType;
declare const typeValidators: Record<string, JsonTypeValidator>;
export default typeValidators;
5 changes: 2 additions & 3 deletions dist/lib/draft2019/step.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JsonSchema, JsonPointer, JsonError } from "../types";
import { Draft } from "../draft";
import { JsonError, SchemaNode } from "../types";
/**
* Returns the json-schema of the given object property or array item.
* e.g. it steps by one key into the data
Expand All @@ -14,4 +13,4 @@ import { Draft } from "../draft";
* @param [pointer] - pointer to schema and data (parent of key)
* @return Schema or Error if failed resolving key
*/
export default function step(draft: Draft, key: string | number, schema: JsonSchema, data?: any, pointer?: JsonPointer): JsonSchema | JsonError;
export default function step(node: SchemaNode, key: string | number, data?: any): SchemaNode | JsonError;
4 changes: 2 additions & 2 deletions dist/lib/features/allOf.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @draft-04
*/
import { JsonSchema, JsonValidator, JsonError } from "../types";
import { JsonSchema, JsonValidator, JsonError, SchemaNode } from "../types";
import { Draft } from "../draft";
/**
* resolves schema
* when complete this will have much duplication to step.object etc
*/
export declare function resolveSchema(draft: Draft, schemaToResolve: JsonSchema, data: unknown): JsonSchema;
export declare function resolveSchema(node: SchemaNode, data: unknown): SchemaNode | JsonError;
export declare function resolveAllOf(draft: Draft, data: any, schema?: JsonSchema): JsonSchema | JsonError;
/**
* @attention: subschemas have to be resolved upfront (e.g. if-else that do not apply)
Expand Down
8 changes: 4 additions & 4 deletions dist/lib/features/anyOf.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonSchema, JsonPointer, JsonValidator, JsonError } from "../types";
import { JsonSchema, JsonValidator, JsonError, SchemaNode } from "../types";
import { Draft } from "../draft";
/**
* returns merged schema of all valid anyOf subschemas for the given input data.
Expand All @@ -8,12 +8,12 @@ import { Draft } from "../draft";
*/
export declare function mergeValidAnyOfSchema(draft: Draft, schema: JsonSchema, data: unknown): JsonSchema;
/**
* @unused this function is only exposed via draft and not used otherwise
* @returns extended input schema with valid anyOf subschemas or JsonError if
* no anyOf schema matches input data
*/
export declare function resolveAnyOf(draft: Draft, data: any, schema?: JsonSchema, pointer?: JsonPointer): JsonSchema | JsonError;
export declare function resolveAnyOf(node: SchemaNode, data: any): SchemaNode | JsonError;
/**
* validate anyOf definition for given input data
*/
declare const validateAnyOf: JsonValidator;
export { validateAnyOf };
export declare const validateAnyOf: JsonValidator;
5 changes: 2 additions & 3 deletions dist/lib/features/dependencies.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @draft 06, 2019-09
*/
import { JsonSchema, JsonValidator } from "../types";
import { Draft } from "../draft";
import { JsonSchema, JsonValidator, SchemaNode } from "../types";
/**
* @todo add support for dependentRequired (draft 2019-09)
* returns dependencies as an object json schema. does not merge with input
Expand All @@ -11,7 +10,7 @@ import { Draft } from "../draft";
*
* @returns merged json schema defined by dependencies or undefined
*/
export declare function resolveDependencies(draft: Draft, schema: JsonSchema, data: unknown): JsonSchema | undefined;
export declare function resolveDependencies(node: SchemaNode, data: unknown): JsonSchema | undefined;
/**
* @draft 2019-09
*/
Expand Down
5 changes: 2 additions & 3 deletions dist/lib/features/if.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/**
* @draft-07
*/
import { JsonSchema, JsonValidator } from "../types";
import { Draft } from "../draft";
import { JsonError, JsonValidator, SchemaNode } from "../types";
/**
* returns if-then-else as a json schema. does not merge with input
* json schema. you probably will need to do so to correctly resolve
* references.
*
* @returns json schema defined by if-then-else or undefined
*/
export declare function resolveIfSchema(draft: Draft, schema: JsonSchema, data: unknown): JsonSchema | undefined;
export declare function resolveIfSchema(node: SchemaNode, data: unknown): SchemaNode | JsonError | undefined;
/**
* @returns validation result of it-then-else schema
*/
Expand Down
7 changes: 3 additions & 4 deletions dist/lib/features/oneOf.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Draft } from "../draft";
import { JsonSchema, JsonPointer, JsonError, JsonValidator } from "../types";
import { JsonError, JsonValidator, SchemaNode } from "../types";
/**
* Selects and returns a oneOf schema for the given data
*
Expand All @@ -9,7 +8,7 @@ import { JsonSchema, JsonPointer, JsonError, JsonValidator } from "../types";
* @param pointer - json pointer to data
* @return oneOf schema or an error
*/
export declare function resolveOneOf(draft: Draft, data: any, schema?: JsonSchema, pointer?: JsonPointer): JsonSchema | JsonError;
export declare function resolveOneOf(node: SchemaNode, data: any): SchemaNode | JsonError;
/**
* Selects and returns a oneOf schema for the given data
*
Expand All @@ -19,7 +18,7 @@ export declare function resolveOneOf(draft: Draft, data: any, schema?: JsonSchem
* @param [pointer] - json pointer to data
* @return oneOf schema or an error
*/
export declare function resolveOneOfFuzzy(draft: Draft, data: any, schema?: JsonSchema, pointer?: JsonPointer): JsonSchema | JsonError;
export declare function resolveOneOfFuzzy(node: SchemaNode, data: any): SchemaNode | JsonError;
/**
* validates oneOf definition for given input data
*/
Expand Down
4 changes: 2 additions & 2 deletions dist/lib/getSchema.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonSchema, JsonPointer } from "./types";
import { JsonSchema, JsonPointer, JsonError, SchemaNode } from "./types";
import { Draft } from "./draft";
export type GetSchemaOptions = {
pointer?: JsonPointer;
Expand Down Expand Up @@ -28,4 +28,4 @@ export type GetSchemaOptions = {
* @param [options.withSchemaWarning] - if true returns an error instead of `undefined` for valid properties missing a schema definition
* @return resolved json-schema object of requested json-pointer location or json-error
*/
export default function getSchema(draft: Draft, options?: GetSchemaOptions): JsonSchema;
export default function getSchema(draft: Draft, options?: GetSchemaOptions): SchemaNode | JsonError;
7 changes: 1 addition & 6 deletions dist/lib/mergeSchema.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { JsonSchema } from "./types";
/**
* merges to two json schema. In case of conflicts, will use overwrite first
* schema or directly return first json schema.
*/
export declare function _mergeSchema(a: JsonSchema, b: JsonSchema): JsonSchema;
export declare function mergeSchema<T extends JsonSchema>(a: T, b: T): T;
export declare function mergeSchema<T extends JsonSchema>(a: T, b: T, ...omit: string[]): T;
export declare function mergeSchema2(a: unknown, b: unknown, property?: string): unknown;
7 changes: 4 additions & 3 deletions dist/lib/reduceSchema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JsonSchema, JsonPointer } from "./types";
import { Draft } from "./draft";
import { SchemaNode } from "./types";
/**
* reduces json schema by merging dynamic constructs like if-then-else,
* dependencies, allOf, anyOf, oneOf, etc into a static json schema
Expand All @@ -8,4 +7,6 @@ import { Draft } from "./draft";
* @returns input schema reduced by dynamic schema definitions for the given
* input data
*/
export declare function reduceSchema(draft: Draft, schema: JsonSchema, data: unknown, pointer: JsonPointer): JsonSchema;
export declare function reduceSchema(node: SchemaNode, data: unknown): SchemaNode | import("./types").JsonError<import("./types").ErrorData<{
[p: string]: unknown;
}>>;
7 changes: 4 additions & 3 deletions dist/lib/resolveDynamicSchema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JsonPointer, JsonSchema } from "./types";
import { Draft } from "./draft";
import { JsonError, SchemaNode } from "./types";
import { JsonData } from "@sagold/json-pointer";
export declare function isDynamicSchema(schema: JsonData): boolean;
/**
Expand All @@ -18,4 +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(draft: Draft, schema: JsonSchema, data: unknown, pointer: JsonPointer): JsonSchema;
export declare function resolveDynamicSchema(_node: SchemaNode, data: unknown): SchemaNode | JsonError<import("./types").ErrorData<{
[p: string]: unknown;
}>>;
4 changes: 2 additions & 2 deletions dist/lib/resolveRef.merge.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonSchema } from "./types";
import { SchemaNode } from "./types";
/**
* @todo update types
* Note: JsonSchema my be false
*/
export default function resolveRefMerge(schema: JsonSchema, rootSchema: JsonSchema): JsonSchema;
export default function resolveRefMerge(node: SchemaNode): SchemaNode;
4 changes: 2 additions & 2 deletions dist/lib/resolveRef.strict.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { JsonSchema } from "./types";
export default function resolveRef(schema: JsonSchema, rootSchema: JsonSchema): JsonSchema;
import { SchemaNode } from "./types";
export default function resolveRef(node: SchemaNode): SchemaNode;
5 changes: 2 additions & 3 deletions dist/lib/step.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JsonSchema, JsonPointer, JsonError } from "./types";
import { Draft } from "./draft";
import { JsonError, SchemaNode } from "./types";
/**
* Returns the json-schema of the given object property or array item.
* e.g. it steps by one key into the data
Expand All @@ -14,4 +13,4 @@ import { Draft } from "./draft";
* @param [pointer] - pointer to schema and data (parent of key)
* @return Schema or Error if failed resolving key
*/
export default function step(draft: Draft, key: string | number, schema: JsonSchema, data?: any, pointer?: JsonPointer): JsonSchema | JsonError;
export default function step(node: SchemaNode, key: string | number, data?: any): SchemaNode | JsonError;
22 changes: 17 additions & 5 deletions dist/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export type SchemaScope = {
pointer: string;
history: JsonSchema[];
};
export type SchemaNode = JsonSchema & {
__scope: SchemaScope;
};
export type JsonPointer = string;
export type ErrorData<T extends Record<string, unknown> = {
[p: string]: unknown;
Expand All @@ -30,9 +27,24 @@ export type JsonError<T extends ErrorData = ErrorData> = {
* @returns true if passed type is a JsonError
*/
export declare function isJsonError(error: any): error is JsonError;
/**
* create next node based from current node
*/
declare function next(schema: JsonError, key?: string | number): JsonError;
declare function next(schema: JsonSchema, key?: string | number): SchemaNode;
export type SchemaNode = {
draft: Draft;
pointer: string;
schema: JsonSchema;
path: JsonSchema[];
next: typeof next;
};
export declare function isSchemaNode(value: unknown): value is SchemaNode;
export declare function createNode(draft: Draft, schema: JsonSchema, pointer?: string): SchemaNode;
export interface JsonValidator {
(draft: Draft, schema: JsonSchema, value: unknown, pointer: JsonPointer): void | undefined | JsonError | JsonError[] | JsonError[][];
(node: SchemaNode, value: unknown): void | undefined | JsonError | JsonError[] | JsonError[][];
}
export interface JsonTypeValidator {
(draft: Draft, schema: JsonSchema, value: unknown, pointer: JsonPointer): Array<void | undefined | JsonError | JsonError[] | JsonError[][]>;
(node: SchemaNode, value: unknown): Array<void | undefined | JsonError | JsonError[] | JsonError[][]>;
}
export {};
4 changes: 4 additions & 0 deletions dist/lib/utils/shallowCloneSchema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { JsonSchema } from "../types";
export declare function shallowCloneSchemaNode(node: JsonSchema): {
[x: string]: any;
};
5 changes: 2 additions & 3 deletions dist/lib/validate.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JsonSchema, JsonPointer, JsonError } from "./types";
import { Draft } from "./draft";
import { JsonError, SchemaNode } from "./types";
/**
* Validates data with json schema
*
Expand All @@ -9,4 +8,4 @@ import { Draft } from "./draft";
* @param [pointer] - json pointer pointing to value (used for error-messages only)
* @return list of errors or empty
*/
export default function validate(draft: Draft, value: unknown, schema?: JsonSchema, pointer?: JsonPointer): Array<JsonError>;
export default function validate(node: SchemaNode, value: unknown): Array<JsonError>;
5 changes: 2 additions & 3 deletions dist/lib/validation/format.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { JsonError, JsonSchema } from "../types";
import { Draft } from "../draft";
declare const formatValidators: Record<string, (draft: Draft, schema: JsonSchema, value: unknown, pointer: string) => undefined | JsonError | JsonError[]>;
import { JsonError, SchemaNode } from "../types";
declare const formatValidators: Record<string, (node: SchemaNode, value: unknown) => undefined | JsonError | JsonError[]>;
export default formatValidators;
2 changes: 1 addition & 1 deletion dist/lib/validation/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JsonTypeValidator } from "../types";
/**
* @todo: type is also a keyword, as is properties, items, etc
*
* An instance has one of six primitive types (http:https://json-schema.org/latest/json-schema-draft.html#rfc.section.4.2)
* An instance has one of six primitive types (http:https://json-schema.org/latest/json-schema-node.draft.html#rfc.section.4.2)
* or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
* 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
*/
Expand Down
Loading

0 comments on commit 0253b5a

Please sign in to comment.