@takeshape/json-schema 8.130.0 → 8.132.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.
|
@@ -13,7 +13,7 @@ export interface ValidateResult {
|
|
|
13
13
|
}
|
|
14
14
|
export declare type Validator = (data: Data, options?: Partial<ValidateParams>) => ValidateResult;
|
|
15
15
|
export declare function parseDataPath(instancePath: string): string[];
|
|
16
|
-
export declare function isInvalidPropertyRequired(
|
|
16
|
+
export declare function isInvalidPropertyRequired(topLevelSchema: SchemaObject, error: ErrorObject): boolean;
|
|
17
17
|
export declare function refToPath(ref: string): string[];
|
|
18
18
|
/**
|
|
19
19
|
* Given a schema object traverse it using a "instancePath" and return the schema at that path
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-validator.d.ts","sourceRoot":"","sources":["../../src/schema-validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,WAAW,EAAE,OAAO,EAAE,YAAY,EAAC,MAAM,KAAK,CAAC;AAC5D,OAAO,GAAG,MAAM,KAAK,CAAC;AAOtB,oBAAY,IAAI,GAAG,GAAG,CAAC;AAEvB,MAAM,WAAW,cAAc;IAC7B,aAAa,EAAE,OAAO,CAAC;IACvB,WAAW,EAAE,OAAO,CAAC;IACrB,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,OAAO,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,WAAW,EAAE,CAAC;CACvB;AAED,oBAAY,SAAS,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,KAAK,cAAc,CAAC;AAE1F,wBAAgB,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,CAE5D;AAwBD,wBAAgB,yBAAyB,CAAC,
|
|
1
|
+
{"version":3,"file":"schema-validator.d.ts","sourceRoot":"","sources":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SAAS,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,GAAG,CAmBhD;AAeD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,YAAY,GAAG,YAAY,CAmB5D;AAED,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,YAAY,GAAG,YAAY,EAAE,EACrC,WAAW,GAAE,YAAY,EAAO,EAChC,OAAO,GAAE,OAAY,GACpB,SAAS,CAqBX"}
|
package/dist/schema-validator.js
CHANGED
|
@@ -45,25 +45,25 @@ function ignoreMissing(error) {
|
|
|
45
45
|
return !(error.instancePath === '' && error.keyword === 'required');
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
function ignoreNull(error, data,
|
|
49
|
-
return !((error.keyword === 'type' || error.keyword === 'oneOf') && getData(error, data) === null && !isInvalidPropertyRequired(
|
|
48
|
+
function ignoreNull(error, data, topLevelSchema) {
|
|
49
|
+
return !((error.keyword === 'type' || error.keyword === 'oneOf') && getData(error, data) === null && !isInvalidPropertyRequired(topLevelSchema, error));
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
function getErrorFilter(params, data, schema) {
|
|
53
53
|
return error => (!params.ignoreMissing || ignoreMissing(error)) && (!params.ignoreNulls || ignoreNull(error, data, schema));
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
function isInvalidPropertyRequired(
|
|
56
|
+
function isInvalidPropertyRequired(topLevelSchema, error) {
|
|
57
57
|
const instancePath = parseDataPath(error.instancePath);
|
|
58
58
|
const parentDataPath = instancePath.slice(0, instancePath.length - 1);
|
|
59
|
-
const parentSchema = followSchemaPath(
|
|
59
|
+
const parentSchema = followSchemaPath(topLevelSchema, parentDataPath);
|
|
60
60
|
const name = getName(error.instancePath);
|
|
61
61
|
|
|
62
62
|
if (!parentSchema) {
|
|
63
63
|
throw new Error('Unexpected error cannot find parent schema');
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
return isRequired(
|
|
66
|
+
return isRequired(topLevelSchema, parentSchema, name);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
function isRequired(topLevelSchema, schema, name) {
|
|
@@ -71,7 +71,7 @@ function isRequired(topLevelSchema, schema, name) {
|
|
|
71
71
|
return schemas.some(childSchema => {
|
|
72
72
|
var _followRef, _followRef$required;
|
|
73
73
|
|
|
74
|
-
return (_followRef = followRef(topLevelSchema, childSchema)) === null || _followRef === void 0 ? void 0 : (_followRef$required = _followRef.required) === null || _followRef$required === void 0 ? void 0 : _followRef$required.
|
|
74
|
+
return (_followRef = followRef(topLevelSchema, childSchema)) === null || _followRef === void 0 ? void 0 : (_followRef$required = _followRef.required) === null || _followRef$required === void 0 ? void 0 : _followRef$required.includes(name);
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
|
|
@@ -81,11 +81,21 @@ function getName(path) {
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
function refToPath(ref) {
|
|
84
|
-
return ref.
|
|
84
|
+
return ref.substring(2).split('/');
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
function followRef(topLevelSchema, schema) {
|
|
88
|
-
|
|
88
|
+
if (schema.$ref) {
|
|
89
|
+
const referencedSchema = (0, _get.default)(topLevelSchema, refToPath(schema.$ref));
|
|
90
|
+
|
|
91
|
+
if (!referencedSchema) {
|
|
92
|
+
throw new Error(`Could not resolve ${schema.$ref}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return referencedSchema;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return schema;
|
|
89
99
|
}
|
|
90
100
|
/**
|
|
91
101
|
* Given a schema object traverse it using a "instancePath" and return the schema at that path
|
|
@@ -134,6 +144,28 @@ function isSchemaObject(schema) {
|
|
|
134
144
|
return typeof schema === 'object';
|
|
135
145
|
}
|
|
136
146
|
|
|
147
|
+
function getSchemaWithDefinitions(ajv, id) {
|
|
148
|
+
var _ajv$getSchema2;
|
|
149
|
+
|
|
150
|
+
if (id.startsWith('#')) {
|
|
151
|
+
var _ajv$getSchema;
|
|
152
|
+
|
|
153
|
+
const rootSchema = (_ajv$getSchema = ajv.getSchema('#')) === null || _ajv$getSchema === void 0 ? void 0 : _ajv$getSchema.schema;
|
|
154
|
+
|
|
155
|
+
if (isSchemaObject(rootSchema)) {
|
|
156
|
+
const path = refToPath(id);
|
|
157
|
+
const {
|
|
158
|
+
definitions
|
|
159
|
+
} = rootSchema;
|
|
160
|
+
return { ...(0, _get.default)(rootSchema, path),
|
|
161
|
+
definitions
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return (_ajv$getSchema2 = ajv.getSchema(id)) === null || _ajv$getSchema2 === void 0 ? void 0 : _ajv$getSchema2.schema;
|
|
167
|
+
}
|
|
168
|
+
|
|
137
169
|
function validate(ajv, id, data, options) {
|
|
138
170
|
const params = {
|
|
139
171
|
ignoreMissing: false,
|
|
@@ -150,9 +182,7 @@ function validate(ajv, id, data, options) {
|
|
|
150
182
|
errors = ajv.errors;
|
|
151
183
|
|
|
152
184
|
if (params.ignoreNulls || params.ignoreMissing) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const schema = (_ajv$getSchema = ajv.getSchema(id)) === null || _ajv$getSchema === void 0 ? void 0 : _ajv$getSchema.schema;
|
|
185
|
+
const schema = getSchemaWithDefinitions(ajv, id);
|
|
156
186
|
|
|
157
187
|
if (isSchemaObject(schema)) {
|
|
158
188
|
errors = errors.filter(getErrorFilter(params, data, schema));
|
package/es/schema-validator.js
CHANGED
|
@@ -17,25 +17,25 @@ function ignoreMissing(error) {
|
|
|
17
17
|
return !(error.instancePath === '' && error.keyword === 'required');
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function ignoreNull(error, data,
|
|
21
|
-
return !((error.keyword === 'type' || error.keyword === 'oneOf') && getData(error, data) === null && !isInvalidPropertyRequired(
|
|
20
|
+
function ignoreNull(error, data, topLevelSchema) {
|
|
21
|
+
return !((error.keyword === 'type' || error.keyword === 'oneOf') && getData(error, data) === null && !isInvalidPropertyRequired(topLevelSchema, error));
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
function getErrorFilter(params, data, schema) {
|
|
25
25
|
return error => (!params.ignoreMissing || ignoreMissing(error)) && (!params.ignoreNulls || ignoreNull(error, data, schema));
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export function isInvalidPropertyRequired(
|
|
28
|
+
export function isInvalidPropertyRequired(topLevelSchema, error) {
|
|
29
29
|
const instancePath = parseDataPath(error.instancePath);
|
|
30
30
|
const parentDataPath = instancePath.slice(0, instancePath.length - 1);
|
|
31
|
-
const parentSchema = followSchemaPath(
|
|
31
|
+
const parentSchema = followSchemaPath(topLevelSchema, parentDataPath);
|
|
32
32
|
const name = getName(error.instancePath);
|
|
33
33
|
|
|
34
34
|
if (!parentSchema) {
|
|
35
35
|
throw new Error('Unexpected error cannot find parent schema');
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
return isRequired(
|
|
38
|
+
return isRequired(topLevelSchema, parentSchema, name);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
function isRequired(topLevelSchema, schema, name) {
|
|
@@ -43,7 +43,7 @@ function isRequired(topLevelSchema, schema, name) {
|
|
|
43
43
|
return schemas.some(childSchema => {
|
|
44
44
|
var _followRef, _followRef$required;
|
|
45
45
|
|
|
46
|
-
return (_followRef = followRef(topLevelSchema, childSchema)) === null || _followRef === void 0 ? void 0 : (_followRef$required = _followRef.required) === null || _followRef$required === void 0 ? void 0 : _followRef$required.
|
|
46
|
+
return (_followRef = followRef(topLevelSchema, childSchema)) === null || _followRef === void 0 ? void 0 : (_followRef$required = _followRef.required) === null || _followRef$required === void 0 ? void 0 : _followRef$required.includes(name);
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
49
|
|
|
@@ -53,11 +53,21 @@ function getName(path) {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export function refToPath(ref) {
|
|
56
|
-
return ref.
|
|
56
|
+
return ref.substring(2).split('/');
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
function followRef(topLevelSchema, schema) {
|
|
60
|
-
|
|
60
|
+
if (schema.$ref) {
|
|
61
|
+
const referencedSchema = get(topLevelSchema, refToPath(schema.$ref));
|
|
62
|
+
|
|
63
|
+
if (!referencedSchema) {
|
|
64
|
+
throw new Error(`Could not resolve ${schema.$ref}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return referencedSchema;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return schema;
|
|
61
71
|
}
|
|
62
72
|
/**
|
|
63
73
|
* Given a schema object traverse it using a "instancePath" and return the schema at that path
|
|
@@ -106,6 +116,28 @@ function isSchemaObject(schema) {
|
|
|
106
116
|
return typeof schema === 'object';
|
|
107
117
|
}
|
|
108
118
|
|
|
119
|
+
function getSchemaWithDefinitions(ajv, id) {
|
|
120
|
+
var _ajv$getSchema2;
|
|
121
|
+
|
|
122
|
+
if (id.startsWith('#')) {
|
|
123
|
+
var _ajv$getSchema;
|
|
124
|
+
|
|
125
|
+
const rootSchema = (_ajv$getSchema = ajv.getSchema('#')) === null || _ajv$getSchema === void 0 ? void 0 : _ajv$getSchema.schema;
|
|
126
|
+
|
|
127
|
+
if (isSchemaObject(rootSchema)) {
|
|
128
|
+
const path = refToPath(id);
|
|
129
|
+
const {
|
|
130
|
+
definitions
|
|
131
|
+
} = rootSchema;
|
|
132
|
+
return { ...get(rootSchema, path),
|
|
133
|
+
definitions
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return (_ajv$getSchema2 = ajv.getSchema(id)) === null || _ajv$getSchema2 === void 0 ? void 0 : _ajv$getSchema2.schema;
|
|
139
|
+
}
|
|
140
|
+
|
|
109
141
|
export function validate(ajv, id, data, options) {
|
|
110
142
|
const params = {
|
|
111
143
|
ignoreMissing: false,
|
|
@@ -122,9 +154,7 @@ export function validate(ajv, id, data, options) {
|
|
|
122
154
|
errors = ajv.errors;
|
|
123
155
|
|
|
124
156
|
if (params.ignoreNulls || params.ignoreMissing) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const schema = (_ajv$getSchema = ajv.getSchema(id)) === null || _ajv$getSchema === void 0 ? void 0 : _ajv$getSchema.schema;
|
|
157
|
+
const schema = getSchemaWithDefinitions(ajv, id);
|
|
128
158
|
|
|
129
159
|
if (isSchemaObject(schema)) {
|
|
130
160
|
errors = errors.filter(getErrorFilter(params, data, schema));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@takeshape/json-schema",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.132.0",
|
|
4
4
|
"description": "JSON Schema validator",
|
|
5
5
|
"homepage": "https://www.takeshape.io",
|
|
6
6
|
"repository": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"es"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@takeshape/util": "8.
|
|
23
|
+
"@takeshape/util": "8.132.0",
|
|
24
24
|
"ajv": "^8.11.0",
|
|
25
25
|
"ajv-formats": "^2.1.1",
|
|
26
26
|
"lodash": "^4.17.21"
|