json-schema-library 5.2.1 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +1 -0
- package/.prettierrc +7 -0
- package/README.md +9 -8
- package/dist/index.d.ts +13 -44
- package/dist/jsonSchemaLibrary.js +1 -1
- package/dist/lib/addValidator.d.ts +2 -1
- package/dist/lib/compile/index.d.ts +11 -0
- package/dist/lib/config/strings.d.ts +1 -39
- package/dist/lib/cores/CoreInterface.d.ts +28 -9
- package/dist/lib/cores/Draft04.d.ts +2 -2
- package/dist/lib/cores/Draft06.d.ts +15 -0
- package/dist/lib/cores/Draft07.d.ts +15 -0
- package/dist/lib/cores/JsonEditor.d.ts +2 -2
- package/dist/lib/draft06/addSchema.d.ts +7 -0
- package/dist/lib/draft06/compile/index.d.ts +15 -0
- package/dist/lib/draft06/validation/keyword.d.ts +3 -0
- package/dist/lib/draft06/validation/type.d.ts +17 -0
- package/dist/lib/draft06/validation/typeKeywordMapping.d.ts +13 -0
- package/dist/lib/getChildSchemaSelection.d.ts +7 -5
- package/dist/lib/getSchema.d.ts +1 -1
- package/dist/lib/getTemplate.d.ts +5 -1
- package/dist/lib/getTypeOf.d.ts +2 -1
- package/dist/lib/schema/getTypeId.d.ts +1 -1
- package/dist/lib/step.d.ts +4 -4
- package/dist/lib/types.d.ts +10 -2
- package/dist/lib/utils/filter.d.ts +0 -1
- package/dist/lib/utils/merge.d.ts +3 -0
- package/dist/lib/validate.d.ts +1 -1
- package/dist/lib/validation/format.d.ts +6 -0
- package/dist/lib/validation/keyword.d.ts +2 -27
- package/dist/lib/validation/type.d.ts +3 -10
- package/dist/lib/validation/typeKeywordMapping.d.ts +4 -4
- package/dist/module/index.js +13 -6
- package/dist/module/lib/addValidator.js +3 -4
- package/dist/module/lib/compile/getRef.js +1 -1
- package/dist/module/lib/compile/index.js +11 -0
- package/dist/module/lib/config/strings.js +15 -2
- package/dist/module/lib/cores/CoreInterface.js +22 -0
- package/dist/module/lib/cores/Draft06.js +61 -0
- package/dist/module/lib/cores/Draft07.js +61 -0
- package/dist/module/lib/createSchemaOf.js +1 -1
- package/dist/module/lib/draft06/addSchema.js +11 -0
- package/dist/module/lib/draft06/compile/index.js +65 -0
- package/dist/module/lib/draft06/validation/keyword.js +156 -0
- package/dist/module/lib/draft06/validation/type.js +30 -0
- package/dist/module/lib/draft06/validation/typeKeywordMapping.js +15 -0
- package/dist/module/lib/each.js +1 -1
- package/dist/module/lib/eachSchema.js +3 -3
- package/dist/module/lib/getChildSchemaSelection.js +7 -6
- package/dist/module/lib/getSchema.js +2 -1
- package/dist/module/lib/getTemplate.js +57 -23
- package/dist/module/lib/resolveAllOf.js +3 -4
- package/dist/module/lib/resolveOneOf.fuzzy.js +13 -3
- package/dist/module/lib/resolveOneOf.strict.js +11 -2
- package/dist/module/lib/resolveRef.strict.js +8 -0
- package/dist/module/lib/schema/getTypeDefs.js +12 -1
- package/dist/module/lib/schema/getTypeId.js +1 -1
- package/dist/module/lib/step.js +62 -11
- package/dist/module/lib/types.js +7 -1
- package/dist/module/lib/utils/filter.js +3 -5
- package/dist/module/lib/utils/merge.js +3 -0
- package/dist/module/lib/validate.js +33 -8
- package/dist/module/lib/validateAsync.js +7 -7
- package/dist/module/lib/validation/errors.js +15 -2
- package/dist/module/lib/validation/format.js +105 -4
- package/dist/module/lib/validation/keyword.js +77 -30
- package/dist/module/lib/validation/type.js +2 -1
- package/dist/module/remotes/draft06.json +155 -0
- package/dist/module/remotes/draft07.json +172 -0
- package/dist/module/remotes/index.js +0 -1
- package/dist/remotes/index.d.ts +0 -1
- package/index.ts +14 -5
- package/lib/addValidator.ts +5 -5
- package/lib/compile/getRef.ts +1 -1
- package/lib/compile/index.ts +11 -1
- package/lib/config/strings.ts +17 -3
- package/lib/cores/CoreInterface.ts +37 -10
- package/lib/cores/Draft04.ts +2 -4
- package/lib/cores/Draft06.ts +76 -0
- package/lib/cores/Draft07.ts +75 -0
- package/lib/cores/JsonEditor.ts +2 -4
- package/lib/createSchemaOf.ts +1 -3
- package/lib/draft06/addSchema.ts +14 -0
- package/lib/draft06/compile/index.ts +68 -0
- package/lib/draft06/validation/keyword.ts +177 -0
- package/lib/draft06/validation/type.ts +43 -0
- package/lib/draft06/validation/typeKeywordMapping.ts +15 -0
- package/lib/each.ts +8 -3
- package/lib/eachSchema.ts +3 -3
- package/lib/getChildSchemaSelection.ts +14 -7
- package/lib/getSchema.ts +15 -7
- package/lib/getTemplate.ts +148 -38
- package/lib/getTypeOf.ts +2 -1
- package/lib/resolveAllOf.ts +9 -5
- package/lib/resolveOneOf.fuzzy.ts +25 -8
- package/lib/resolveOneOf.strict.ts +17 -4
- package/lib/resolveRef.strict.ts +9 -0
- package/lib/schema/getTypeDefs.ts +14 -1
- package/lib/schema/getTypeId.ts +2 -2
- package/lib/step.ts +103 -22
- package/lib/types.ts +21 -4
- package/lib/utils/filter.ts +4 -6
- package/lib/utils/merge.ts +4 -0
- package/lib/validate.ts +45 -15
- package/lib/validateAsync.ts +13 -12
- package/lib/validation/errors.ts +15 -2
- package/lib/validation/format.ts +113 -4
- package/lib/validation/keyword.ts +147 -78
- package/lib/validation/type.ts +5 -1
- package/package.json +73 -63
- package/remotes/draft06.json +155 -0
- package/remotes/draft07.json +172 -0
- package/remotes/draft2019-09.json +86 -0
- package/remotes/index.ts +0 -2
- package/tsconfig.json +2 -9
|
@@ -1,40 +1,2 @@
|
|
|
1
|
-
declare const _default:
|
|
2
|
-
AdditionalItemsError: string;
|
|
3
|
-
AdditionalPropertiesError: string;
|
|
4
|
-
AnyOfError: string;
|
|
5
|
-
AllOfError: string;
|
|
6
|
-
EnumError: string;
|
|
7
|
-
FormatDateTimeError: string;
|
|
8
|
-
FormatEmailError: string;
|
|
9
|
-
FormatUrlError: string;
|
|
10
|
-
FormatUriError: string;
|
|
11
|
-
FormatHostnameError: string;
|
|
12
|
-
FormatIPV4Error: string;
|
|
13
|
-
FormatIPV6Error: string;
|
|
14
|
-
FormatRegExError: string;
|
|
15
|
-
MaximumError: string;
|
|
16
|
-
MaxItemsError: string;
|
|
17
|
-
MaxLengthError: string;
|
|
18
|
-
MaxPropertiesError: string;
|
|
19
|
-
MinimumError: string;
|
|
20
|
-
MinItemsError: string;
|
|
21
|
-
MinLengthError: string;
|
|
22
|
-
MinPropertiesError: string;
|
|
23
|
-
MissingOneOfPropertyError: string;
|
|
24
|
-
MissingDependencyError: string;
|
|
25
|
-
MultipleOfError: string;
|
|
26
|
-
MultipleOneOfError: string;
|
|
27
|
-
NoAdditionalPropertiesError: string;
|
|
28
|
-
NotError: string;
|
|
29
|
-
OneOfError: string;
|
|
30
|
-
OneOfPropertyError: string;
|
|
31
|
-
PatternError: string;
|
|
32
|
-
PatternPropertiesError: string;
|
|
33
|
-
RequiredPropertyError: string;
|
|
34
|
-
TypeError: string;
|
|
35
|
-
UndefinedValueError: string;
|
|
36
|
-
UnknownPropertyError: string;
|
|
37
|
-
UniqueItemsError: string;
|
|
38
|
-
ValueNotEmptyError: string;
|
|
39
|
-
};
|
|
1
|
+
declare const _default: Record<string, string>;
|
|
40
2
|
export default _default;
|
|
@@ -1,23 +1,42 @@
|
|
|
1
|
-
import { JSONSchema, JSONPointer, JSONError } from "../types";
|
|
1
|
+
import { JSONSchema, JSONPointer, JSONValidator, JSONTypeValidator, JSONError } from "../types";
|
|
2
|
+
import { CreateError } from "../utils/createCustomError";
|
|
2
3
|
export default class CoreInterface {
|
|
4
|
+
/** entry point of schema */
|
|
3
5
|
__rootSchema: JSONSchema;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
/** error creators by id */
|
|
7
|
+
errors: Record<string, CreateError>;
|
|
8
|
+
/** map for valid keywords of a type */
|
|
9
|
+
typeKeywords: Record<string, string[]>;
|
|
10
|
+
/** keyword validators */
|
|
11
|
+
validateKeyword: Record<string, JSONValidator>;
|
|
12
|
+
/** type validators */
|
|
13
|
+
validateType: Record<string, JSONTypeValidator>;
|
|
14
|
+
/** format validators */
|
|
15
|
+
validateFormat: Record<string, JSONValidator>;
|
|
9
16
|
constructor(schema?: JSONSchema);
|
|
10
17
|
get rootSchema(): JSONSchema;
|
|
11
18
|
set rootSchema(rootSchema: JSONSchema);
|
|
12
19
|
each(data: any, callback: any, schema?: JSONSchema, pointer?: JSONPointer): void;
|
|
13
|
-
validate(data:
|
|
20
|
+
validate(data: unknown, schema?: JSONSchema, pointer?: JSONPointer): Array<JSONError>;
|
|
14
21
|
isValid(data: any, schema?: JSONSchema, pointer?: JSONPointer): boolean;
|
|
15
22
|
resolveAnyOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema;
|
|
16
23
|
resolveAllOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema;
|
|
17
24
|
resolveRef(schema: JSONSchema): JSONSchema;
|
|
18
25
|
resolveOneOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema;
|
|
19
26
|
getSchema(pointer: JSONPointer, data: any, schema?: JSONSchema): void;
|
|
20
|
-
getTemplate(data
|
|
27
|
+
getTemplate(data?: unknown, schema?: JSONSchema): void;
|
|
21
28
|
setSchema(schema: JSONSchema): void;
|
|
22
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Returns the json-schema of the given object property or array item.
|
|
31
|
+
* e.g. it steps by one key into the data
|
|
32
|
+
* This helper determines the location of the property within the schema
|
|
33
|
+
* (additional properties, oneOf, ...) and returns the correct schema.
|
|
34
|
+
*
|
|
35
|
+
* @param key property-name or array-index
|
|
36
|
+
* @param schema json schema of current data
|
|
37
|
+
* @param data parent object or array of key
|
|
38
|
+
* @param [pointer] json pointer of parent object or array
|
|
39
|
+
* @return schema or error if failed resolving key
|
|
40
|
+
*/
|
|
41
|
+
step(key: string | number, schema: JSONSchema, data: any, pointer?: JSONPointer): JSONSchema;
|
|
23
42
|
}
|
|
@@ -5,9 +5,9 @@ export default class Draft04Core extends CoreInterface {
|
|
|
5
5
|
each(data: any, callback: any, schema?: JSONSchema, pointer?: JSONPointer): void;
|
|
6
6
|
validate(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
7
7
|
isValid(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
8
|
-
resolveOneOf(data: any, schema: JSONSchema, pointer
|
|
8
|
+
resolveOneOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
9
9
|
resolveRef(schema: JSONSchema): JSONSchema;
|
|
10
10
|
getSchema(pointer: JSONPointer, data: any, schema?: JSONSchema): JSONSchema;
|
|
11
|
-
getTemplate(data
|
|
11
|
+
getTemplate(data?: unknown, schema?: JSONSchema): any;
|
|
12
12
|
step(key: string, schema: JSONSchema, data: any, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
13
13
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import CoreInterface from "./CoreInterface";
|
|
2
|
+
import { JSONSchema, JSONPointer } from "../types";
|
|
3
|
+
export default class Draft06Core extends CoreInterface {
|
|
4
|
+
constructor(schema?: JSONSchema);
|
|
5
|
+
get rootSchema(): JSONSchema;
|
|
6
|
+
set rootSchema(rootSchema: JSONSchema);
|
|
7
|
+
each(data: any, callback: any, schema?: JSONSchema, pointer?: JSONPointer): void;
|
|
8
|
+
validate(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
9
|
+
isValid(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
10
|
+
resolveOneOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
11
|
+
resolveRef(schema: JSONSchema): JSONSchema;
|
|
12
|
+
getSchema(pointer: JSONPointer, data: any, schema?: JSONSchema): JSONSchema;
|
|
13
|
+
getTemplate(data?: unknown, schema?: JSONSchema): any;
|
|
14
|
+
step(key: string, schema: JSONSchema, data: any, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import CoreInterface from "./CoreInterface";
|
|
2
|
+
import { JSONSchema, JSONPointer } from "../types";
|
|
3
|
+
export default class Draft07Core extends CoreInterface {
|
|
4
|
+
constructor(schema?: JSONSchema);
|
|
5
|
+
get rootSchema(): JSONSchema;
|
|
6
|
+
set rootSchema(rootSchema: JSONSchema);
|
|
7
|
+
each(data: any, callback: any, schema?: JSONSchema, pointer?: JSONPointer): void;
|
|
8
|
+
validate(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
9
|
+
isValid(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
10
|
+
resolveOneOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
11
|
+
resolveRef(schema: JSONSchema): JSONSchema;
|
|
12
|
+
getSchema(pointer: JSONPointer, data: any, schema?: JSONSchema): JSONSchema;
|
|
13
|
+
getTemplate(data?: unknown, schema?: JSONSchema): any;
|
|
14
|
+
step(key: string, schema: JSONSchema, data: any, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
15
|
+
}
|
|
@@ -5,9 +5,9 @@ export default class JsonEditorCore extends CoreInterface {
|
|
|
5
5
|
each(data: any, callback: any, schema?: JSONSchema, pointer?: JSONPointer): void;
|
|
6
6
|
validate(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
7
7
|
isValid(data: any, schema?: JSONSchema, pointer?: JSONPointer): any;
|
|
8
|
-
resolveOneOf(data: any, schema: JSONSchema, pointer
|
|
8
|
+
resolveOneOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
9
9
|
resolveRef(schema: JSONSchema): JSONSchema;
|
|
10
10
|
getSchema(pointer: JSONPointer, data: any, schema?: JSONSchema): JSONSchema;
|
|
11
|
-
getTemplate(data
|
|
11
|
+
getTemplate(data?: any, schema?: JSONSchema): any;
|
|
12
12
|
step(key: string, schema: JSONSchema, data: any, pointer?: JSONPointer): JSONSchema | import("../types").JSONError;
|
|
13
13
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { JSONSchema } from "../../types";
|
|
2
|
+
/**
|
|
3
|
+
* @draft starting with _draft 06_ keyword `id` has been renamed to `$id`
|
|
4
|
+
*
|
|
5
|
+
* compiles the input root schema for $ref resolution and returns it again
|
|
6
|
+
* @attention this modifies input schema but maintains object-structure
|
|
7
|
+
*
|
|
8
|
+
* for a compiled json-schema you can call getRef on any contained schema (location of type).
|
|
9
|
+
* this resolves a $ref target to a valid schema (for a valid $ref)
|
|
10
|
+
*
|
|
11
|
+
* @param rootSchema root json-schema ($id, defs, ... ) to compile
|
|
12
|
+
* @param [force] = false force compile json-schema
|
|
13
|
+
* @return compiled json-schema
|
|
14
|
+
*/
|
|
15
|
+
export default function compile(rootSchema: JSONSchema, force?: boolean): JSONSchema;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @todo: type is also a keyword, as is properties, items, etc
|
|
3
|
+
*
|
|
4
|
+
* An instance has one of six primitive types (http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2)
|
|
5
|
+
* or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
|
|
6
|
+
* 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
|
|
7
|
+
*/
|
|
8
|
+
declare const _default: {
|
|
9
|
+
array: (core: any, schema: any, value: any, pointer: any) => any;
|
|
10
|
+
object: (core: any, schema: any, value: any, pointer: any) => any;
|
|
11
|
+
string: (core: any, schema: any, value: any, pointer: any) => any;
|
|
12
|
+
integer: (core: any, schema: any, value: any, pointer: any) => any;
|
|
13
|
+
number: (core: any, schema: any, value: any, pointer: any) => any;
|
|
14
|
+
boolean: (core: any, schema: any, value: any, pointer: any) => any;
|
|
15
|
+
null: (core: any, schema: any, value: any, pointer: any) => any;
|
|
16
|
+
};
|
|
17
|
+
export default _default;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mapping, used in type validation to iterate over type-specific keywords to validate
|
|
3
|
+
* - overview https://epoberezkin.github.io/ajv/keywords.html
|
|
4
|
+
*/
|
|
5
|
+
declare const _default: {
|
|
6
|
+
array: string[];
|
|
7
|
+
boolean: string[];
|
|
8
|
+
object: string[];
|
|
9
|
+
string: string[];
|
|
10
|
+
number: string[];
|
|
11
|
+
null: string[];
|
|
12
|
+
};
|
|
13
|
+
export default _default;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import CoreInterface from "./cores/CoreInterface";
|
|
2
|
+
import { JSONError, JSONSchema } from "./types";
|
|
1
3
|
/**
|
|
2
4
|
* Returns a list of possible child-schemas for the given property key. In case of a oneOf selection, multiple schemas
|
|
3
5
|
* could be added at the given property (e.g. item-index), thus an array of options is returned. In all other cases
|
|
4
6
|
* a list with a single item will be returned
|
|
5
7
|
*
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
* @return
|
|
8
|
+
* @param core - core to use
|
|
9
|
+
* @param property - parent schema of following property
|
|
10
|
+
* @param [schema] - parent schema of following property
|
|
11
|
+
* @return
|
|
10
12
|
*/
|
|
11
|
-
export default function getChildSchemaSelection(core:
|
|
13
|
+
export default function getChildSchemaSelection(core: CoreInterface, property: string | number, schema?: JSONSchema): JSONSchema[] | JSONError;
|
package/dist/lib/getSchema.d.ts
CHANGED
|
@@ -13,4 +13,4 @@ import Core from "./cores/CoreInterface";
|
|
|
13
13
|
* @param [schema] - the json schema to iterate. Defaults to core.rootSchema
|
|
14
14
|
* @return json schema object of the json-pointer or an error
|
|
15
15
|
*/
|
|
16
|
-
export default function getSchema(core: Core, pointer: JSONPointer, data?:
|
|
16
|
+
export default function getSchema(core: Core, pointer: JSONPointer, data?: unknown, schema?: JSONSchema): JSONSchema;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { JSONSchema } from "./types";
|
|
2
2
|
import Core from "./cores/CoreInterface";
|
|
3
|
-
|
|
3
|
+
interface TemplateOptions {
|
|
4
|
+
/** Add all properties (required and optional) to the generated data */
|
|
5
|
+
addOptionalProps: boolean;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: (core: Core, data?: any, schema?: JSONSchema, opts?: TemplateOptions) => any;
|
|
4
8
|
export default _default;
|
package/dist/lib/getTypeOf.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare type JSType = "array" | "bigint" | "boolean" | "function" | "null" | "number" | "object" | "string" | "symbol" | "undefined";
|
|
2
|
+
export default function getTypeOf(value: unknown): JSType;
|
package/dist/lib/step.d.ts
CHANGED
|
@@ -7,11 +7,11 @@ import Core from "./cores/CoreInterface";
|
|
|
7
7
|
* This helper determines the location of the property within the schema (additional properties, oneOf, ...) and
|
|
8
8
|
* returns the correct schema.
|
|
9
9
|
*
|
|
10
|
-
* @param core
|
|
11
|
-
* @param key
|
|
12
|
-
* @param schema
|
|
10
|
+
* @param core - validator
|
|
11
|
+
* @param key - property-name or array-index
|
|
12
|
+
* @param schema - json schema of current data
|
|
13
13
|
* @param data - parent of key
|
|
14
|
-
* @param [pointer]
|
|
14
|
+
* @param [pointer] - pointer to schema and data (parent of key)
|
|
15
15
|
* @return Schema or Error if failed resolving key
|
|
16
16
|
*/
|
|
17
17
|
export default function step(core: Core, key: string | number, schema: JSONSchema, data?: any, pointer?: JSONPointer): JSONSchema | JSONError;
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare type JSONSchema = {
|
|
|
4
4
|
};
|
|
5
5
|
export declare type JSONPointer = string;
|
|
6
6
|
export declare type JSONError = {
|
|
7
|
-
type: "error"
|
|
7
|
+
type: "error";
|
|
8
8
|
name: string;
|
|
9
9
|
code: string;
|
|
10
10
|
message: string;
|
|
@@ -13,6 +13,14 @@ export declare type JSONError = {
|
|
|
13
13
|
};
|
|
14
14
|
[p: string]: any;
|
|
15
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* ts type guard for json error
|
|
18
|
+
* @returns true if passed type is a JSONError
|
|
19
|
+
*/
|
|
20
|
+
export declare function isJSONError(error: any): error is JSONError;
|
|
16
21
|
export interface JSONValidator {
|
|
17
|
-
(core: Core, schema: JSONSchema, value: any, pointer: JSONPointer): void | undefined | JSONError |
|
|
22
|
+
(core: Core, schema: JSONSchema, value: any, pointer: JSONPointer): void | undefined | JSONError | JSONError[];
|
|
23
|
+
}
|
|
24
|
+
export interface JSONTypeValidator {
|
|
25
|
+
(core: Core, schema: JSONSchema, value: any, pointer: JSONPointer): Array<void | undefined | JSONError | JSONError[]>;
|
|
18
26
|
}
|
package/dist/lib/validate.d.ts
CHANGED
|
@@ -9,4 +9,4 @@ import Core from "./cores/CoreInterface";
|
|
|
9
9
|
* @param [pointer] - json pointer pointing to value (used for error-messages only)
|
|
10
10
|
* @return list of errors or empty
|
|
11
11
|
*/
|
|
12
|
-
export default function validate(core: Core, value:
|
|
12
|
+
export default function validate(core: Core, value: unknown, schema?: JSONSchema, pointer?: JSONPointer): Array<JSONError>;
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
|
+
date: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
2
3
|
"date-time": (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
3
4
|
email: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
4
5
|
hostname: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
5
6
|
ipv4: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
6
7
|
ipv6: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
8
|
+
"json-pointer": (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
9
|
+
"relative-json-pointer": (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
7
10
|
regex: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
11
|
+
time: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
8
12
|
uri: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
13
|
+
"uri-reference": (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
14
|
+
"uri-template": (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
9
15
|
url: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
|
|
10
16
|
};
|
|
11
17
|
export default _default;
|
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
allOf: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
4
|
-
anyOf: (core: any, schema: any, value: any, pointer: any) => any;
|
|
5
|
-
dependencies: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
6
|
-
enum: (core: any, schema: any, value: any, pointer: any) => any;
|
|
7
|
-
format: (core: any, schema: any, value: any, pointer: any) => any;
|
|
8
|
-
items: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
9
|
-
maximum: (core: any, schema: any, value: any, pointer: any) => any;
|
|
10
|
-
maxItems: (core: any, schema: any, value: any, pointer: any) => any;
|
|
11
|
-
maxLength: (core: any, schema: any, value: any, pointer: any) => any;
|
|
12
|
-
maxProperties: (core: any, schema: any, value: any, pointer: any) => any;
|
|
13
|
-
minLength: (core: any, schema: any, value: any, pointer: any) => any;
|
|
14
|
-
minimum: (core: any, schema: any, value: any, pointer: any) => any;
|
|
15
|
-
minItems: (core: any, schema: any, value: any, pointer: any) => any;
|
|
16
|
-
minProperties: (core: any, schema: any, value: any, pointer: any) => any;
|
|
17
|
-
multipleOf: (core: any, schema: any, value: any, pointer: any) => any;
|
|
18
|
-
not: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
19
|
-
oneOf: (core: any, schema: any, value: any, pointer: any) => any;
|
|
20
|
-
pattern: (core: any, schema: any, value: any, pointer: any) => any;
|
|
21
|
-
patternProperties: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
22
|
-
properties: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
23
|
-
propertiesRequired: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
24
|
-
required: (core: any, schema: any, value: any, pointer: any) => any;
|
|
25
|
-
requiredNotEmpty: (core: any, schema: any, value: any, pointer: any) => any;
|
|
26
|
-
uniqueItems: (core: any, schema: any, value: any, pointer: any) => any[];
|
|
27
|
-
};
|
|
1
|
+
import { JSONValidator } from "../types";
|
|
2
|
+
declare const KeywordValidation: Record<string, JSONValidator>;
|
|
28
3
|
export default KeywordValidation;
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
array: (core: any, schema: any, value: any, pointer: any) => any;
|
|
3
|
-
object: (core: any, schema: any, value: any, pointer: any) => any;
|
|
4
|
-
string: (core: any, schema: any, value: any, pointer: any) => any;
|
|
5
|
-
integer: (core: any, schema: any, value: any, pointer: any) => any;
|
|
6
|
-
number: (core: any, schema: any, value: any, pointer: any) => any;
|
|
7
|
-
boolean: (core: any, schema: any, value: any, pointer: any) => any;
|
|
8
|
-
null: (core: any, schema: any, value: any, pointer: any) => any;
|
|
9
|
-
};
|
|
1
|
+
import { JSONTypeValidator } from "../types";
|
|
10
2
|
/**
|
|
11
3
|
* @todo: type is also a keyword, as is properties, items, etc
|
|
12
4
|
*
|
|
@@ -14,4 +6,5 @@ declare const _default: {
|
|
|
14
6
|
* or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
|
|
15
7
|
* 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
|
|
16
8
|
*/
|
|
17
|
-
|
|
9
|
+
declare const typeValidators: Record<string, JSONTypeValidator>;
|
|
10
|
+
export default typeValidators;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mapping, used in type validation to iterate over type-specific keywords to validate
|
|
3
|
+
* - overview https://epoberezkin.github.io/ajv/keywords.html
|
|
4
|
+
*/
|
|
1
5
|
declare const _default: {
|
|
2
6
|
array: string[];
|
|
3
7
|
boolean: string[];
|
|
@@ -6,8 +10,4 @@ declare const _default: {
|
|
|
6
10
|
number: string[];
|
|
7
11
|
null: string[];
|
|
8
12
|
};
|
|
9
|
-
/**
|
|
10
|
-
* Mapping, used in type validation to iterate over type-specific keywords to validate
|
|
11
|
-
* - overview https://epoberezkin.github.io/ajv/keywords.html
|
|
12
|
-
*/
|
|
13
13
|
export default _default;
|
package/dist/module/index.js
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
|
-
import strings from "./lib/config/strings";
|
|
2
|
-
import Interface from "./lib/cores/CoreInterface";
|
|
3
|
-
import Draft04 from "./lib/cores/Draft04";
|
|
4
|
-
import JsonEditor from "./lib/cores/JsonEditor";
|
|
5
1
|
import addSchema from "./lib/addSchema";
|
|
6
2
|
import addValidator from "./lib/addValidator";
|
|
7
3
|
import compileSchema from "./lib/compileSchema";
|
|
8
4
|
import createCustomError from "./lib/utils/createCustomError";
|
|
9
5
|
import createSchemaOf from "./lib/createSchemaOf";
|
|
6
|
+
import Draft04 from "./lib/cores/Draft04";
|
|
7
|
+
import Draft06 from "./lib/cores/Draft06";
|
|
8
|
+
import Draft07 from "./lib/cores/Draft07";
|
|
10
9
|
import each from "./lib/each";
|
|
11
10
|
import eachSchema from "./lib/eachSchema";
|
|
12
11
|
import getChildSchemaSelection from "./lib/getChildSchemaSelection";
|
|
13
12
|
import getSchema from "./lib/getSchema";
|
|
14
13
|
import getTemplate from "./lib/getTemplate";
|
|
15
14
|
import getTypeOf from "./lib/getTypeOf";
|
|
15
|
+
import Interface from "./lib/cores/CoreInterface";
|
|
16
16
|
import isValid from "./lib/isValid";
|
|
17
|
+
import JsonEditor from "./lib/cores/JsonEditor";
|
|
18
|
+
import resolveOneOf from "./lib/resolveOneOf.strict";
|
|
19
|
+
import resolveOneOfFuzzy from "./lib/resolveOneOf.fuzzy";
|
|
17
20
|
import SchemaService from "./lib/SchemaService";
|
|
21
|
+
import settings from "./lib/config/settings";
|
|
18
22
|
import step from "./lib/step";
|
|
23
|
+
import strings from "./lib/config/strings";
|
|
19
24
|
import validate from "./lib/validate";
|
|
20
25
|
import validateAsync from "./lib/validateAsync";
|
|
21
|
-
import settings from "./lib/config/settings";
|
|
22
26
|
const config = { strings };
|
|
23
27
|
export { config, Interface, Draft04, // core implementing draft04 specs
|
|
28
|
+
Draft06, // core implementing draft06 specs
|
|
29
|
+
Draft07, // core implementing draft07 specs
|
|
24
30
|
JsonEditor, // adjusted core of draft04 to better support the json-editor
|
|
25
31
|
addSchema, // add a schema to be references via $ref
|
|
26
32
|
addValidator, // add validation for keyword, format, datatype and customize errors
|
|
@@ -32,7 +38,8 @@ getSchema, // get schema of datapointer
|
|
|
32
38
|
getTemplate, // create data which is valid to the given schema
|
|
33
39
|
getTypeOf, // returns the javascript datatype
|
|
34
40
|
isValid, // returns a boolean if the schema is valid
|
|
35
|
-
settings, SchemaService, step, // steps into a json-schema, returning the matching child-schema
|
|
41
|
+
resolveOneOf, resolveOneOfFuzzy, settings, SchemaService, step, // steps into a json-schema, returning the matching child-schema
|
|
36
42
|
validate, // validates data by a schema
|
|
37
43
|
validateAsync // async validation of data by a schema
|
|
38
44
|
};
|
|
45
|
+
export { isJSONError } from "./lib/types";
|
|
@@ -22,11 +22,10 @@ function addFormat(core, formatType, validationFunction) {
|
|
|
22
22
|
if (typeof validationFunction !== "function") {
|
|
23
23
|
throw new Error(`Validation function expected. Received ${typeof validationFunction}`);
|
|
24
24
|
}
|
|
25
|
-
if (core.validateFormat[formatType]
|
|
26
|
-
|
|
27
|
-
return;
|
|
25
|
+
if (core.validateFormat[formatType]) {
|
|
26
|
+
throw new Error(`A format '${formatType}' is already registered to validation`);
|
|
28
27
|
}
|
|
29
|
-
|
|
28
|
+
core.validateFormat[formatType] = validationFunction;
|
|
30
29
|
}
|
|
31
30
|
/**
|
|
32
31
|
* Adds a custom keyword validation to a specific type. May not override existing keywords.
|
|
@@ -2,7 +2,7 @@ import { get } from "gson-pointer";
|
|
|
2
2
|
import splitRef from "./splitRef";
|
|
3
3
|
import getTypeOf from "../getTypeOf";
|
|
4
4
|
const suffixes = /(#|\/)+$/g;
|
|
5
|
-
const isObject = val => (getTypeOf(val) === "object");
|
|
5
|
+
const isObject = (val) => (getTypeOf(val) === "object");
|
|
6
6
|
// 1. combined is known
|
|
7
7
|
// 2. base or pointer is known
|
|
8
8
|
// 3. base + pointer is known
|
|
@@ -8,6 +8,17 @@ const COMPILED_REF = "__ref";
|
|
|
8
8
|
const GET_REF = "getRef";
|
|
9
9
|
const GET_ROOT = "getRoot";
|
|
10
10
|
const suffixes = /(#|\/)+$/g;
|
|
11
|
+
/**
|
|
12
|
+
* compiles the input root schema for $ref resolution and returns it again
|
|
13
|
+
* @attention this modifies input schema but maintains object-structure
|
|
14
|
+
*
|
|
15
|
+
* for a compiled json-schema you can call getRef on any contained schema (location of type).
|
|
16
|
+
* this resolves a $ref target to a valid schema (for a valid $ref)
|
|
17
|
+
*
|
|
18
|
+
* @param rootSchema root json-schema ($id, defs, ... ) to compile
|
|
19
|
+
* @param [force] = false force compile json-schema
|
|
20
|
+
* @return compiled json-schema
|
|
21
|
+
*/
|
|
11
22
|
export default function compile(rootSchema, force = false) {
|
|
12
23
|
if (rootSchema[COMPILED] !== undefined) {
|
|
13
24
|
return rootSchema;
|
|
@@ -5,15 +5,28 @@ export default {
|
|
|
5
5
|
AdditionalPropertiesError: "Additional property `{{property}}` on `{{pointer}}` does not match schema `{{schema}}`",
|
|
6
6
|
AnyOfError: "Value `{{value}}` at `{{pointer}}` does not match any schema of `{{anyOf}}`",
|
|
7
7
|
AllOfError: "Value `{{value}}` at `{{pointer}}` does not match schema of `{{allOf}}`",
|
|
8
|
+
ConstError: "Expected value at `{{pointer}}` to be `{{expected}}`, but value given is `{{value}}`",
|
|
9
|
+
ContainsError: "The array at `{{pointer}}` must contain an element that matches `{{schema}}`",
|
|
10
|
+
ContainsArrayError: "The property at `{{pointer}}` must not be an array",
|
|
11
|
+
containsAnyError: "The array at `{{pointer}}` must contain at least one item",
|
|
8
12
|
EnumError: "Expected given value `{{value}}` in `{{pointer}}` to be one of `{{values}}`",
|
|
13
|
+
FormatDateError: "Value `{{value}}` at `{{pointer}}` is not a valid date",
|
|
9
14
|
FormatDateTimeError: "Value `{{value}}` at `{{pointer}}` is not a valid date-time",
|
|
10
15
|
FormatEmailError: "Value `{{value}}` at `{{pointer}}` is not a valid email",
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
FormatURLError: "Value `{{value}}` at `{{pointer}}` is not a valid url",
|
|
17
|
+
FormatURIError: "Value `{{value}}` at `{{pointer}}` is not a valid uri",
|
|
18
|
+
FormatURIReferenceError: "Value `{{value}}` at `{{pointer}}` is not a valid uri-reference",
|
|
19
|
+
FormatURITemplateError: "Value `{{value}}` at `{{pointer}}` is not a valid uri-template",
|
|
13
20
|
FormatHostnameError: "Value `{{value}}` at `{{pointer}}` is not a valid hostname",
|
|
14
21
|
FormatIPV4Error: "Value `{{value}}` at `{{pointer}}` is not a valid IPv4 address",
|
|
22
|
+
FormatIPV4LeadingZeroError: "IPv4 addresses starting with zero are invalid, since they are interpreted as octals",
|
|
15
23
|
FormatIPV6Error: "Value `{{value}}` at `{{pointer}}` is not a valid IPv6 address",
|
|
24
|
+
FormatIPV6LeadingZeroError: "IPv6 addresses starting with zero are invalid, since they are interpreted as octals",
|
|
25
|
+
FormatJSONPointerError: "Value `{{value}}` at `{{pointer}}` is not a valid json-pointer",
|
|
16
26
|
FormatRegExError: "Value `{{value}}` at `{{pointer}}` is not a valid regular expression",
|
|
27
|
+
FormatTimeError: "Value `{{value}}` at `{{pointer}}` is not a valid time",
|
|
28
|
+
InvalidDataError: "No value may be specified in `{{pointer}}`",
|
|
29
|
+
InvalidPropertyNameError: "Invalid property name `{{property}}` at `{{pointer}}`",
|
|
17
30
|
MaximumError: "Value in `{{pointer}}` is `{{length}}`, but should be `{{maximum}}` at maximum",
|
|
18
31
|
MaxItemsError: "Too many items in `{{pointer}}`, should be `{{maximum}}` at most, but got `{{length}}`",
|
|
19
32
|
MaxLengthError: "Value `{{pointer}}` should have a maximum length of `{{maxLength}}`, but got `{{length}}`.",
|
|
@@ -4,6 +4,16 @@ import resolveAllOf from "../resolveAllOf";
|
|
|
4
4
|
/* eslint no-unused-vars: 0 no-empty-function: 0 */
|
|
5
5
|
export default class CoreInterface {
|
|
6
6
|
constructor(schema) {
|
|
7
|
+
/** error creators by id */
|
|
8
|
+
this.errors = {};
|
|
9
|
+
/** map for valid keywords of a type */
|
|
10
|
+
this.typeKeywords = {};
|
|
11
|
+
/** keyword validators */
|
|
12
|
+
this.validateKeyword = {};
|
|
13
|
+
/** type validators */
|
|
14
|
+
this.validateType = {};
|
|
15
|
+
/** format validators */
|
|
16
|
+
this.validateFormat = {};
|
|
7
17
|
this.setSchema(schema);
|
|
8
18
|
}
|
|
9
19
|
get rootSchema() {
|
|
@@ -45,6 +55,18 @@ export default class CoreInterface {
|
|
|
45
55
|
setSchema(schema) {
|
|
46
56
|
this.rootSchema = schema;
|
|
47
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns the json-schema of the given object property or array item.
|
|
60
|
+
* e.g. it steps by one key into the data
|
|
61
|
+
* This helper determines the location of the property within the schema
|
|
62
|
+
* (additional properties, oneOf, ...) and returns the correct schema.
|
|
63
|
+
*
|
|
64
|
+
* @param key property-name or array-index
|
|
65
|
+
* @param schema json schema of current data
|
|
66
|
+
* @param data parent object or array of key
|
|
67
|
+
* @param [pointer] json pointer of parent object or array
|
|
68
|
+
* @return schema or error if failed resolving key
|
|
69
|
+
*/
|
|
48
70
|
step(key, schema, data, pointer = "#") {
|
|
49
71
|
throw new Error("function 'step' is not implemented");
|
|
50
72
|
}
|