z-schema 3.22.0 → 3.24.2
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/dist/ZSchema-browser-min.js +1 -1
- package/dist/ZSchema-browser-min.js.map +1 -1
- package/dist/ZSchema-browser-test.js +7553 -6180
- package/dist/ZSchema-browser.js +2378 -1140
- package/index.d.ts +166 -0
- package/package.json +5 -2
- package/src/JsonValidation.js +41 -33
- package/src/Report.js +91 -6
- package/src/SchemaCache.js +19 -0
- package/src/SchemaValidation.js +12 -0
- package/src/Utils.js +51 -2
- package/src/ZSchema.js +46 -9
package/index.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// Type definitions for z-schema v3.16.0
|
|
2
|
+
// Project: https://github.com/zaggino/z-schema
|
|
3
|
+
// Definitions by: pgonzal <https://github.com/pgonzal>
|
|
4
|
+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
5
|
+
|
|
6
|
+
declare namespace Validator {
|
|
7
|
+
export interface Options {
|
|
8
|
+
asyncTimeout?: number;
|
|
9
|
+
forceAdditional?: boolean;
|
|
10
|
+
assumeAdditional?: boolean;
|
|
11
|
+
forceItems?: boolean;
|
|
12
|
+
forceMinItems?: boolean;
|
|
13
|
+
forceMaxItems?: boolean;
|
|
14
|
+
forceMinLength?: boolean;
|
|
15
|
+
forceMaxLength?: boolean;
|
|
16
|
+
forceProperties?: boolean;
|
|
17
|
+
ignoreUnresolvableReferences?: boolean;
|
|
18
|
+
noExtraKeywords?: boolean;
|
|
19
|
+
noTypeless?: boolean;
|
|
20
|
+
noEmptyStrings?: boolean;
|
|
21
|
+
noEmptyArrays?: boolean;
|
|
22
|
+
strictUris?: boolean;
|
|
23
|
+
strictMode?: boolean;
|
|
24
|
+
reportPathAsArray?: boolean;
|
|
25
|
+
breakOnFirstError?: boolean;
|
|
26
|
+
pedanticCheck?: boolean;
|
|
27
|
+
ignoreUnknownFormats?: boolean;
|
|
28
|
+
customValidator?: (report: Report, schema: any, json: any) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SchemaError extends Error {
|
|
32
|
+
/**
|
|
33
|
+
* Implements the Error.name contract. The value is always "z-schema validation error".
|
|
34
|
+
*/
|
|
35
|
+
name: string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* An identifier indicating the type of error.
|
|
39
|
+
* Example: "JSON_OBJECT_VALIDATION_FAILED"
|
|
40
|
+
*/
|
|
41
|
+
message: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns details for each error that occurred during validation.
|
|
45
|
+
* See Options.breakOnFirstError.
|
|
46
|
+
*/
|
|
47
|
+
details: SchemaErrorDetail[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SchemaErrorDetail {
|
|
51
|
+
/**
|
|
52
|
+
* Example: "Expected type string but found type array"
|
|
53
|
+
*/
|
|
54
|
+
message: string;
|
|
55
|
+
/**
|
|
56
|
+
* An error identifier that can be used to format a custom error message.
|
|
57
|
+
* Example: "INVALID_TYPE"
|
|
58
|
+
*/
|
|
59
|
+
code: string;
|
|
60
|
+
/**
|
|
61
|
+
* Format parameters that can be used to format a custom error message.
|
|
62
|
+
* Example: ["string","array"]
|
|
63
|
+
*/
|
|
64
|
+
params: Array<string>;
|
|
65
|
+
/**
|
|
66
|
+
* A JSON path indicating the location of the error.
|
|
67
|
+
* Example: "#/projects/1"
|
|
68
|
+
*/
|
|
69
|
+
path: string;
|
|
70
|
+
/**
|
|
71
|
+
* The schema rule description, which is included for certain errors where
|
|
72
|
+
* this information is useful (e.g. to describe a constraint).
|
|
73
|
+
*/
|
|
74
|
+
description: string;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns details for sub-schemas that failed to match. For example, if the schema
|
|
78
|
+
* uses the "oneOf" constraint to accept several alternative possibilities, each
|
|
79
|
+
* alternative will have its own inner detail object explaining why it failed to match.
|
|
80
|
+
*/
|
|
81
|
+
inner: SchemaErrorDetail[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const schemaSymbol: unique symbol
|
|
85
|
+
|
|
86
|
+
export const jsonSymbol: unique symbol
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
declare class Validator {
|
|
90
|
+
/**
|
|
91
|
+
* Register a custom format.
|
|
92
|
+
*
|
|
93
|
+
* @param name - name of the custom format
|
|
94
|
+
* @param validatorFunction - custom format validator function.
|
|
95
|
+
* Returns `true` if `value` matches the custom format.
|
|
96
|
+
*/
|
|
97
|
+
public static registerFormat(formatName: string, validatorFunction: (value: any) => boolean): void;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Unregister a format.
|
|
101
|
+
*
|
|
102
|
+
* @param name - name of the custom format
|
|
103
|
+
*/
|
|
104
|
+
public static unregisterFormat(name: string): void;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get the list of all registered formats.
|
|
108
|
+
*
|
|
109
|
+
* Both the names of the burned-in formats and the custom format names are
|
|
110
|
+
* returned by this function.
|
|
111
|
+
*
|
|
112
|
+
* @returns {string[]} the list of all registered format names.
|
|
113
|
+
*/
|
|
114
|
+
public static getRegisteredFormats(): string[];
|
|
115
|
+
|
|
116
|
+
public static getDefaultOptions(): Validator.Options;
|
|
117
|
+
|
|
118
|
+
constructor(options: Validator.Options);
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param schema - JSON object representing schema
|
|
122
|
+
* @returns {boolean} true if schema is valid.
|
|
123
|
+
*/
|
|
124
|
+
validateSchema(schema: any): boolean;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param json - either a JSON string or a parsed JSON object
|
|
128
|
+
* @param schema - the JSON object representing the schema
|
|
129
|
+
* @returns true if json matches schema
|
|
130
|
+
*/
|
|
131
|
+
validate(json: any, schema: any): boolean;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @param json - either a JSON string or a parsed JSON object
|
|
135
|
+
* @param schema - the JSON object representing the schema
|
|
136
|
+
*/
|
|
137
|
+
validate(json: any, schema: any, callback: (err: any, valid: boolean) => void): void;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Returns an Error object for the most recent failed validation, or null if the validation was successful.
|
|
141
|
+
*/
|
|
142
|
+
getLastError(): Validator.SchemaError;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns the error details for the most recent validation, or undefined if the validation was successful.
|
|
146
|
+
* This is the same list as the SchemaError.details property.
|
|
147
|
+
*/
|
|
148
|
+
getLastErrors(): Validator.SchemaErrorDetail[];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Basic representation of the Report class -- just enough to support customValidator
|
|
153
|
+
*/
|
|
154
|
+
declare class Report {
|
|
155
|
+
/**
|
|
156
|
+
* @param errorCode - a string representing the code for the custom error, e.g. INVALID_VALUE_SET
|
|
157
|
+
* @param errorMessage - string with the message to be returned in the error
|
|
158
|
+
* @param params - an array of relevant params for the error, e.g. [fieldName, fieldValue]
|
|
159
|
+
* @param subReports - sub-schema involved in the error
|
|
160
|
+
* @param schemaDescription - description from the schema used in the validation
|
|
161
|
+
* Adds custom error to the errors array in the validation instance and sets valid to false if it is not already set as false
|
|
162
|
+
*/
|
|
163
|
+
addCustomError: (errorCode: string, errorMessage: string, params: string[], subReports: string, schemaDescription: string) => void;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export = Validator;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "z-schema",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.24.2",
|
|
4
4
|
"description": "JSON schema validator",
|
|
5
5
|
"homepage": "https://github.com/zaggino/z-schema",
|
|
6
6
|
"authors": [
|
|
@@ -28,8 +28,10 @@
|
|
|
28
28
|
"src",
|
|
29
29
|
"dist",
|
|
30
30
|
"LICENSE",
|
|
31
|
-
"README.md"
|
|
31
|
+
"README.md",
|
|
32
|
+
"index.d.ts"
|
|
32
33
|
],
|
|
34
|
+
"types": "index.d.ts",
|
|
33
35
|
"scripts": {
|
|
34
36
|
"prepublish": "npm test && grunt",
|
|
35
37
|
"test": "jasmine-node test/ && grunt lint"
|
|
@@ -58,6 +60,7 @@
|
|
|
58
60
|
]
|
|
59
61
|
},
|
|
60
62
|
"dependencies": {
|
|
63
|
+
"core-js": "^2.5.7",
|
|
61
64
|
"lodash.get": "^4.0.0",
|
|
62
65
|
"lodash.isequal": "^4.0.0",
|
|
63
66
|
"validator": "^10.0.0"
|
package/src/JsonValidation.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var FormatValidators
|
|
4
|
-
Report
|
|
5
|
-
Utils
|
|
3
|
+
var FormatValidators = require("./FormatValidators"),
|
|
4
|
+
Report = require("./Report"),
|
|
5
|
+
Utils = require("./Utils");
|
|
6
6
|
|
|
7
7
|
var JsonValidators = {
|
|
8
8
|
multipleOf: function (report, schema, json) {
|
|
@@ -11,7 +11,7 @@ var JsonValidators = {
|
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
13
|
if (Utils.whatIs(json / schema.multipleOf) !== "integer") {
|
|
14
|
-
report.addError("MULTIPLE_OF", [json, schema.multipleOf], null, schema
|
|
14
|
+
report.addError("MULTIPLE_OF", [json, schema.multipleOf], null, schema);
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
maximum: function (report, schema, json) {
|
|
@@ -21,11 +21,11 @@ var JsonValidators = {
|
|
|
21
21
|
}
|
|
22
22
|
if (schema.exclusiveMaximum !== true) {
|
|
23
23
|
if (json > schema.maximum) {
|
|
24
|
-
report.addError("MAXIMUM", [json, schema.maximum], null, schema
|
|
24
|
+
report.addError("MAXIMUM", [json, schema.maximum], null, schema);
|
|
25
25
|
}
|
|
26
26
|
} else {
|
|
27
27
|
if (json >= schema.maximum) {
|
|
28
|
-
report.addError("MAXIMUM_EXCLUSIVE", [json, schema.maximum], null, schema
|
|
28
|
+
report.addError("MAXIMUM_EXCLUSIVE", [json, schema.maximum], null, schema);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
},
|
|
@@ -39,11 +39,11 @@ var JsonValidators = {
|
|
|
39
39
|
}
|
|
40
40
|
if (schema.exclusiveMinimum !== true) {
|
|
41
41
|
if (json < schema.minimum) {
|
|
42
|
-
report.addError("MINIMUM", [json, schema.minimum], null, schema
|
|
42
|
+
report.addError("MINIMUM", [json, schema.minimum], null, schema);
|
|
43
43
|
}
|
|
44
44
|
} else {
|
|
45
45
|
if (json <= schema.minimum) {
|
|
46
|
-
report.addError("MINIMUM_EXCLUSIVE", [json, schema.minimum], null, schema
|
|
46
|
+
report.addError("MINIMUM_EXCLUSIVE", [json, schema.minimum], null, schema);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
},
|
|
@@ -56,7 +56,7 @@ var JsonValidators = {
|
|
|
56
56
|
return;
|
|
57
57
|
}
|
|
58
58
|
if (Utils.ucs2decode(json).length > schema.maxLength) {
|
|
59
|
-
report.addError("MAX_LENGTH", [json.length, schema.maxLength], null, schema
|
|
59
|
+
report.addError("MAX_LENGTH", [json.length, schema.maxLength], null, schema);
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
minLength: function (report, schema, json) {
|
|
@@ -65,7 +65,7 @@ var JsonValidators = {
|
|
|
65
65
|
return;
|
|
66
66
|
}
|
|
67
67
|
if (Utils.ucs2decode(json).length < schema.minLength) {
|
|
68
|
-
report.addError("MIN_LENGTH", [json.length, schema.minLength], null, schema
|
|
68
|
+
report.addError("MIN_LENGTH", [json.length, schema.minLength], null, schema);
|
|
69
69
|
}
|
|
70
70
|
},
|
|
71
71
|
pattern: function (report, schema, json) {
|
|
@@ -74,7 +74,7 @@ var JsonValidators = {
|
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
76
|
if (RegExp(schema.pattern).test(json) === false) {
|
|
77
|
-
report.addError("PATTERN", [schema.pattern, json], null, schema
|
|
77
|
+
report.addError("PATTERN", [schema.pattern, json], null, schema);
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
80
|
additionalItems: function (report, schema, json) {
|
|
@@ -86,7 +86,7 @@ var JsonValidators = {
|
|
|
86
86
|
// the json is valid if its size is less than, or equal to, the size of "items".
|
|
87
87
|
if (schema.additionalItems === false && Array.isArray(schema.items)) {
|
|
88
88
|
if (json.length > schema.items.length) {
|
|
89
|
-
report.addError("ARRAY_ADDITIONAL_ITEMS", null, null, schema
|
|
89
|
+
report.addError("ARRAY_ADDITIONAL_ITEMS", null, null, schema);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
},
|
|
@@ -99,7 +99,7 @@ var JsonValidators = {
|
|
|
99
99
|
return;
|
|
100
100
|
}
|
|
101
101
|
if (json.length > schema.maxItems) {
|
|
102
|
-
report.addError("ARRAY_LENGTH_LONG", [json.length, schema.maxItems], null, schema
|
|
102
|
+
report.addError("ARRAY_LENGTH_LONG", [json.length, schema.maxItems], null, schema);
|
|
103
103
|
}
|
|
104
104
|
},
|
|
105
105
|
minItems: function (report, schema, json) {
|
|
@@ -108,7 +108,7 @@ var JsonValidators = {
|
|
|
108
108
|
return;
|
|
109
109
|
}
|
|
110
110
|
if (json.length < schema.minItems) {
|
|
111
|
-
report.addError("ARRAY_LENGTH_SHORT", [json.length, schema.minItems], null, schema
|
|
111
|
+
report.addError("ARRAY_LENGTH_SHORT", [json.length, schema.minItems], null, schema);
|
|
112
112
|
}
|
|
113
113
|
},
|
|
114
114
|
uniqueItems: function (report, schema, json) {
|
|
@@ -119,7 +119,7 @@ var JsonValidators = {
|
|
|
119
119
|
if (schema.uniqueItems === true) {
|
|
120
120
|
var matches = [];
|
|
121
121
|
if (Utils.isUniqueArray(json, matches) === false) {
|
|
122
|
-
report.addError("ARRAY_UNIQUE", matches, null, schema
|
|
122
|
+
report.addError("ARRAY_UNIQUE", matches, null, schema);
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
},
|
|
@@ -130,7 +130,7 @@ var JsonValidators = {
|
|
|
130
130
|
}
|
|
131
131
|
var keysCount = Object.keys(json).length;
|
|
132
132
|
if (keysCount > schema.maxProperties) {
|
|
133
|
-
report.addError("OBJECT_PROPERTIES_MAXIMUM", [keysCount, schema.maxProperties], null, schema
|
|
133
|
+
report.addError("OBJECT_PROPERTIES_MAXIMUM", [keysCount, schema.maxProperties], null, schema);
|
|
134
134
|
}
|
|
135
135
|
},
|
|
136
136
|
minProperties: function (report, schema, json) {
|
|
@@ -140,7 +140,7 @@ var JsonValidators = {
|
|
|
140
140
|
}
|
|
141
141
|
var keysCount = Object.keys(json).length;
|
|
142
142
|
if (keysCount < schema.minProperties) {
|
|
143
|
-
report.addError("OBJECT_PROPERTIES_MINIMUM", [keysCount, schema.minProperties], null, schema
|
|
143
|
+
report.addError("OBJECT_PROPERTIES_MINIMUM", [keysCount, schema.minProperties], null, schema);
|
|
144
144
|
}
|
|
145
145
|
},
|
|
146
146
|
required: function (report, schema, json) {
|
|
@@ -152,7 +152,7 @@ var JsonValidators = {
|
|
|
152
152
|
while (idx--) {
|
|
153
153
|
var requiredPropertyName = schema.required[idx];
|
|
154
154
|
if (json[requiredPropertyName] === undefined) {
|
|
155
|
-
report.addError("OBJECT_MISSING_REQUIRED_PROPERTY", [requiredPropertyName], null, schema
|
|
155
|
+
report.addError("OBJECT_MISSING_REQUIRED_PROPERTY", [requiredPropertyName], null, schema);
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
},
|
|
@@ -208,7 +208,7 @@ var JsonValidators = {
|
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
if (s.length > 0) {
|
|
211
|
-
report.addError("OBJECT_ADDITIONAL_PROPERTIES", [s], null, schema
|
|
211
|
+
report.addError("OBJECT_ADDITIONAL_PROPERTIES", [s], null, schema);
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
}
|
|
@@ -236,7 +236,7 @@ var JsonValidators = {
|
|
|
236
236
|
while (idx2--) {
|
|
237
237
|
var requiredPropertyName = dependencyDefinition[idx2];
|
|
238
238
|
if (json[requiredPropertyName] === undefined) {
|
|
239
|
-
report.addError("OBJECT_DEPENDENCY_KEY", [requiredPropertyName, dependencyName], null, schema
|
|
239
|
+
report.addError("OBJECT_DEPENDENCY_KEY", [requiredPropertyName, dependencyName], null, schema);
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
}
|
|
@@ -259,7 +259,7 @@ var JsonValidators = {
|
|
|
259
259
|
|
|
260
260
|
if (match === false) {
|
|
261
261
|
var error = caseInsensitiveMatch && this.options.enumCaseInsensitiveComparison ? "ENUM_CASE_MISMATCH" : "ENUM_MISMATCH";
|
|
262
|
-
report.addError(error, [json], null, schema
|
|
262
|
+
report.addError(error, [json], null, schema);
|
|
263
263
|
}
|
|
264
264
|
},
|
|
265
265
|
type: function (report, schema, json) {
|
|
@@ -267,11 +267,11 @@ var JsonValidators = {
|
|
|
267
267
|
var jsonType = Utils.whatIs(json);
|
|
268
268
|
if (typeof schema.type === "string") {
|
|
269
269
|
if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
|
|
270
|
-
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema
|
|
270
|
+
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema);
|
|
271
271
|
}
|
|
272
272
|
} else {
|
|
273
273
|
if (schema.type.indexOf(jsonType) === -1 && (jsonType !== "integer" || schema.type.indexOf("number") === -1)) {
|
|
274
|
-
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema
|
|
274
|
+
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema);
|
|
275
275
|
}
|
|
276
276
|
}
|
|
277
277
|
},
|
|
@@ -298,7 +298,7 @@ var JsonValidators = {
|
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
if (passed === false) {
|
|
301
|
-
report.addError("ANY_OF_MISSING", undefined, subReports, schema
|
|
301
|
+
report.addError("ANY_OF_MISSING", undefined, subReports, schema);
|
|
302
302
|
}
|
|
303
303
|
},
|
|
304
304
|
oneOf: function (report, schema, json) {
|
|
@@ -316,16 +316,16 @@ var JsonValidators = {
|
|
|
316
316
|
}
|
|
317
317
|
|
|
318
318
|
if (passes === 0) {
|
|
319
|
-
report.addError("ONE_OF_MISSING", undefined, subReports, schema
|
|
319
|
+
report.addError("ONE_OF_MISSING", undefined, subReports, schema);
|
|
320
320
|
} else if (passes > 1) {
|
|
321
|
-
report.addError("ONE_OF_MULTIPLE", null, null, schema
|
|
321
|
+
report.addError("ONE_OF_MULTIPLE", null, null, schema);
|
|
322
322
|
}
|
|
323
323
|
},
|
|
324
324
|
not: function (report, schema, json) {
|
|
325
325
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.6.2
|
|
326
326
|
var subReport = new Report(report);
|
|
327
327
|
if (exports.validate.call(this, subReport, schema.not, json) === true) {
|
|
328
|
-
report.addError("NOT_PASSED", null, null, schema
|
|
328
|
+
report.addError("NOT_PASSED", null, null, schema);
|
|
329
329
|
}
|
|
330
330
|
},
|
|
331
331
|
definitions: function () { /*report, schema, json*/
|
|
@@ -340,17 +340,17 @@ var JsonValidators = {
|
|
|
340
340
|
// async
|
|
341
341
|
report.addAsyncTask(formatValidatorFn, [json], function (result) {
|
|
342
342
|
if (result !== true) {
|
|
343
|
-
report.addError("INVALID_FORMAT", [schema.format, json], null, schema
|
|
343
|
+
report.addError("INVALID_FORMAT", [schema.format, json], null, schema);
|
|
344
344
|
}
|
|
345
345
|
});
|
|
346
346
|
} else {
|
|
347
347
|
// sync
|
|
348
348
|
if (formatValidatorFn.call(this, json) !== true) {
|
|
349
|
-
report.addError("INVALID_FORMAT", [schema.format, json], null, schema
|
|
349
|
+
report.addError("INVALID_FORMAT", [schema.format, json], null, schema);
|
|
350
350
|
}
|
|
351
351
|
}
|
|
352
352
|
} else if (this.options.ignoreUnknownFormats !== true) {
|
|
353
|
-
report.addError("UNKNOWN_FORMAT", [schema.format], null, schema
|
|
353
|
+
report.addError("UNKNOWN_FORMAT", [schema.format], null, schema);
|
|
354
354
|
}
|
|
355
355
|
}
|
|
356
356
|
};
|
|
@@ -367,7 +367,7 @@ var recurseArray = function (report, schema, json) {
|
|
|
367
367
|
if (Array.isArray(schema.items)) {
|
|
368
368
|
|
|
369
369
|
while (idx--) {
|
|
370
|
-
// equal to
|
|
370
|
+
// equal to doesn't make sense here
|
|
371
371
|
if (idx < schema.items.length) {
|
|
372
372
|
report.path.push(idx.toString());
|
|
373
373
|
exports.validate.call(this, report, schema.items[idx], json[idx]);
|
|
@@ -455,6 +455,14 @@ var recurseObject = function (report, schema, json) {
|
|
|
455
455
|
}
|
|
456
456
|
};
|
|
457
457
|
|
|
458
|
+
exports.JsonValidators = JsonValidators;
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
*
|
|
462
|
+
* @param {Report} report
|
|
463
|
+
* @param {*} schema
|
|
464
|
+
* @param {*} json
|
|
465
|
+
*/
|
|
458
466
|
exports.validate = function (report, schema, json) {
|
|
459
467
|
|
|
460
468
|
report.commonErrorMessage = "JSON_OBJECT_VALIDATION_FAILED";
|
|
@@ -462,7 +470,7 @@ exports.validate = function (report, schema, json) {
|
|
|
462
470
|
// check if schema is an object
|
|
463
471
|
var to = Utils.whatIs(schema);
|
|
464
472
|
if (to !== "object") {
|
|
465
|
-
report.addError("SCHEMA_NOT_AN_OBJECT", [to], null, schema
|
|
473
|
+
report.addError("SCHEMA_NOT_AN_OBJECT", [to], null, schema);
|
|
466
474
|
return false;
|
|
467
475
|
}
|
|
468
476
|
|
|
@@ -485,7 +493,7 @@ exports.validate = function (report, schema, json) {
|
|
|
485
493
|
var maxRefs = 99;
|
|
486
494
|
while (schema.$ref && maxRefs > 0) {
|
|
487
495
|
if (!schema.__$refResolved) {
|
|
488
|
-
report.addError("REF_UNRESOLVED", [schema.$ref], null, schema
|
|
496
|
+
report.addError("REF_UNRESOLVED", [schema.$ref], null, schema);
|
|
489
497
|
break;
|
|
490
498
|
} else if (schema.__$refResolved === schema) {
|
|
491
499
|
break;
|
package/src/Report.js
CHANGED
|
@@ -4,6 +4,12 @@ var get = require("lodash.get");
|
|
|
4
4
|
var Errors = require("./Errors");
|
|
5
5
|
var Utils = require("./Utils");
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @class
|
|
9
|
+
*
|
|
10
|
+
* @param {Report|object} parentOrOptions
|
|
11
|
+
* @param {object} [reportOptions]
|
|
12
|
+
*/
|
|
7
13
|
function Report(parentOrOptions, reportOptions) {
|
|
8
14
|
this.parentReport = parentOrOptions instanceof Report ?
|
|
9
15
|
parentOrOptions :
|
|
@@ -16,10 +22,20 @@ function Report(parentOrOptions, reportOptions) {
|
|
|
16
22
|
this.reportOptions = reportOptions || {};
|
|
17
23
|
|
|
18
24
|
this.errors = [];
|
|
25
|
+
/**
|
|
26
|
+
* @type {string[]}
|
|
27
|
+
*/
|
|
19
28
|
this.path = [];
|
|
20
29
|
this.asyncTasks = [];
|
|
30
|
+
|
|
31
|
+
this.rootSchema = undefined;
|
|
32
|
+
this.commonErrorMessage = undefined;
|
|
33
|
+
this.json = undefined;
|
|
21
34
|
}
|
|
22
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
23
39
|
Report.prototype.isValid = function () {
|
|
24
40
|
if (this.asyncTasks.length > 0) {
|
|
25
41
|
throw new Error("Async tasks pending, can't answer isValid");
|
|
@@ -27,10 +43,23 @@ Report.prototype.isValid = function () {
|
|
|
27
43
|
return this.errors.length === 0;
|
|
28
44
|
};
|
|
29
45
|
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param {*} fn
|
|
49
|
+
* @param {*} args
|
|
50
|
+
* @param {*} asyncTaskResultProcessFn
|
|
51
|
+
*/
|
|
30
52
|
Report.prototype.addAsyncTask = function (fn, args, asyncTaskResultProcessFn) {
|
|
31
53
|
this.asyncTasks.push([fn, args, asyncTaskResultProcessFn]);
|
|
32
54
|
};
|
|
33
55
|
|
|
56
|
+
/**
|
|
57
|
+
*
|
|
58
|
+
* @param {*} timeout
|
|
59
|
+
* @param {function(*, *)} callback
|
|
60
|
+
*
|
|
61
|
+
* @returns {void}
|
|
62
|
+
*/
|
|
34
63
|
Report.prototype.processAsyncTasks = function (timeout, callback) {
|
|
35
64
|
|
|
36
65
|
var validationTimeout = timeout || 2000,
|
|
@@ -42,7 +71,7 @@ Report.prototype.processAsyncTasks = function (timeout, callback) {
|
|
|
42
71
|
function finish() {
|
|
43
72
|
process.nextTick(function () {
|
|
44
73
|
var valid = self.errors.length === 0,
|
|
45
|
-
err
|
|
74
|
+
err = valid ? undefined : self.errors;
|
|
46
75
|
callback(err, valid);
|
|
47
76
|
});
|
|
48
77
|
}
|
|
@@ -78,7 +107,16 @@ Report.prototype.processAsyncTasks = function (timeout, callback) {
|
|
|
78
107
|
|
|
79
108
|
};
|
|
80
109
|
|
|
110
|
+
/**
|
|
111
|
+
*
|
|
112
|
+
* @param {*} returnPathAsString
|
|
113
|
+
*
|
|
114
|
+
* @return {string[]|string}
|
|
115
|
+
*/
|
|
81
116
|
Report.prototype.getPath = function (returnPathAsString) {
|
|
117
|
+
/**
|
|
118
|
+
* @type {string[]|string}
|
|
119
|
+
*/
|
|
82
120
|
var path = [];
|
|
83
121
|
if (this.parentReport) {
|
|
84
122
|
path = path.concat(this.parentReport.path);
|
|
@@ -123,6 +161,13 @@ Report.prototype.getSchemaId = function () {
|
|
|
123
161
|
return this.rootSchema.id;
|
|
124
162
|
};
|
|
125
163
|
|
|
164
|
+
/**
|
|
165
|
+
*
|
|
166
|
+
* @param {*} errorCode
|
|
167
|
+
* @param {*} params
|
|
168
|
+
*
|
|
169
|
+
* @return {boolean}
|
|
170
|
+
*/
|
|
126
171
|
Report.prototype.hasError = function (errorCode, params) {
|
|
127
172
|
var idx = this.errors.length;
|
|
128
173
|
while (idx--) {
|
|
@@ -145,13 +190,43 @@ Report.prototype.hasError = function (errorCode, params) {
|
|
|
145
190
|
return false;
|
|
146
191
|
};
|
|
147
192
|
|
|
148
|
-
|
|
193
|
+
/**
|
|
194
|
+
*
|
|
195
|
+
* @param {*} errorCode
|
|
196
|
+
* @param {*} params
|
|
197
|
+
* @param {Report[]|Report} [subReports]
|
|
198
|
+
* @param {*} [schema]
|
|
199
|
+
*
|
|
200
|
+
* @return {void}
|
|
201
|
+
*/
|
|
202
|
+
Report.prototype.addError = function (errorCode, params, subReports, schema) {
|
|
149
203
|
if (!errorCode) { throw new Error("No errorCode passed into addError()"); }
|
|
150
204
|
|
|
151
|
-
this.addCustomError(errorCode, Errors[errorCode], params, subReports,
|
|
205
|
+
this.addCustomError(errorCode, Errors[errorCode], params, subReports, schema);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
Report.prototype.getJson = function () {
|
|
209
|
+
var self = this;
|
|
210
|
+
while (self.json === undefined) {
|
|
211
|
+
self = self.parentReport;
|
|
212
|
+
if (self === undefined) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return self.json;
|
|
152
217
|
};
|
|
153
218
|
|
|
154
|
-
|
|
219
|
+
/**
|
|
220
|
+
*
|
|
221
|
+
* @param {*} errorCode
|
|
222
|
+
* @param {*} errorMessage
|
|
223
|
+
* @param {*[]} params
|
|
224
|
+
* @param {Report[]|Report} subReports
|
|
225
|
+
* @param {*} schema
|
|
226
|
+
*
|
|
227
|
+
* @returns {void}
|
|
228
|
+
*/
|
|
229
|
+
Report.prototype.addCustomError = function (errorCode, errorMessage, params, subReports, schema) {
|
|
155
230
|
if (this.errors.length >= this.reportOptions.maxErrors) {
|
|
156
231
|
return;
|
|
157
232
|
}
|
|
@@ -175,8 +250,18 @@ Report.prototype.addCustomError = function (errorCode, errorMessage, params, sub
|
|
|
175
250
|
schemaId: this.getSchemaId()
|
|
176
251
|
};
|
|
177
252
|
|
|
178
|
-
|
|
179
|
-
|
|
253
|
+
err[Utils.schemaSymbol] = schema;
|
|
254
|
+
err[Utils.jsonSymbol] = this.getJson();
|
|
255
|
+
|
|
256
|
+
if (schema && typeof schema === "string") {
|
|
257
|
+
err.description = schema;
|
|
258
|
+
} else if (schema && typeof schema === "object") {
|
|
259
|
+
if (schema.title) {
|
|
260
|
+
err.title = schema.title;
|
|
261
|
+
}
|
|
262
|
+
if (schema.description) {
|
|
263
|
+
err.description = schema.description;
|
|
264
|
+
}
|
|
180
265
|
}
|
|
181
266
|
|
|
182
267
|
if (subReports != null) {
|
package/src/SchemaCache.js
CHANGED
|
@@ -64,6 +64,13 @@ function findId(schema, id) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
*
|
|
69
|
+
* @param {*} uri
|
|
70
|
+
* @param {*} schema
|
|
71
|
+
*
|
|
72
|
+
* @returns {void}
|
|
73
|
+
*/
|
|
67
74
|
exports.cacheSchemaByUri = function (uri, schema) {
|
|
68
75
|
var remotePath = getRemotePath(uri);
|
|
69
76
|
if (remotePath) {
|
|
@@ -71,6 +78,12 @@ exports.cacheSchemaByUri = function (uri, schema) {
|
|
|
71
78
|
}
|
|
72
79
|
};
|
|
73
80
|
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* @param {*} uri
|
|
84
|
+
*
|
|
85
|
+
* @returns {void}
|
|
86
|
+
*/
|
|
74
87
|
exports.removeFromCacheByUri = function (uri) {
|
|
75
88
|
var remotePath = getRemotePath(uri);
|
|
76
89
|
if (remotePath) {
|
|
@@ -78,6 +91,12 @@ exports.removeFromCacheByUri = function (uri) {
|
|
|
78
91
|
}
|
|
79
92
|
};
|
|
80
93
|
|
|
94
|
+
/**
|
|
95
|
+
*
|
|
96
|
+
* @param {*} uri
|
|
97
|
+
*
|
|
98
|
+
* @returns {boolean}
|
|
99
|
+
*/
|
|
81
100
|
exports.checkCacheForUri = function (uri) {
|
|
82
101
|
var remotePath = getRemotePath(uri);
|
|
83
102
|
return remotePath ? this.cache[remotePath] != null : false;
|
package/src/SchemaValidation.js
CHANGED
|
@@ -501,6 +501,13 @@ var SchemaValidators = {
|
|
|
501
501
|
}
|
|
502
502
|
};
|
|
503
503
|
|
|
504
|
+
/**
|
|
505
|
+
*
|
|
506
|
+
* @param {Report} report
|
|
507
|
+
* @param {*[]} arr
|
|
508
|
+
*
|
|
509
|
+
* @returns {boolean}
|
|
510
|
+
*/
|
|
504
511
|
var validateArrayOfSchemas = function (report, arr) {
|
|
505
512
|
var idx = arr.length;
|
|
506
513
|
while (idx--) {
|
|
@@ -509,6 +516,11 @@ var validateArrayOfSchemas = function (report, arr) {
|
|
|
509
516
|
return report.isValid();
|
|
510
517
|
};
|
|
511
518
|
|
|
519
|
+
/**
|
|
520
|
+
*
|
|
521
|
+
* @param {Report} report
|
|
522
|
+
* @param {*} schema
|
|
523
|
+
*/
|
|
512
524
|
exports.validateSchema = function (report, schema) {
|
|
513
525
|
|
|
514
526
|
report.commonErrorMessage = "SCHEMA_VALIDATION_FAILED";
|