@sinclair/typebox 0.29.6 → 0.30.0-dev-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/compiler/compiler.d.ts +7 -4
- package/compiler/compiler.js +208 -194
- package/errors/errors.d.ts +65 -60
- package/errors/errors.js +515 -492
- package/package.json +14 -2
- package/readme.md +161 -169
- package/system/system.js +0 -6
- package/typebox.d.ts +99 -52
- package/typebox.js +496 -573
- package/value/cast.d.ts +4 -4
- package/value/cast.js +254 -260
- package/value/check.d.ts +4 -3
- package/value/check.js +412 -397
- package/value/clone.d.ts +2 -3
- package/value/clone.js +62 -42
- package/value/convert.d.ts +4 -4
- package/value/convert.js +304 -318
- package/value/create.d.ts +4 -6
- package/value/create.js +386 -376
- package/value/delta.d.ts +2 -4
- package/value/delta.js +121 -130
- package/value/equal.d.ts +2 -3
- package/value/equal.js +48 -51
- package/value/guard.d.ts +44 -0
- package/value/guard.js +146 -0
- package/value/hash.d.ts +14 -3
- package/value/hash.js +124 -166
- package/value/index.d.ts +3 -4
- package/value/index.js +6 -20
- package/value/mutate.d.ts +2 -4
- package/value/mutate.js +75 -67
- package/value/pointer.js +8 -2
- package/value/value.d.ts +20 -20
- package/value/value.js +27 -27
- package/value/is.d.ts +0 -11
- package/value/is.js +0 -53
package/value/check.js
CHANGED
|
@@ -27,13 +27,14 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
31
|
-
const Types = require("../typebox");
|
|
30
|
+
exports.Check = exports.ValueCheckDereferenceError = exports.ValueCheckUnknownTypeError = void 0;
|
|
32
31
|
const index_1 = require("../system/index");
|
|
33
|
-
const
|
|
34
|
-
|
|
32
|
+
const Types = require("../typebox");
|
|
33
|
+
const ValueGuard = require("./guard");
|
|
34
|
+
const ValueHash = require("./hash");
|
|
35
|
+
// --------------------------------------------------------------------------
|
|
35
36
|
// Errors
|
|
36
|
-
//
|
|
37
|
+
// --------------------------------------------------------------------------
|
|
37
38
|
class ValueCheckUnknownTypeError extends Error {
|
|
38
39
|
constructor(schema) {
|
|
39
40
|
super(`ValueCheck: ${schema[Types.Kind] ? `Unknown type '${schema[Types.Kind]}'` : 'Unknown type'}`);
|
|
@@ -48,445 +49,459 @@ class ValueCheckDereferenceError extends Error {
|
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
exports.ValueCheckDereferenceError = ValueCheckDereferenceError;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return index_1.TypeSystem.AllowVoidNull ? result || value === null : result;
|
|
94
|
-
}
|
|
95
|
-
// ----------------------------------------------------------------------
|
|
96
|
-
// Types
|
|
97
|
-
// ----------------------------------------------------------------------
|
|
98
|
-
function Any(schema, references, value) {
|
|
99
|
-
return true;
|
|
52
|
+
// --------------------------------------------------------------------------
|
|
53
|
+
// TypeGuards
|
|
54
|
+
// --------------------------------------------------------------------------
|
|
55
|
+
function IsAnyOrUnknown(schema) {
|
|
56
|
+
return schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown';
|
|
57
|
+
}
|
|
58
|
+
// --------------------------------------------------------------------------
|
|
59
|
+
// Guards
|
|
60
|
+
// --------------------------------------------------------------------------
|
|
61
|
+
function IsDefined(value) {
|
|
62
|
+
return value !== undefined;
|
|
63
|
+
}
|
|
64
|
+
// --------------------------------------------------------------------------
|
|
65
|
+
// Policies
|
|
66
|
+
// --------------------------------------------------------------------------
|
|
67
|
+
function IsExactOptionalProperty(value, key) {
|
|
68
|
+
return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
|
|
69
|
+
}
|
|
70
|
+
function IsObject(value) {
|
|
71
|
+
const isObject = ValueGuard.IsObject(value);
|
|
72
|
+
return index_1.TypeSystem.AllowArrayObjects ? isObject : isObject && !ValueGuard.IsArray(value);
|
|
73
|
+
}
|
|
74
|
+
function IsRecordObject(value) {
|
|
75
|
+
return IsObject(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
|
|
76
|
+
}
|
|
77
|
+
function IsNumber(value) {
|
|
78
|
+
const isNumber = ValueGuard.IsNumber(value);
|
|
79
|
+
return index_1.TypeSystem.AllowNaN ? isNumber : isNumber && Number.isFinite(value);
|
|
80
|
+
}
|
|
81
|
+
function IsVoid(value) {
|
|
82
|
+
const isUndefined = ValueGuard.IsUndefined(value);
|
|
83
|
+
return index_1.TypeSystem.AllowVoidNull ? isUndefined || value === null : isUndefined;
|
|
84
|
+
}
|
|
85
|
+
// --------------------------------------------------------------------------
|
|
86
|
+
// Types
|
|
87
|
+
// --------------------------------------------------------------------------
|
|
88
|
+
function TAny(schema, references, value) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
function TArray(schema, references, value) {
|
|
92
|
+
if (!Array.isArray(value)) {
|
|
93
|
+
return false;
|
|
100
94
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
// prettier-ignore
|
|
112
|
-
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
113
|
-
const hashed = hash_1.ValueHash.Create(element);
|
|
114
|
-
if (set.has(hashed)) {
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
set.add(hashed);
|
|
119
|
-
}
|
|
120
|
-
} return true; })())) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
return value.every((value) => Visit(schema.items, references, value));
|
|
95
|
+
if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
96
|
+
return false;
|
|
124
97
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
98
|
+
if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (!value.every((value) => Visit(schema.items, references, value))) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
// prettier-ignore
|
|
105
|
+
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
106
|
+
const hashed = ValueHash.Hash(element);
|
|
107
|
+
if (set.has(hashed)) {
|
|
139
108
|
return false;
|
|
140
109
|
}
|
|
141
|
-
|
|
142
|
-
|
|
110
|
+
else {
|
|
111
|
+
set.add(hashed);
|
|
143
112
|
}
|
|
144
|
-
|
|
113
|
+
} return true; })())) {
|
|
114
|
+
return false;
|
|
145
115
|
}
|
|
146
|
-
|
|
147
|
-
|
|
116
|
+
// contains
|
|
117
|
+
if (!(IsDefined(schema.contains) || IsNumber(schema.minContains) || IsNumber(schema.maxContains))) {
|
|
118
|
+
return true; // exit
|
|
148
119
|
}
|
|
149
|
-
|
|
150
|
-
|
|
120
|
+
const containsSchema = IsDefined(schema.contains) ? schema.contains : Types.Type.Never();
|
|
121
|
+
const containsCount = value.reduce((acc, value) => (Visit(containsSchema, references, value) ? acc + 1 : acc), 0);
|
|
122
|
+
if (containsCount === 0) {
|
|
123
|
+
return false;
|
|
151
124
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
if (!IsNumber(value.getTime())) {
|
|
157
|
-
return false;
|
|
158
|
-
}
|
|
159
|
-
if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
return true;
|
|
125
|
+
if (IsNumber(schema.minContains) && containsCount < schema.minContains) {
|
|
126
|
+
return false;
|
|
172
127
|
}
|
|
173
|
-
|
|
174
|
-
return
|
|
128
|
+
if (IsNumber(schema.maxContains) && containsCount > schema.maxContains) {
|
|
129
|
+
return false;
|
|
175
130
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return false;
|
|
185
|
-
}
|
|
186
|
-
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
return true;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
function TAsyncIterator(schema, references, value) {
|
|
134
|
+
return IsObject(value) && Symbol.asyncIterator in value;
|
|
135
|
+
}
|
|
136
|
+
function TBigInt(schema, references, value) {
|
|
137
|
+
if (!ValueGuard.IsBigInt(value)) {
|
|
138
|
+
return false;
|
|
196
139
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (schema.unevaluatedProperties === false) {
|
|
200
|
-
const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
201
|
-
const check2 = globalThis.Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key));
|
|
202
|
-
return check1 && check2;
|
|
203
|
-
}
|
|
204
|
-
else if (Types.TypeGuard.TSchema(schema.unevaluatedProperties)) {
|
|
205
|
-
const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
206
|
-
const check2 = globalThis.Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key) || Visit(schema.unevaluatedProperties, references, value[key]));
|
|
207
|
-
return check1 && check2;
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
return check1;
|
|
211
|
-
}
|
|
140
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === BigInt(0))) {
|
|
141
|
+
return false;
|
|
212
142
|
}
|
|
213
|
-
|
|
214
|
-
return
|
|
143
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
144
|
+
return false;
|
|
215
145
|
}
|
|
216
|
-
|
|
146
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
217
147
|
return false;
|
|
218
148
|
}
|
|
219
|
-
|
|
220
|
-
return
|
|
149
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
150
|
+
return false;
|
|
221
151
|
}
|
|
222
|
-
|
|
223
|
-
return
|
|
152
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
153
|
+
return false;
|
|
224
154
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return false;
|
|
237
|
-
}
|
|
238
|
-
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
return true;
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
function TBoolean(schema, references, value) {
|
|
158
|
+
return typeof value === 'boolean';
|
|
159
|
+
}
|
|
160
|
+
function TConstructor(schema, references, value) {
|
|
161
|
+
return Visit(schema.returns, references, value.prototype);
|
|
162
|
+
}
|
|
163
|
+
function TDate(schema, references, value) {
|
|
164
|
+
if (!(value instanceof Date)) {
|
|
165
|
+
return false;
|
|
245
166
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
167
|
+
if (!IsNumber(value.getTime())) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
function TFunction(schema, references, value) {
|
|
185
|
+
return typeof value === 'function';
|
|
186
|
+
}
|
|
187
|
+
function TInteger(schema, references, value) {
|
|
188
|
+
if (!ValueGuard.IsInteger(value)) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
function TIntersect(schema, references, value) {
|
|
209
|
+
const check1 = schema.allOf.every((schema) => Visit(schema, references, value));
|
|
210
|
+
if (schema.unevaluatedProperties === false) {
|
|
211
|
+
const keyPattern = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
212
|
+
const check2 = Object.getOwnPropertyNames(value).every((key) => keyPattern.test(key));
|
|
213
|
+
return check1 && check2;
|
|
214
|
+
}
|
|
215
|
+
else if (Types.TypeGuard.TSchema(schema.unevaluatedProperties)) {
|
|
216
|
+
const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
217
|
+
const check2 = Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key) || Visit(schema.unevaluatedProperties, references, value[key]));
|
|
218
|
+
return check1 && check2;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
return check1;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function TIterator(schema, references, value) {
|
|
225
|
+
return IsObject(value) && Symbol.iterator in value;
|
|
226
|
+
}
|
|
227
|
+
function TLiteral(schema, references, value) {
|
|
228
|
+
return value === schema.const;
|
|
229
|
+
}
|
|
230
|
+
function TNever(schema, references, value) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
function TNot(schema, references, value) {
|
|
234
|
+
return !Visit(schema.not, references, value);
|
|
235
|
+
}
|
|
236
|
+
function TNull(schema, references, value) {
|
|
237
|
+
return value === null;
|
|
238
|
+
}
|
|
239
|
+
function TNumber(schema, references, value) {
|
|
240
|
+
if (!IsNumber(value)) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
function TObject(schema, references, value) {
|
|
261
|
+
if (!IsObject(value)) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
if (IsDefined(schema.maxProperties) && !(Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
271
|
+
for (const knownKey of knownKeys) {
|
|
272
|
+
const property = schema.properties[knownKey];
|
|
273
|
+
if (schema.required && schema.required.includes(knownKey)) {
|
|
274
|
+
if (!Visit(property, references, value[knownKey])) {
|
|
275
|
+
return false;
|
|
266
276
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
277
|
+
if ((Types.ExtendsUndefined.Check(property) || IsAnyOrUnknown(property)) && !(knownKey in value)) {
|
|
278
|
+
return false;
|
|
271
279
|
}
|
|
272
280
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (schema.required && schema.required.length === knownKeys.length && valueKeys.length === knownKeys.length) {
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
return valueKeys.every((valueKey) => knownKeys.includes(valueKey));
|
|
281
|
+
else {
|
|
282
|
+
if (IsExactOptionalProperty(value, knownKey) && !Visit(property, references, value[knownKey])) {
|
|
283
|
+
return false;
|
|
281
284
|
}
|
|
282
285
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
+
}
|
|
287
|
+
if (schema.additionalProperties === false) {
|
|
288
|
+
const valueKeys = Object.getOwnPropertyNames(value);
|
|
289
|
+
// optimization: value is valid if schemaKey length matches the valueKey length
|
|
290
|
+
if (schema.required && schema.required.length === knownKeys.length && valueKeys.length === knownKeys.length) {
|
|
291
|
+
return true;
|
|
286
292
|
}
|
|
287
293
|
else {
|
|
288
|
-
return
|
|
294
|
+
return valueKeys.every((valueKey) => knownKeys.includes(valueKey));
|
|
289
295
|
}
|
|
290
296
|
}
|
|
291
|
-
|
|
292
|
-
|
|
297
|
+
else if (typeof schema.additionalProperties === 'object') {
|
|
298
|
+
const valueKeys = Object.getOwnPropertyNames(value);
|
|
299
|
+
return valueKeys.every((key) => knownKeys.includes(key) || Visit(schema.additionalProperties, references, value[key]));
|
|
293
300
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
299
|
-
return false;
|
|
300
|
-
}
|
|
301
|
-
if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
302
|
-
return false;
|
|
303
|
-
}
|
|
304
|
-
const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
305
|
-
const regex = new RegExp(patternKey);
|
|
306
|
-
return globalThis.Object.entries(value).every(([key, value]) => {
|
|
307
|
-
if (regex.test(key)) {
|
|
308
|
-
return Visit(patternSchema, references, value);
|
|
309
|
-
}
|
|
310
|
-
if (typeof schema.additionalProperties === 'object') {
|
|
311
|
-
return Visit(schema.additionalProperties, references, value);
|
|
312
|
-
}
|
|
313
|
-
if (schema.additionalProperties === false) {
|
|
314
|
-
return false;
|
|
315
|
-
}
|
|
316
|
-
return true;
|
|
317
|
-
});
|
|
301
|
+
else {
|
|
302
|
+
return true;
|
|
318
303
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
304
|
+
}
|
|
305
|
+
function TPromise(schema, references, value) {
|
|
306
|
+
return typeof value === 'object' && typeof value.then === 'function';
|
|
307
|
+
}
|
|
308
|
+
function TRecord(schema, references, value) {
|
|
309
|
+
if (!IsRecordObject(value)) {
|
|
310
|
+
return false;
|
|
325
311
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
if (IsDefined(schema.pattern)) {
|
|
339
|
-
const regex = new RegExp(schema.pattern);
|
|
340
|
-
if (!regex.test(value))
|
|
341
|
-
return false;
|
|
312
|
+
if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
if (IsDefined(schema.maxProperties) && !(Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
319
|
+
const regex = new RegExp(patternKey);
|
|
320
|
+
return Object.entries(value).every(([key, value]) => {
|
|
321
|
+
if (regex.test(key)) {
|
|
322
|
+
return Visit(patternSchema, references, value);
|
|
342
323
|
}
|
|
343
|
-
if (
|
|
344
|
-
|
|
345
|
-
return false;
|
|
346
|
-
const func = Types.FormatRegistry.Get(schema.format);
|
|
347
|
-
return func(value);
|
|
324
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
325
|
+
return Visit(schema.additionalProperties, references, value);
|
|
348
326
|
}
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
function Symbol(schema, references, value) {
|
|
352
|
-
if (!(typeof value === 'symbol')) {
|
|
327
|
+
if (schema.additionalProperties === false) {
|
|
353
328
|
return false;
|
|
354
329
|
}
|
|
355
330
|
return true;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
function TRef(schema, references, value) {
|
|
334
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
335
|
+
if (index === -1)
|
|
336
|
+
throw new ValueCheckDereferenceError(schema);
|
|
337
|
+
const target = references[index];
|
|
338
|
+
return Visit(target, references, value);
|
|
339
|
+
}
|
|
340
|
+
function TString(schema, references, value) {
|
|
341
|
+
if (!ValueGuard.IsString(value)) {
|
|
342
|
+
return false;
|
|
356
343
|
}
|
|
357
|
-
|
|
358
|
-
if (!
|
|
344
|
+
if (IsDefined(schema.minLength)) {
|
|
345
|
+
if (!(value.length >= schema.minLength))
|
|
359
346
|
return false;
|
|
360
|
-
}
|
|
361
|
-
return new RegExp(schema.pattern).test(value);
|
|
362
347
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
if (index === -1)
|
|
366
|
-
throw new ValueCheckDereferenceError(schema);
|
|
367
|
-
const target = references[index];
|
|
368
|
-
return Visit(target, references, value);
|
|
369
|
-
}
|
|
370
|
-
function Tuple(schema, references, value) {
|
|
371
|
-
if (!globalThis.Array.isArray(value)) {
|
|
348
|
+
if (IsDefined(schema.maxLength)) {
|
|
349
|
+
if (!(value.length <= schema.maxLength))
|
|
372
350
|
return false;
|
|
373
|
-
|
|
374
|
-
|
|
351
|
+
}
|
|
352
|
+
if (IsDefined(schema.pattern)) {
|
|
353
|
+
const regex = new RegExp(schema.pattern);
|
|
354
|
+
if (!regex.test(value))
|
|
375
355
|
return false;
|
|
376
|
-
|
|
377
|
-
|
|
356
|
+
}
|
|
357
|
+
if (IsDefined(schema.format)) {
|
|
358
|
+
if (!Types.FormatRegistry.Has(schema.format))
|
|
378
359
|
return false;
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
return true;
|
|
382
|
-
}
|
|
383
|
-
for (let i = 0; i < schema.items.length; i++) {
|
|
384
|
-
if (!Visit(schema.items[i], references, value[i]))
|
|
385
|
-
return false;
|
|
386
|
-
}
|
|
387
|
-
return true;
|
|
360
|
+
const func = Types.FormatRegistry.Get(schema.format);
|
|
361
|
+
return func(value);
|
|
388
362
|
}
|
|
389
|
-
|
|
390
|
-
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
function TSymbol(schema, references, value) {
|
|
366
|
+
if (!(typeof value === 'symbol')) {
|
|
367
|
+
return false;
|
|
391
368
|
}
|
|
392
|
-
|
|
393
|
-
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
function TTemplateLiteral(schema, references, value) {
|
|
372
|
+
if (!ValueGuard.IsString(value)) {
|
|
373
|
+
return false;
|
|
394
374
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
375
|
+
return new RegExp(schema.pattern).test(value);
|
|
376
|
+
}
|
|
377
|
+
function TThis(schema, references, value) {
|
|
378
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
379
|
+
if (index === -1)
|
|
380
|
+
throw new ValueCheckDereferenceError(schema);
|
|
381
|
+
const target = references[index];
|
|
382
|
+
return Visit(target, references, value);
|
|
383
|
+
}
|
|
384
|
+
function TTuple(schema, references, value) {
|
|
385
|
+
if (!ValueGuard.IsArray(value)) {
|
|
386
|
+
return false;
|
|
406
387
|
}
|
|
407
|
-
|
|
408
|
-
return
|
|
388
|
+
if (schema.items === undefined && !(value.length === 0)) {
|
|
389
|
+
return false;
|
|
409
390
|
}
|
|
410
|
-
|
|
411
|
-
return
|
|
391
|
+
if (!(value.length === schema.maxItems)) {
|
|
392
|
+
return false;
|
|
412
393
|
}
|
|
413
|
-
|
|
414
|
-
|
|
394
|
+
if (!schema.items) {
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
for (let i = 0; i < schema.items.length; i++) {
|
|
398
|
+
if (!Visit(schema.items[i], references, value[i]))
|
|
415
399
|
return false;
|
|
416
|
-
const func = Types.TypeRegistry.Get(schema[Types.Kind]);
|
|
417
|
-
return func(schema, value);
|
|
418
|
-
}
|
|
419
|
-
function Visit(schema, references, value) {
|
|
420
|
-
const references_ = IsDefined(schema.$id) ? [...references, schema] : references;
|
|
421
|
-
const schema_ = schema;
|
|
422
|
-
switch (schema_[Types.Kind]) {
|
|
423
|
-
case 'Any':
|
|
424
|
-
return Any(schema_, references_, value);
|
|
425
|
-
case 'Array':
|
|
426
|
-
return Array(schema_, references_, value);
|
|
427
|
-
case 'BigInt':
|
|
428
|
-
return BigInt(schema_, references_, value);
|
|
429
|
-
case 'Boolean':
|
|
430
|
-
return Boolean(schema_, references_, value);
|
|
431
|
-
case 'Constructor':
|
|
432
|
-
return Constructor(schema_, references_, value);
|
|
433
|
-
case 'Date':
|
|
434
|
-
return Date(schema_, references_, value);
|
|
435
|
-
case 'Function':
|
|
436
|
-
return Function(schema_, references_, value);
|
|
437
|
-
case 'Integer':
|
|
438
|
-
return Integer(schema_, references_, value);
|
|
439
|
-
case 'Intersect':
|
|
440
|
-
return Intersect(schema_, references_, value);
|
|
441
|
-
case 'Literal':
|
|
442
|
-
return Literal(schema_, references_, value);
|
|
443
|
-
case 'Never':
|
|
444
|
-
return Never(schema_, references_, value);
|
|
445
|
-
case 'Not':
|
|
446
|
-
return Not(schema_, references_, value);
|
|
447
|
-
case 'Null':
|
|
448
|
-
return Null(schema_, references_, value);
|
|
449
|
-
case 'Number':
|
|
450
|
-
return Number(schema_, references_, value);
|
|
451
|
-
case 'Object':
|
|
452
|
-
return Object(schema_, references_, value);
|
|
453
|
-
case 'Promise':
|
|
454
|
-
return Promise(schema_, references_, value);
|
|
455
|
-
case 'Record':
|
|
456
|
-
return Record(schema_, references_, value);
|
|
457
|
-
case 'Ref':
|
|
458
|
-
return Ref(schema_, references_, value);
|
|
459
|
-
case 'String':
|
|
460
|
-
return String(schema_, references_, value);
|
|
461
|
-
case 'Symbol':
|
|
462
|
-
return Symbol(schema_, references_, value);
|
|
463
|
-
case 'TemplateLiteral':
|
|
464
|
-
return TemplateLiteral(schema_, references_, value);
|
|
465
|
-
case 'This':
|
|
466
|
-
return This(schema_, references_, value);
|
|
467
|
-
case 'Tuple':
|
|
468
|
-
return Tuple(schema_, references_, value);
|
|
469
|
-
case 'Undefined':
|
|
470
|
-
return Undefined(schema_, references_, value);
|
|
471
|
-
case 'Union':
|
|
472
|
-
return Union(schema_, references_, value);
|
|
473
|
-
case 'Uint8Array':
|
|
474
|
-
return Uint8Array(schema_, references_, value);
|
|
475
|
-
case 'Unknown':
|
|
476
|
-
return Unknown(schema_, references_, value);
|
|
477
|
-
case 'Void':
|
|
478
|
-
return Void(schema_, references_, value);
|
|
479
|
-
default:
|
|
480
|
-
if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
|
|
481
|
-
throw new ValueCheckUnknownTypeError(schema_);
|
|
482
|
-
return UserDefined(schema_, references_, value);
|
|
483
|
-
}
|
|
484
400
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
function TUndefined(schema, references, value) {
|
|
404
|
+
return value === undefined;
|
|
405
|
+
}
|
|
406
|
+
function TUnion(schema, references, value) {
|
|
407
|
+
return schema.anyOf.some((inner) => Visit(inner, references, value));
|
|
408
|
+
}
|
|
409
|
+
function TUint8Array(schema, references, value) {
|
|
410
|
+
if (!(value instanceof Uint8Array)) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
|
|
417
|
+
return false;
|
|
490
418
|
}
|
|
491
|
-
|
|
492
|
-
}
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
function TUnknown(schema, references, value) {
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
function TVoid(schema, references, value) {
|
|
425
|
+
return IsVoid(value);
|
|
426
|
+
}
|
|
427
|
+
function TKind(schema, references, value) {
|
|
428
|
+
if (!Types.TypeRegistry.Has(schema[Types.Kind]))
|
|
429
|
+
return false;
|
|
430
|
+
const func = Types.TypeRegistry.Get(schema[Types.Kind]);
|
|
431
|
+
return func(schema, value);
|
|
432
|
+
}
|
|
433
|
+
function Visit(schema, references, value) {
|
|
434
|
+
const references_ = IsDefined(schema.$id) ? [...references, schema] : references;
|
|
435
|
+
const schema_ = schema;
|
|
436
|
+
switch (schema_[Types.Kind]) {
|
|
437
|
+
case 'Any':
|
|
438
|
+
return TAny(schema_, references_, value);
|
|
439
|
+
case 'Array':
|
|
440
|
+
return TArray(schema_, references_, value);
|
|
441
|
+
case 'AsyncIterator':
|
|
442
|
+
return TAsyncIterator(schema_, references_, value);
|
|
443
|
+
case 'BigInt':
|
|
444
|
+
return TBigInt(schema_, references_, value);
|
|
445
|
+
case 'Boolean':
|
|
446
|
+
return TBoolean(schema_, references_, value);
|
|
447
|
+
case 'Constructor':
|
|
448
|
+
return TConstructor(schema_, references_, value);
|
|
449
|
+
case 'Date':
|
|
450
|
+
return TDate(schema_, references_, value);
|
|
451
|
+
case 'Function':
|
|
452
|
+
return TFunction(schema_, references_, value);
|
|
453
|
+
case 'Integer':
|
|
454
|
+
return TInteger(schema_, references_, value);
|
|
455
|
+
case 'Intersect':
|
|
456
|
+
return TIntersect(schema_, references_, value);
|
|
457
|
+
case 'Iterator':
|
|
458
|
+
return TIterator(schema_, references_, value);
|
|
459
|
+
case 'Literal':
|
|
460
|
+
return TLiteral(schema_, references_, value);
|
|
461
|
+
case 'Never':
|
|
462
|
+
return TNever(schema_, references_, value);
|
|
463
|
+
case 'Not':
|
|
464
|
+
return TNot(schema_, references_, value);
|
|
465
|
+
case 'Null':
|
|
466
|
+
return TNull(schema_, references_, value);
|
|
467
|
+
case 'Number':
|
|
468
|
+
return TNumber(schema_, references_, value);
|
|
469
|
+
case 'Object':
|
|
470
|
+
return TObject(schema_, references_, value);
|
|
471
|
+
case 'Promise':
|
|
472
|
+
return TPromise(schema_, references_, value);
|
|
473
|
+
case 'Record':
|
|
474
|
+
return TRecord(schema_, references_, value);
|
|
475
|
+
case 'Ref':
|
|
476
|
+
return TRef(schema_, references_, value);
|
|
477
|
+
case 'String':
|
|
478
|
+
return TString(schema_, references_, value);
|
|
479
|
+
case 'Symbol':
|
|
480
|
+
return TSymbol(schema_, references_, value);
|
|
481
|
+
case 'TemplateLiteral':
|
|
482
|
+
return TTemplateLiteral(schema_, references_, value);
|
|
483
|
+
case 'This':
|
|
484
|
+
return TThis(schema_, references_, value);
|
|
485
|
+
case 'Tuple':
|
|
486
|
+
return TTuple(schema_, references_, value);
|
|
487
|
+
case 'Undefined':
|
|
488
|
+
return TUndefined(schema_, references_, value);
|
|
489
|
+
case 'Union':
|
|
490
|
+
return TUnion(schema_, references_, value);
|
|
491
|
+
case 'Uint8Array':
|
|
492
|
+
return TUint8Array(schema_, references_, value);
|
|
493
|
+
case 'Unknown':
|
|
494
|
+
return TUnknown(schema_, references_, value);
|
|
495
|
+
case 'Void':
|
|
496
|
+
return TVoid(schema_, references_, value);
|
|
497
|
+
default:
|
|
498
|
+
if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
|
|
499
|
+
throw new ValueCheckUnknownTypeError(schema_);
|
|
500
|
+
return TKind(schema_, references_, value);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
/** Returns true if the value matches the given type. */
|
|
504
|
+
function Check(...args) {
|
|
505
|
+
return args.length === 3 ? Visit(args[0], args[1], args[2]) : Visit(args[0], [], args[1]);
|
|
506
|
+
}
|
|
507
|
+
exports.Check = Check;
|