z-schema 6.0.2 → 7.0.0-beta.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/README.md +154 -134
- package/bin/z-schema +128 -124
- package/cjs/ZSchema.d.ts +227 -0
- package/cjs/ZSchema.js +13785 -0
- package/dist/Errors.js +50 -0
- package/dist/FormatValidators.js +136 -0
- package/{src → dist}/JsonValidation.js +184 -213
- package/dist/Report.js +220 -0
- package/{src → dist}/SchemaCache.js +67 -82
- package/{src → dist}/SchemaCompilation.js +89 -129
- package/dist/SchemaValidation.js +631 -0
- package/{src → dist}/Utils.js +96 -104
- package/dist/ZSchema.js +365 -0
- package/dist/index.js +2 -0
- package/dist/schemas/hyper-schema.json +156 -0
- package/dist/schemas/schema.json +151 -0
- package/dist/types/Errors.d.ts +44 -0
- package/dist/types/FormatValidators.d.ts +12 -0
- package/dist/types/JsonValidation.d.ts +37 -0
- package/dist/types/Report.d.ts +87 -0
- package/dist/types/SchemaCache.d.ts +26 -0
- package/dist/types/SchemaCompilation.d.ts +1 -0
- package/dist/types/SchemaValidation.d.ts +6 -0
- package/dist/types/Utils.d.ts +64 -0
- package/dist/types/ZSchema.d.ts +97 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +59 -45
- package/src/Errors.ts +56 -0
- package/src/FormatValidators.ts +136 -0
- package/src/JsonValidation.ts +624 -0
- package/src/Report.ts +337 -0
- package/src/SchemaCache.ts +189 -0
- package/src/SchemaCompilation.ts +293 -0
- package/src/SchemaValidation.ts +629 -0
- package/src/Utils.ts +286 -0
- package/src/ZSchema.ts +467 -0
- package/src/index.ts +3 -0
- package/src/schemas/_ +0 -0
- package/umd/ZSchema.js +13791 -0
- package/umd/ZSchema.min.js +1 -0
- package/dist/ZSchema-browser-min.js +0 -2
- package/dist/ZSchema-browser-min.js.map +0 -1
- package/dist/ZSchema-browser-test.js +0 -32247
- package/dist/ZSchema-browser.js +0 -12745
- package/index.d.ts +0 -175
- package/src/Errors.js +0 -60
- package/src/FormatValidators.js +0 -129
- package/src/Polyfills.js +0 -16
- package/src/Report.js +0 -299
- package/src/SchemaValidation.js +0 -619
- package/src/ZSchema.js +0 -409
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import validator from 'validator';
|
|
2
|
+
|
|
3
|
+
export const FormatValidators = {
|
|
4
|
+
date: function (date) {
|
|
5
|
+
if (typeof date !== 'string') {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
// full-date from http://tools.ietf.org/html/rfc3339#section-5.6
|
|
9
|
+
const matches = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/.exec(date);
|
|
10
|
+
if (matches === null) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
// var year = matches[1];
|
|
14
|
+
// var month = matches[2];
|
|
15
|
+
// var day = matches[3];
|
|
16
|
+
if (matches[2] < '01' || matches[2] > '12' || matches[3] < '01' || matches[3] > '31') {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
},
|
|
21
|
+
'date-time': function (dateTime) {
|
|
22
|
+
if (typeof dateTime !== 'string') {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
// date-time from http://tools.ietf.org/html/rfc3339#section-5.6
|
|
26
|
+
const s = dateTime.toLowerCase().split('t');
|
|
27
|
+
if (!FormatValidators.date(s[0])) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
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
|
+
if (matches === null) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
// var hour = matches[1];
|
|
35
|
+
// var minute = matches[2];
|
|
36
|
+
// var second = matches[3];
|
|
37
|
+
// var fraction = matches[4];
|
|
38
|
+
// var timezone = matches[5];
|
|
39
|
+
if (matches[1] > '23' || matches[2] > '59' || matches[3] > '59') {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
},
|
|
44
|
+
email: function (email) {
|
|
45
|
+
if (typeof email !== 'string') {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return validator.isEmail(email, { require_tld: true });
|
|
49
|
+
},
|
|
50
|
+
hostname: function (hostname) {
|
|
51
|
+
if (typeof hostname !== 'string') {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
/*
|
|
55
|
+
http://json-schema.org/latest/json-schema-validation.html#anchor114
|
|
56
|
+
A string instance is valid against this attribute if it is a valid
|
|
57
|
+
representation for an Internet host name, as defined by RFC 1034, section 3.1 [RFC1034].
|
|
58
|
+
|
|
59
|
+
http://tools.ietf.org/html/rfc1034#section-3.5
|
|
60
|
+
|
|
61
|
+
<digit> ::= any one of the ten digits 0 through 9
|
|
62
|
+
var digit = /[0-9]/;
|
|
63
|
+
|
|
64
|
+
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
|
|
65
|
+
var letter = /[a-zA-Z]/;
|
|
66
|
+
|
|
67
|
+
<let-dig> ::= <letter> | <digit>
|
|
68
|
+
var letDig = /[0-9a-zA-Z]/;
|
|
69
|
+
|
|
70
|
+
<let-dig-hyp> ::= <let-dig> | "-"
|
|
71
|
+
var letDigHyp = /[-0-9a-zA-Z]/;
|
|
72
|
+
|
|
73
|
+
<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
|
|
74
|
+
var ldhStr = /[-0-9a-zA-Z]+/;
|
|
75
|
+
|
|
76
|
+
<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
|
|
77
|
+
var label = /[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?/;
|
|
78
|
+
|
|
79
|
+
<subdomain> ::= <label> | <subdomain> "." <label>
|
|
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
|
+
|
|
82
|
+
<domain> ::= <subdomain> | " "
|
|
83
|
+
var domain = null;
|
|
84
|
+
*/
|
|
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
|
+
if (valid) {
|
|
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
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return valid;
|
|
100
|
+
},
|
|
101
|
+
'host-name': function (hostname) {
|
|
102
|
+
return FormatValidators.hostname.call(this, hostname);
|
|
103
|
+
},
|
|
104
|
+
ipv4: function (ipv4) {
|
|
105
|
+
if (typeof ipv4 !== 'string') {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return validator.isIP(ipv4, 4);
|
|
109
|
+
},
|
|
110
|
+
ipv6: function (ipv6) {
|
|
111
|
+
if (typeof ipv6 !== 'string') {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return validator.isIP(ipv6, 6);
|
|
115
|
+
},
|
|
116
|
+
regex: function (str) {
|
|
117
|
+
try {
|
|
118
|
+
RegExp(str);
|
|
119
|
+
return true;
|
|
120
|
+
} catch (_e) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
uri: function (...args: [string]) {
|
|
125
|
+
if (this.options.strictUris) {
|
|
126
|
+
return FormatValidators['strict-uri'].apply(this, args);
|
|
127
|
+
}
|
|
128
|
+
const [uri] = args;
|
|
129
|
+
// https://github.com/zaggino/z-schema/issues/18
|
|
130
|
+
// RegExp from http://tools.ietf.org/html/rfc3986#appendix-B
|
|
131
|
+
return typeof uri !== 'string' || RegExp('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?').test(uri);
|
|
132
|
+
},
|
|
133
|
+
'strict-uri': function (uri) {
|
|
134
|
+
return typeof uri !== 'string' || validator.isURL(uri);
|
|
135
|
+
},
|
|
136
|
+
};
|