z-schema 7.0.0 → 7.0.6
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/README.md +7 -11
- package/bin/z-schema +0 -0
- package/cjs/index.d.ts +192 -117
- package/cjs/index.js +949 -998
- package/{src/FormatValidators.ts → dist/format-validators.js} +97 -65
- package/dist/index.js +1 -1
- package/dist/json-schema.js +40 -0
- package/dist/{JsonValidation.js → json-validation.js} +75 -69
- package/dist/{Report.js → report.js} +35 -45
- package/dist/schema-cache.js +109 -0
- package/dist/schema-compiler.js +255 -0
- package/dist/{SchemaValidation.js → schema-validator.js} +153 -149
- package/dist/types/{Errors.d.ts → errors.d.ts} +2 -0
- package/dist/types/format-validators.d.ts +10 -0
- package/dist/types/index.d.ts +10 -1
- package/dist/types/json-schema.d.ts +50 -0
- package/dist/types/json-validation.d.ts +7 -0
- package/dist/types/{Report.d.ts → report.d.ts} +22 -23
- package/dist/types/schema-cache.d.ts +17 -0
- package/dist/types/schema-compiler.d.ts +16 -0
- package/dist/types/schema-validator.d.ts +10 -0
- package/dist/types/utils/array.d.ts +2 -0
- package/dist/types/utils/clone.d.ts +2 -0
- package/dist/types/utils/json.d.ts +7 -0
- package/dist/types/utils/symbols.d.ts +2 -0
- package/dist/types/utils/unicode.d.ts +14 -0
- package/dist/types/utils/uri.d.ts +4 -0
- package/dist/types/utils/what-is.d.ts +3 -0
- package/dist/types/z-schema.d.ts +75 -0
- package/dist/utils/array.js +27 -0
- package/dist/utils/clone.js +61 -0
- package/dist/utils/json.js +59 -0
- package/dist/utils/symbols.js +2 -0
- package/dist/utils/unicode.js +45 -0
- package/dist/utils/uri.js +15 -0
- package/dist/utils/what-is.js +29 -0
- package/dist/{ZSchema.js → z-schema.js} +66 -77
- package/package.json +8 -4
- package/src/{Errors.ts → errors.ts} +4 -0
- package/src/format-validators.ts +191 -0
- package/src/index.ts +12 -1
- package/src/json-schema.ts +97 -0
- package/src/{JsonValidation.ts → json-validation.ts} +137 -127
- package/src/{Report.ts → report.ts} +60 -70
- package/src/schema-cache.ts +122 -0
- package/src/schema-compiler.ts +300 -0
- package/src/{SchemaValidation.ts → schema-validator.ts} +213 -215
- package/src/utils/array.ts +29 -0
- package/src/utils/clone.ts +63 -0
- package/src/utils/json.ts +74 -0
- package/src/utils/symbols.ts +3 -0
- package/src/utils/unicode.ts +43 -0
- package/src/utils/uri.ts +18 -0
- package/src/utils/what-is.ts +46 -0
- package/src/{ZSchema.ts → z-schema.ts} +108 -113
- package/umd/ZSchema.js +949 -998
- package/umd/ZSchema.min.js +1 -1
- package/dist/FormatValidators.js +0 -136
- package/dist/SchemaCache.js +0 -173
- package/dist/SchemaCompilation.js +0 -259
- package/dist/Utils.js +0 -266
- package/dist/types/FormatValidators.d.ts +0 -12
- package/dist/types/JsonValidation.d.ts +0 -37
- package/dist/types/SchemaCache.d.ts +0 -26
- package/dist/types/SchemaCompilation.d.ts +0 -1
- package/dist/types/SchemaValidation.d.ts +0 -6
- package/dist/types/Utils.d.ts +0 -64
- package/dist/types/ZSchema.d.ts +0 -97
- package/src/SchemaCache.ts +0 -189
- package/src/SchemaCompilation.ts +0 -293
- package/src/Utils.ts +0 -286
- /package/dist/{Errors.js → errors.js} +0 -0
- /package/dist/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/dist/schemas/{schema.json → draft-04-schema.json} +0 -0
- /package/src/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/src/schemas/{schema.json → draft-04-schema.json} +0 -0
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
import validator from 'validator';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { sortedKeys } from './utils/json.js';
|
|
3
|
+
const { isEmail, isIP, isURL } = validator;
|
|
4
|
+
const dateValidator = (date) => {
|
|
5
5
|
if (typeof date !== 'string') {
|
|
6
|
-
|
|
6
|
+
return true;
|
|
7
7
|
}
|
|
8
8
|
// full-date from http://tools.ietf.org/html/rfc3339#section-5.6
|
|
9
9
|
const matches = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/.exec(date);
|
|
10
10
|
if (matches === null) {
|
|
11
|
-
|
|
11
|
+
return false;
|
|
12
12
|
}
|
|
13
13
|
// var year = matches[1];
|
|
14
14
|
// var month = matches[2];
|
|
15
15
|
// var day = matches[3];
|
|
16
16
|
if (matches[2] < '01' || matches[2] > '12' || matches[3] < '01' || matches[3] > '31') {
|
|
17
|
-
|
|
17
|
+
return false;
|
|
18
18
|
}
|
|
19
19
|
return true;
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
};
|
|
21
|
+
const dateTimeValidator = (dateTime) => {
|
|
22
22
|
if (typeof dateTime !== 'string') {
|
|
23
|
-
|
|
23
|
+
return true;
|
|
24
24
|
}
|
|
25
25
|
// date-time from http://tools.ietf.org/html/rfc3339#section-5.6
|
|
26
26
|
const s = dateTime.toLowerCase().split('t');
|
|
27
|
-
if (!
|
|
28
|
-
|
|
27
|
+
if (!dateValidator(s[0])) {
|
|
28
|
+
return false;
|
|
29
29
|
}
|
|
30
30
|
const matches = /^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}:[0-9]{2}))$/.exec(s[1]);
|
|
31
31
|
if (matches === null) {
|
|
32
|
-
|
|
32
|
+
return false;
|
|
33
33
|
}
|
|
34
34
|
// var hour = matches[1];
|
|
35
35
|
// var minute = matches[2];
|
|
@@ -37,100 +37,132 @@ export const FormatValidators = {
|
|
|
37
37
|
// var fraction = matches[4];
|
|
38
38
|
// var timezone = matches[5];
|
|
39
39
|
if (matches[1] > '23' || matches[2] > '59' || matches[3] > '59') {
|
|
40
|
-
|
|
40
|
+
return false;
|
|
41
41
|
}
|
|
42
42
|
return true;
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
};
|
|
44
|
+
const emailValidator = (email) => {
|
|
45
45
|
if (typeof email !== 'string') {
|
|
46
|
-
|
|
46
|
+
return true;
|
|
47
47
|
}
|
|
48
|
-
return
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
return isEmail(email, { require_tld: true });
|
|
49
|
+
};
|
|
50
|
+
const hostnameValidator = (hostname) => {
|
|
51
51
|
if (typeof hostname !== 'string') {
|
|
52
|
-
|
|
52
|
+
return true;
|
|
53
53
|
}
|
|
54
54
|
/*
|
|
55
55
|
http://json-schema.org/latest/json-schema-validation.html#anchor114
|
|
56
56
|
A string instance is valid against this attribute if it is a valid
|
|
57
57
|
representation for an Internet host name, as defined by RFC 1034, section 3.1 [RFC1034].
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
http://tools.ietf.org/html/rfc1034#section-3.5
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
<digit> ::= any one of the ten digits 0 through 9
|
|
62
62
|
var digit = /[0-9]/;
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
|
|
65
65
|
var letter = /[a-zA-Z]/;
|
|
66
|
-
|
|
66
|
+
|
|
67
67
|
<let-dig> ::= <letter> | <digit>
|
|
68
68
|
var letDig = /[0-9a-zA-Z]/;
|
|
69
|
-
|
|
69
|
+
|
|
70
70
|
<let-dig-hyp> ::= <let-dig> | "-"
|
|
71
71
|
var letDigHyp = /[-0-9a-zA-Z]/;
|
|
72
|
-
|
|
72
|
+
|
|
73
73
|
<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
|
|
74
74
|
var ldhStr = /[-0-9a-zA-Z]+/;
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
|
|
77
77
|
var label = /[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?/;
|
|
78
|
-
|
|
78
|
+
|
|
79
79
|
<subdomain> ::= <label> | <subdomain> "." <label>
|
|
80
80
|
var subdomain = /^[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?(\.[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?)*$/;
|
|
81
|
-
|
|
81
|
+
|
|
82
82
|
<domain> ::= <subdomain> | " "
|
|
83
83
|
var domain = null;
|
|
84
84
|
*/
|
|
85
85
|
const valid = /^[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?(\.[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?)*$/.test(hostname);
|
|
86
86
|
if (valid) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
87
|
+
// the sum of all label octets and label lengths is limited to 255.
|
|
88
|
+
if (hostname.length > 255) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
// Each node has a label, which is zero to 63 octets in length
|
|
92
|
+
const labels = hostname.split('.');
|
|
93
|
+
for (let i = 0; i < labels.length; i++) {
|
|
94
|
+
if (labels[i].length > 63) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
96
97
|
}
|
|
97
|
-
}
|
|
98
98
|
}
|
|
99
99
|
return valid;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return FormatValidators.hostname.call(this, hostname);
|
|
103
|
-
},
|
|
104
|
-
ipv4: function (ipv4) {
|
|
100
|
+
};
|
|
101
|
+
const ipv4Validator = (ipv4) => {
|
|
105
102
|
if (typeof ipv4 !== 'string') {
|
|
106
|
-
|
|
103
|
+
return true;
|
|
107
104
|
}
|
|
108
|
-
return
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
return isIP(ipv4, 4);
|
|
106
|
+
};
|
|
107
|
+
const ipv6Validator = (ipv6) => {
|
|
111
108
|
if (typeof ipv6 !== 'string') {
|
|
112
|
-
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return isIP(ipv6, 6);
|
|
112
|
+
};
|
|
113
|
+
const regexValidator = (input) => {
|
|
114
|
+
if (typeof input !== 'string') {
|
|
115
|
+
return false;
|
|
113
116
|
}
|
|
114
|
-
return validator.isIP(ipv6, 6);
|
|
115
|
-
},
|
|
116
|
-
regex: function (str) {
|
|
117
117
|
try {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} catch (_e) {
|
|
121
|
-
return false;
|
|
118
|
+
RegExp(input);
|
|
119
|
+
return true;
|
|
122
120
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (this.options.strictUris) {
|
|
126
|
-
return FormatValidators['strict-uri'].apply(this, args);
|
|
121
|
+
catch (_e) {
|
|
122
|
+
return false;
|
|
127
123
|
}
|
|
128
|
-
|
|
124
|
+
};
|
|
125
|
+
const strictUriValidator = (uri) => typeof uri !== 'string' || isURL(uri);
|
|
126
|
+
const uriValidator = function (uri) {
|
|
129
127
|
// https://github.com/zaggino/z-schema/issues/18
|
|
130
128
|
// RegExp from http://tools.ietf.org/html/rfc3986#appendix-B
|
|
131
129
|
return typeof uri !== 'string' || RegExp('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?').test(uri);
|
|
132
|
-
},
|
|
133
|
-
'strict-uri': function (uri) {
|
|
134
|
-
return typeof uri !== 'string' || validator.isURL(uri);
|
|
135
|
-
},
|
|
136
130
|
};
|
|
131
|
+
const inbuiltValidators = {
|
|
132
|
+
date: dateValidator,
|
|
133
|
+
'date-time': dateTimeValidator,
|
|
134
|
+
email: emailValidator,
|
|
135
|
+
hostname: hostnameValidator,
|
|
136
|
+
'host-name': hostnameValidator,
|
|
137
|
+
ipv4: ipv4Validator,
|
|
138
|
+
ipv6: ipv6Validator,
|
|
139
|
+
regex: regexValidator,
|
|
140
|
+
uri: uriValidator,
|
|
141
|
+
'strict-uri': strictUriValidator,
|
|
142
|
+
};
|
|
143
|
+
const customValidators = {};
|
|
144
|
+
export function getFormatValidators(options) {
|
|
145
|
+
return {
|
|
146
|
+
...inbuiltValidators,
|
|
147
|
+
...(options?.strictUris ? { uri: strictUriValidator } : {}),
|
|
148
|
+
...customValidators,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
export function registerFormat(name, validatorFunction) {
|
|
152
|
+
customValidators[name] = validatorFunction;
|
|
153
|
+
}
|
|
154
|
+
export function unregisterFormat(name) {
|
|
155
|
+
delete customValidators[name];
|
|
156
|
+
}
|
|
157
|
+
export function getSupportedFormats() {
|
|
158
|
+
return sortedKeys({
|
|
159
|
+
...inbuiltValidators,
|
|
160
|
+
...customValidators,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
export function isFormatSupported(name) {
|
|
164
|
+
return inbuiltValidators[name] != null || customValidators[name] != null;
|
|
165
|
+
}
|
|
166
|
+
export function getRegisteredFormats() {
|
|
167
|
+
return sortedKeys(customValidators);
|
|
168
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ZSchema } from './
|
|
1
|
+
import { ZSchema } from './z-schema.js';
|
|
2
2
|
export default ZSchema;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isObject } from './utils/what-is.js';
|
|
2
|
+
export const findId = (schema, id) => {
|
|
3
|
+
// process only arrays and objects
|
|
4
|
+
if (typeof schema !== 'object' || schema === null) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
// no id means root so return itself
|
|
8
|
+
if (!id) {
|
|
9
|
+
return schema;
|
|
10
|
+
}
|
|
11
|
+
if (schema.id) {
|
|
12
|
+
if (schema.id === id || (schema.id[0] === '#' && schema.id.substring(1) === id)) {
|
|
13
|
+
return schema;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
let idx, result;
|
|
17
|
+
if (Array.isArray(schema)) {
|
|
18
|
+
idx = schema.length;
|
|
19
|
+
while (idx--) {
|
|
20
|
+
result = findId(schema[idx], id);
|
|
21
|
+
if (result) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (isObject(schema)) {
|
|
27
|
+
const keys = Object.keys(schema);
|
|
28
|
+
idx = keys.length;
|
|
29
|
+
while (idx--) {
|
|
30
|
+
const k = keys[idx];
|
|
31
|
+
if (k.indexOf('__$') === 0) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
result = findId(schema[k], id);
|
|
35
|
+
if (result) {
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|