@serum-enterprises/schema 3.1.0-beta.0 → 3.3.0-beta.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.
- package/build/index.js +393 -494
- package/build/index.js.map +4 -4
- package/package.json +2 -2
- package/types/Builders/Array.d.ts +15 -0
- package/types/Builders/Boolean.d.ts +7 -0
- package/types/Builders/JSON.d.ts +4 -0
- package/types/Builders/Number.d.ts +10 -0
- package/types/Builders/Object.d.ts +30 -0
- package/types/Builders/String.d.ts +9 -0
- package/types/Builders/Union.d.ts +7 -0
- package/types/Builders/index.d.ts +6 -0
- package/types/Definitions.d.ts +52 -0
- package/types/Validator.d.ts +3 -3
- package/types/index.d.ts +29 -22
- package/types/lib/util.d.ts +0 -3
- package/types/validators/Array.d.ts +4 -25
- package/types/validators/Boolean.d.ts +4 -12
- package/types/validators/JSON.d.ts +2 -5
- package/types/validators/Number.d.ts +5 -18
- package/types/validators/Object.d.ts +5 -38
- package/types/validators/String.d.ts +5 -16
- package/types/validators/Union.d.ts +13 -0
- package/types/validators/index.d.ts +6 -0
- package/types/lib/isEquals.d.ts +0 -2
package/build/index.js
CHANGED
|
@@ -120,14 +120,8 @@ var require_Option = __commonJS({
|
|
|
120
120
|
"node_modules/@serum-enterprises/option/build/Option.js"(exports2) {
|
|
121
121
|
"use strict";
|
|
122
122
|
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
123
|
-
exports2.
|
|
124
|
-
var
|
|
125
|
-
static Some(value) {
|
|
126
|
-
return new Some(value);
|
|
127
|
-
}
|
|
128
|
-
static None() {
|
|
129
|
-
return new None();
|
|
130
|
-
}
|
|
123
|
+
exports2.Option = exports2.None = exports2.Some = void 0;
|
|
124
|
+
var BaseOption = class {
|
|
131
125
|
isSome() {
|
|
132
126
|
return this instanceof Some;
|
|
133
127
|
}
|
|
@@ -145,7 +139,7 @@ var require_Option = __commonJS({
|
|
|
145
139
|
return this;
|
|
146
140
|
}
|
|
147
141
|
map(fn) {
|
|
148
|
-
return this.match((value) =>
|
|
142
|
+
return this.match((value) => exports2.Option.Some(fn(value)), () => exports2.Option.None());
|
|
149
143
|
}
|
|
150
144
|
match(onSome, onNone) {
|
|
151
145
|
if (this.isSome())
|
|
@@ -153,28 +147,40 @@ var require_Option = __commonJS({
|
|
|
153
147
|
else
|
|
154
148
|
return onNone();
|
|
155
149
|
}
|
|
150
|
+
equals(other, fn = (a, b) => Object.is(a, b)) {
|
|
151
|
+
if (this.isNone() && other.isNone())
|
|
152
|
+
return true;
|
|
153
|
+
if (this.isSome() && other.isSome())
|
|
154
|
+
return fn(this.value, other.value);
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
156
157
|
};
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
_value;
|
|
158
|
+
var Some = class extends BaseOption {
|
|
159
|
+
value;
|
|
160
160
|
constructor(value) {
|
|
161
161
|
super();
|
|
162
|
-
this.
|
|
163
|
-
}
|
|
164
|
-
get value() {
|
|
165
|
-
return this._value;
|
|
162
|
+
this.value = value;
|
|
166
163
|
}
|
|
167
164
|
};
|
|
168
165
|
exports2.Some = Some;
|
|
169
|
-
var None = class extends
|
|
166
|
+
var None = class extends BaseOption {
|
|
170
167
|
};
|
|
171
168
|
exports2.None = None;
|
|
169
|
+
exports2.Option = {
|
|
170
|
+
Some(value) {
|
|
171
|
+
return new Some(value);
|
|
172
|
+
},
|
|
173
|
+
None() {
|
|
174
|
+
return new None();
|
|
175
|
+
}
|
|
176
|
+
};
|
|
172
177
|
}
|
|
173
178
|
});
|
|
174
179
|
|
|
175
180
|
// src/index.ts
|
|
176
181
|
var index_exports = {};
|
|
177
182
|
__export(index_exports, {
|
|
183
|
+
Builders: () => Builders,
|
|
178
184
|
Schema: () => Schema,
|
|
179
185
|
Validator: () => Validator,
|
|
180
186
|
Validators: () => Validators
|
|
@@ -200,7 +206,7 @@ var Validator = class {
|
|
|
200
206
|
// src/lib/fromJSON.ts
|
|
201
207
|
var import_json7 = __toESM(require_build());
|
|
202
208
|
|
|
203
|
-
// src/
|
|
209
|
+
// src/Validators/JSON.ts
|
|
204
210
|
var import_json = __toESM(require_build());
|
|
205
211
|
|
|
206
212
|
// src/lib/util.ts
|
|
@@ -209,7 +215,7 @@ var AssertError = class extends Error {
|
|
|
209
215
|
var DefinitionError = class extends Error {
|
|
210
216
|
};
|
|
211
217
|
|
|
212
|
-
// src/
|
|
218
|
+
// src/Validators/JSON.ts
|
|
213
219
|
var JSONValidator = class _JSONValidator extends Validator {
|
|
214
220
|
static fromJSON(_definition, _path = "definition") {
|
|
215
221
|
return new _JSONValidator();
|
|
@@ -231,7 +237,7 @@ var JSONValidator = class _JSONValidator extends Validator {
|
|
|
231
237
|
}
|
|
232
238
|
};
|
|
233
239
|
|
|
234
|
-
// src/
|
|
240
|
+
// src/Validators/Boolean.ts
|
|
235
241
|
var import_json2 = __toESM(require_build());
|
|
236
242
|
var import_option = __toESM(require_Option());
|
|
237
243
|
var BooleanValidator = class _BooleanValidator extends Validator {
|
|
@@ -240,25 +246,18 @@ var BooleanValidator = class _BooleanValidator extends Validator {
|
|
|
240
246
|
if ("nullable" in definition) {
|
|
241
247
|
if (!import_json2.JSON.isBoolean(definition["nullable"]))
|
|
242
248
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
243
|
-
|
|
249
|
+
if (definition["nullable"])
|
|
250
|
+
validatorInstance._nullable = import_option.Option.Some(null);
|
|
244
251
|
}
|
|
245
252
|
if ("equals" in definition) {
|
|
246
253
|
if (!import_json2.JSON.isBoolean(definition["equals"]))
|
|
247
254
|
throw new DefinitionError(`Expected ${path}.equals to be a Boolean`);
|
|
248
|
-
validatorInstance.
|
|
255
|
+
validatorInstance._equals = import_option.Option.Some(definition["equals"]);
|
|
249
256
|
}
|
|
250
257
|
return validatorInstance;
|
|
251
258
|
}
|
|
252
259
|
_nullable = import_option.Option.None();
|
|
253
260
|
_equals = import_option.Option.None();
|
|
254
|
-
nullable(flag) {
|
|
255
|
-
this._nullable = flag ?? true ? import_option.Option.Some(null) : import_option.Option.None();
|
|
256
|
-
return this;
|
|
257
|
-
}
|
|
258
|
-
equals(value) {
|
|
259
|
-
this._equals = import_option.Option.Some(value);
|
|
260
|
-
return this;
|
|
261
|
-
}
|
|
262
261
|
assert(data, path = "data") {
|
|
263
262
|
if (import_json2.JSON.isBoolean(data)) {
|
|
264
263
|
if (this._equals.isSome() && this._equals.value !== data)
|
|
@@ -287,13 +286,7 @@ var BooleanValidator = class _BooleanValidator extends Validator {
|
|
|
287
286
|
isEquals(other) {
|
|
288
287
|
if (!(other instanceof _BooleanValidator))
|
|
289
288
|
return false;
|
|
290
|
-
|
|
291
|
-
return false;
|
|
292
|
-
if (this._equals.isSome() !== other._equals.isSome())
|
|
293
|
-
return false;
|
|
294
|
-
if (this._equals.isSome() && other._equals.isSome() && this._equals.value !== other._equals.value)
|
|
295
|
-
return false;
|
|
296
|
-
return true;
|
|
289
|
+
return this._nullable.equals(other._nullable) && this._equals.equals(other._equals);
|
|
297
290
|
}
|
|
298
291
|
toJSON() {
|
|
299
292
|
const definition = {
|
|
@@ -307,7 +300,7 @@ var BooleanValidator = class _BooleanValidator extends Validator {
|
|
|
307
300
|
}
|
|
308
301
|
};
|
|
309
302
|
|
|
310
|
-
// src/
|
|
303
|
+
// src/Validators/Number.ts
|
|
311
304
|
var import_json3 = __toESM(require_build());
|
|
312
305
|
var import_option2 = __toESM(require_Option());
|
|
313
306
|
var NumberValidator = class _NumberValidator extends Validator {
|
|
@@ -316,71 +309,37 @@ var NumberValidator = class _NumberValidator extends Validator {
|
|
|
316
309
|
if ("nullable" in definition) {
|
|
317
310
|
if (!import_json3.JSON.isBoolean(definition["nullable"]))
|
|
318
311
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
319
|
-
|
|
312
|
+
if (definition["nullable"])
|
|
313
|
+
validatorInstance._nullable = import_option2.Option.Some(null);
|
|
320
314
|
}
|
|
321
315
|
if ("equals" in definition) {
|
|
322
316
|
if (!import_json3.JSON.isNumber(definition["equals"]))
|
|
323
317
|
throw new DefinitionError(`Expected ${path}.equals to be a Number`);
|
|
324
|
-
validatorInstance.
|
|
318
|
+
validatorInstance._equals = import_option2.Option.Some(definition["equals"]);
|
|
325
319
|
}
|
|
326
320
|
if ("integer" in definition) {
|
|
327
321
|
if (!import_json3.JSON.isBoolean(definition["integer"]))
|
|
328
322
|
throw new DefinitionError(`Expected ${path}.integer to be a Boolean`);
|
|
329
|
-
|
|
323
|
+
if (definition["integer"])
|
|
324
|
+
validatorInstance._integer = import_option2.Option.Some(null);
|
|
330
325
|
}
|
|
331
326
|
if ("min" in definition) {
|
|
332
327
|
if (!import_json3.JSON.isNumber(definition["min"]))
|
|
333
328
|
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
334
|
-
validatorInstance.
|
|
329
|
+
validatorInstance._min = import_option2.Option.Some(definition["min"]);
|
|
335
330
|
}
|
|
336
331
|
if ("max" in definition) {
|
|
337
332
|
if (!import_json3.JSON.isNumber(definition["max"]))
|
|
338
333
|
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
339
|
-
validatorInstance.
|
|
334
|
+
validatorInstance._max = import_option2.Option.Some(definition["max"]);
|
|
340
335
|
}
|
|
341
336
|
return validatorInstance;
|
|
342
337
|
}
|
|
343
338
|
_nullable = import_option2.Option.None();
|
|
344
|
-
_equals = import_option2.Option.None();
|
|
345
339
|
_integer = import_option2.Option.None();
|
|
346
340
|
_min = import_option2.Option.None();
|
|
347
341
|
_max = import_option2.Option.None();
|
|
348
|
-
|
|
349
|
-
this._nullable = flag ?? true ? import_option2.Option.Some(null) : import_option2.Option.None();
|
|
350
|
-
return this;
|
|
351
|
-
}
|
|
352
|
-
equals(value, path = "equals") {
|
|
353
|
-
if (this._integer.isSome() && !Number.isSafeInteger(value))
|
|
354
|
-
throw new DefinitionError(`Expected Equals Rule to be an Integer according to the Integer Rule at Path ${path}`);
|
|
355
|
-
if (this._min.isSome() && this._min.value > value)
|
|
356
|
-
throw new DefinitionError(`Expected Equals Rule to be larger than or equal to the Minimum Rule at Path ${path}`);
|
|
357
|
-
if (this._max.isSome() && this._max.value < value)
|
|
358
|
-
throw new DefinitionError(`Expected Equals Rule to be smaller than or equal to the Maximum Rule at Path ${path}`);
|
|
359
|
-
this._equals = import_option2.Option.Some(value);
|
|
360
|
-
return this;
|
|
361
|
-
}
|
|
362
|
-
integer(flag = true, path = "integer") {
|
|
363
|
-
if (flag && this._equals.isSome() && !Number.isSafeInteger(this._equals.value))
|
|
364
|
-
throw new DefinitionError(`Expected Integer Rule to be a false due to the Equals Rule being a Float at Path ${path}`);
|
|
365
|
-
this._integer = flag ? import_option2.Option.Some(null) : import_option2.Option.None();
|
|
366
|
-
return this;
|
|
367
|
-
}
|
|
368
|
-
min(value, path = "min") {
|
|
369
|
-
if (this._max.isSome() && this._max.value < value)
|
|
370
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
371
|
-
if (this._equals.isSome() && this._equals.value < value)
|
|
372
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to the Equals Rule at Path ${path}`);
|
|
373
|
-
this._min = import_option2.Option.Some(value);
|
|
374
|
-
return this;
|
|
375
|
-
}
|
|
376
|
-
max(value, path = "max") {
|
|
377
|
-
if (this._min.isSome() && this._min.value > value)
|
|
378
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
379
|
-
if (this._equals.isSome() && this._equals.value > value)
|
|
380
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to the Equals Rule at Path ${path}`);
|
|
381
|
-
this._max = import_option2.Option.Some(value);
|
|
382
|
-
return this;
|
|
383
|
-
}
|
|
342
|
+
_equals = import_option2.Option.None();
|
|
384
343
|
assert(data, path = "data") {
|
|
385
344
|
if (import_json3.JSON.isNumber(data)) {
|
|
386
345
|
if (this._equals.isSome() && this._equals.value !== data)
|
|
@@ -428,23 +387,7 @@ var NumberValidator = class _NumberValidator extends Validator {
|
|
|
428
387
|
isEquals(other) {
|
|
429
388
|
if (!(other instanceof _NumberValidator))
|
|
430
389
|
return false;
|
|
431
|
-
|
|
432
|
-
return false;
|
|
433
|
-
if (this._integer.isSome() !== other._integer.isSome())
|
|
434
|
-
return false;
|
|
435
|
-
if (this._equals.isSome() !== other._equals.isSome())
|
|
436
|
-
return false;
|
|
437
|
-
if (this._equals.isSome() && other._equals.isSome() && this._equals.value !== other._equals.value)
|
|
438
|
-
return false;
|
|
439
|
-
if (this._min.isSome() !== other._min.isSome())
|
|
440
|
-
return false;
|
|
441
|
-
if (this._min.isSome() && other._min.isSome() && this._min.value !== other._min.value)
|
|
442
|
-
return false;
|
|
443
|
-
if (this._max.isSome() !== other._max.isSome())
|
|
444
|
-
return false;
|
|
445
|
-
if (this._max.isSome() && other._max.isSome() && this._max.value !== other._max.value)
|
|
446
|
-
return false;
|
|
447
|
-
return true;
|
|
390
|
+
return this._nullable.equals(other._nullable) && this._integer.equals(other._integer) && this._min.equals(other._min) && this._max.equals(other._max) && this._equals.equals(other._equals);
|
|
448
391
|
}
|
|
449
392
|
toJSON() {
|
|
450
393
|
const definition = {
|
|
@@ -464,7 +407,7 @@ var NumberValidator = class _NumberValidator extends Validator {
|
|
|
464
407
|
}
|
|
465
408
|
};
|
|
466
409
|
|
|
467
|
-
// src/
|
|
410
|
+
// src/Validators/String.ts
|
|
468
411
|
var import_json4 = __toESM(require_build());
|
|
469
412
|
var import_option3 = __toESM(require_Option());
|
|
470
413
|
var StringValidator = class _StringValidator extends Validator {
|
|
@@ -473,61 +416,30 @@ var StringValidator = class _StringValidator extends Validator {
|
|
|
473
416
|
if ("nullable" in definition) {
|
|
474
417
|
if (!import_json4.JSON.isBoolean(definition["nullable"]))
|
|
475
418
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
476
|
-
|
|
419
|
+
if (definition["nullable"])
|
|
420
|
+
validatorInstance._nullable = import_option3.Option.Some(null);
|
|
477
421
|
}
|
|
478
422
|
if ("equals" in definition) {
|
|
479
423
|
if (!import_json4.JSON.isString(definition["equals"]))
|
|
480
424
|
throw new DefinitionError(`Expected ${path}.equals to be a String`);
|
|
481
|
-
validatorInstance.
|
|
425
|
+
validatorInstance._equals = import_option3.Option.Some(definition["equals"]);
|
|
482
426
|
}
|
|
483
427
|
if ("min" in definition) {
|
|
484
428
|
if (!import_json4.JSON.isNumber(definition["min"]))
|
|
485
|
-
throw new DefinitionError(`Expected ${path}.min to be a
|
|
486
|
-
validatorInstance.
|
|
429
|
+
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
430
|
+
validatorInstance._min = import_option3.Option.Some(definition["min"]);
|
|
487
431
|
}
|
|
488
432
|
if ("max" in definition) {
|
|
489
433
|
if (!import_json4.JSON.isNumber(definition["max"]))
|
|
490
|
-
throw new DefinitionError(`Expected ${path}.max to be a
|
|
491
|
-
validatorInstance.
|
|
434
|
+
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
435
|
+
validatorInstance._max = import_option3.Option.Some(definition["max"]);
|
|
492
436
|
}
|
|
493
437
|
return validatorInstance;
|
|
494
438
|
}
|
|
495
439
|
_nullable = import_option3.Option.None();
|
|
496
|
-
_equals = import_option3.Option.None();
|
|
497
440
|
_min = import_option3.Option.None();
|
|
498
441
|
_max = import_option3.Option.None();
|
|
499
|
-
|
|
500
|
-
this._nullable = flag ?? true ? import_option3.Option.Some(null) : import_option3.Option.None();
|
|
501
|
-
return this;
|
|
502
|
-
}
|
|
503
|
-
equals(value, path = "equals") {
|
|
504
|
-
if (this._min.isSome() && this._min.value > value.length)
|
|
505
|
-
throw new DefinitionError(`Expected the Equals Rules Length to be larger than or equal to the Minimum Rule at Path ${path}`);
|
|
506
|
-
if (this._max.isSome() && this._max.value < value.length)
|
|
507
|
-
throw new DefinitionError(`Expected the Equals Rules Length to be smaller than or equal to the Maximum Rule at Path ${path}`);
|
|
508
|
-
this._equals = import_option3.Option.Some(value);
|
|
509
|
-
return this;
|
|
510
|
-
}
|
|
511
|
-
min(value, path = "min") {
|
|
512
|
-
if (!Number.isSafeInteger(value) || value < 0)
|
|
513
|
-
throw new DefinitionError(`Expected ${path}.min to be a positive Integer`);
|
|
514
|
-
if (this._max.isSome() && this._max.value < value)
|
|
515
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
516
|
-
if (this._equals.isSome() && this._equals.value.length < value)
|
|
517
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to the Equals Rules Length at Path ${path}`);
|
|
518
|
-
this._min = import_option3.Option.Some(value);
|
|
519
|
-
return this;
|
|
520
|
-
}
|
|
521
|
-
max(value, path = "max") {
|
|
522
|
-
if (!Number.isSafeInteger(value) || value < 0)
|
|
523
|
-
throw new DefinitionError(`Expected ${path}.max to be a positive Integer`);
|
|
524
|
-
if (this._min.isSome() && this._min.value > value)
|
|
525
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
526
|
-
if (this._equals.isSome() && this._equals.value.length > value)
|
|
527
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to the Equals Rules Length at Path ${path}`);
|
|
528
|
-
this._max = import_option3.Option.Some(value);
|
|
529
|
-
return this;
|
|
530
|
-
}
|
|
442
|
+
_equals = import_option3.Option.None();
|
|
531
443
|
assert(data, path = "data") {
|
|
532
444
|
if (import_json4.JSON.isString(data)) {
|
|
533
445
|
if (this._equals.isSome() && this._equals.value !== data)
|
|
@@ -569,21 +481,7 @@ var StringValidator = class _StringValidator extends Validator {
|
|
|
569
481
|
isEquals(other) {
|
|
570
482
|
if (!(other instanceof _StringValidator))
|
|
571
483
|
return false;
|
|
572
|
-
|
|
573
|
-
return false;
|
|
574
|
-
if (this._equals.isSome() !== other._equals.isSome())
|
|
575
|
-
return false;
|
|
576
|
-
if (this._equals.isSome() && other._equals.isSome() && this._equals.value !== other._equals.value)
|
|
577
|
-
return false;
|
|
578
|
-
if (this._min.isSome() !== other._min.isSome())
|
|
579
|
-
return false;
|
|
580
|
-
if (this._min.isSome() && other._min.isSome() && this._min.value !== other._min.value)
|
|
581
|
-
return false;
|
|
582
|
-
if (this._max.isSome() !== other._max.isSome())
|
|
583
|
-
return false;
|
|
584
|
-
if (this._max.isSome() && other._max.isSome() && this._max.value !== other._max.value)
|
|
585
|
-
return false;
|
|
586
|
-
return true;
|
|
484
|
+
return this._nullable.equals(other._nullable) && this._min.equals(other._min) && this._max.equals(other._max) && this._equals.equals(other._equals);
|
|
587
485
|
}
|
|
588
486
|
toJSON() {
|
|
589
487
|
const definition = {
|
|
@@ -601,41 +499,199 @@ var StringValidator = class _StringValidator extends Validator {
|
|
|
601
499
|
}
|
|
602
500
|
};
|
|
603
501
|
|
|
604
|
-
// src/
|
|
605
|
-
var import_json6 = __toESM(require_build());
|
|
606
|
-
var import_option5 = __toESM(require_Option());
|
|
607
|
-
|
|
608
|
-
// src/validators/Object.ts
|
|
502
|
+
// src/Validators/Array.ts
|
|
609
503
|
var import_json5 = __toESM(require_build());
|
|
610
504
|
var import_option4 = __toESM(require_Option());
|
|
505
|
+
var ArrayValidator = class _ArrayValidator extends Validator {
|
|
506
|
+
static fromJSON(definition, path = "definition") {
|
|
507
|
+
const validatorInstance = new _ArrayValidator();
|
|
508
|
+
if ("nullable" in definition) {
|
|
509
|
+
if (!import_json5.JSON.isBoolean(definition["nullable"]))
|
|
510
|
+
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
511
|
+
if (definition["nullable"])
|
|
512
|
+
validatorInstance._nullable = import_option4.Option.Some(null);
|
|
513
|
+
}
|
|
514
|
+
if ("min" in definition) {
|
|
515
|
+
if (!import_json5.JSON.isNumber(definition["min"]))
|
|
516
|
+
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
517
|
+
validatorInstance._min = import_option4.Option.Some(definition["min"]);
|
|
518
|
+
}
|
|
519
|
+
if ("max" in definition) {
|
|
520
|
+
if (!import_json5.JSON.isNumber(definition["max"]))
|
|
521
|
+
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
522
|
+
validatorInstance._max = import_option4.Option.Some(definition["max"]);
|
|
523
|
+
}
|
|
524
|
+
if ("every" in definition)
|
|
525
|
+
validatorInstance._every = import_option4.Option.Some(fromJSON(definition["every"], `${path}.every`));
|
|
526
|
+
if ("tuple" in definition) {
|
|
527
|
+
if (!import_json5.JSON.isShallowArray(definition["tuple"]))
|
|
528
|
+
throw new DefinitionError(`Expected ${path}.tuple to be an Array`);
|
|
529
|
+
const tupleSchemas = [];
|
|
530
|
+
const errors = [];
|
|
531
|
+
definition["tuple"].forEach((tupleDef, index) => {
|
|
532
|
+
try {
|
|
533
|
+
tupleSchemas.push(fromJSON(tupleDef, `${path}.tuple[${index}]`));
|
|
534
|
+
} catch (e) {
|
|
535
|
+
if (!(e instanceof DefinitionError))
|
|
536
|
+
throw new DefinitionError(`Fatal Error: Undefined Error thrown by Domain.fromJSON at ${path}`);
|
|
537
|
+
errors.push(e);
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
if (errors.length > 0)
|
|
541
|
+
throw new DefinitionError(`Multiple Definition Errors detected at ${path} (see cause)`, { cause: errors });
|
|
542
|
+
validatorInstance._tuple = import_option4.Option.Some(tupleSchemas);
|
|
543
|
+
}
|
|
544
|
+
return validatorInstance;
|
|
545
|
+
}
|
|
546
|
+
_nullable = import_option4.Option.None();
|
|
547
|
+
_every = import_option4.Option.None();
|
|
548
|
+
_tuple = import_option4.Option.None();
|
|
549
|
+
_min = import_option4.Option.None();
|
|
550
|
+
_max = import_option4.Option.None();
|
|
551
|
+
assert(data, path = "data") {
|
|
552
|
+
if (import_json5.JSON.isShallowArray(data)) {
|
|
553
|
+
if (this._min.isSome() && this._min.value > data.length)
|
|
554
|
+
throw new AssertError(`Expected ${path} to be at least ${this._min.value} Elements long`);
|
|
555
|
+
if (this._max.isSome() && this._max.value < data.length)
|
|
556
|
+
throw new AssertError(`Expected ${path} to be at most ${this._max.value} Elements long`);
|
|
557
|
+
const errors = [];
|
|
558
|
+
if (this._every.isSome()) {
|
|
559
|
+
const validator = this._every.value;
|
|
560
|
+
data.forEach((value, index) => {
|
|
561
|
+
try {
|
|
562
|
+
validator.assert(value, `${path}[${index}]`);
|
|
563
|
+
} catch (e) {
|
|
564
|
+
if (!(e instanceof AssertError))
|
|
565
|
+
throw new AssertError(`Fatal Error: Undefined Error thrown by an Assert Method at ${path}[${index}]`);
|
|
566
|
+
errors.push(e);
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
if (this._tuple.isSome()) {
|
|
571
|
+
if (data.length < this._tuple.value.length)
|
|
572
|
+
throw new AssertError(`Expected ${path} to be at least ${this._tuple.value.length} Elements long (Tuple Constraint)`);
|
|
573
|
+
this._tuple.value.forEach((validator, index) => {
|
|
574
|
+
try {
|
|
575
|
+
validator.assert(data[index], `${path}[${index}]`);
|
|
576
|
+
} catch (e) {
|
|
577
|
+
if (!(e instanceof AssertError))
|
|
578
|
+
throw new AssertError(`Fatal Error: Undefined Error thrown by an Assert Method at ${path}[${index}]`);
|
|
579
|
+
errors.push(e);
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
if (errors.length > 0)
|
|
584
|
+
throw new AssertError(`Multiple Errors while asserting ${path} (see cause)`, { cause: errors });
|
|
585
|
+
} else if (import_json5.JSON.isNull(data)) {
|
|
586
|
+
if (!this._nullable.isSome())
|
|
587
|
+
throw new AssertError(`Expected ${path} to be an Array${this._nullable.isSome() ? " or Null" : ""}`);
|
|
588
|
+
} else
|
|
589
|
+
throw new AssertError(`Expected ${path} to be an Array${this._nullable.isSome() ? " or Null" : ""}`);
|
|
590
|
+
}
|
|
591
|
+
isSubset(other) {
|
|
592
|
+
if (other instanceof JSONValidator)
|
|
593
|
+
return true;
|
|
594
|
+
if (!(other instanceof _ArrayValidator))
|
|
595
|
+
return false;
|
|
596
|
+
if (this._nullable.isSome() && !other._nullable.isSome())
|
|
597
|
+
return false;
|
|
598
|
+
const thisTupleLen = this._tuple.isSome() ? this._tuple.value.length : 0;
|
|
599
|
+
const otherTupleLen = other._tuple.isSome() ? other._tuple.value.length : 0;
|
|
600
|
+
const thisMin = this._min.isSome() ? this._min.value : 0;
|
|
601
|
+
const otherMin = other._min.isSome() ? other._min.value : 0;
|
|
602
|
+
const thisMax = this._max.isSome() ? this._max.value : Infinity;
|
|
603
|
+
const otherMax = other._max.isSome() ? other._max.value : Infinity;
|
|
604
|
+
const thisEffectiveMin = Math.max(thisMin, thisTupleLen);
|
|
605
|
+
const otherEffectiveMin = Math.max(otherMin, otherTupleLen);
|
|
606
|
+
if (thisEffectiveMin < otherEffectiveMin)
|
|
607
|
+
return false;
|
|
608
|
+
if (thisMax > otherMax)
|
|
609
|
+
return false;
|
|
610
|
+
const indexConjuncts = (i) => {
|
|
611
|
+
const conjuncts = [];
|
|
612
|
+
if (this._every.isSome())
|
|
613
|
+
conjuncts.push(this._every.value);
|
|
614
|
+
if (this._tuple.isSome() && i < this._tuple.value.length)
|
|
615
|
+
conjuncts.push(this._tuple.value[i]);
|
|
616
|
+
return conjuncts;
|
|
617
|
+
};
|
|
618
|
+
const conjunctsSubset = (conjuncts, target) => {
|
|
619
|
+
if (conjuncts.length === 0)
|
|
620
|
+
return false;
|
|
621
|
+
return conjuncts.some((c) => c.isSubset(target));
|
|
622
|
+
};
|
|
623
|
+
if (other._tuple.isSome()) {
|
|
624
|
+
for (let i = 0; i < other._tuple.value.length; i++) {
|
|
625
|
+
const aConj = indexConjuncts(i);
|
|
626
|
+
if (!conjunctsSubset(aConj, other._tuple.value[i]))
|
|
627
|
+
return false;
|
|
628
|
+
if (other._every.isSome() && !conjunctsSubset(aConj, other._every.value))
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return !(other._every.isSome() && (thisMax === Infinity ? true : thisMax > Math.max(thisTupleLen, otherTupleLen)) && (!this._every.isSome() || !this._every.value.isSubset(other._every.value)));
|
|
633
|
+
}
|
|
634
|
+
isEquals(other) {
|
|
635
|
+
if (!(other instanceof _ArrayValidator))
|
|
636
|
+
return false;
|
|
637
|
+
return this._nullable.equals(other._nullable) && this._min.equals(other._min) && this._max.equals(other._max) && this._every.equals(other._every, (a, b) => a.isEquals(b)) && this._tuple.equals(other._tuple, (a, b) => {
|
|
638
|
+
if (a.length !== b.length)
|
|
639
|
+
return false;
|
|
640
|
+
for (let i = 0; i < a.length; i++) {
|
|
641
|
+
if (!a[i].isEquals(b[i]))
|
|
642
|
+
return false;
|
|
643
|
+
}
|
|
644
|
+
return true;
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
toJSON() {
|
|
648
|
+
const definition = { type: "array" };
|
|
649
|
+
if (this._nullable.isSome())
|
|
650
|
+
definition.nullable = true;
|
|
651
|
+
if (this._min.isSome())
|
|
652
|
+
definition.min = this._min.value;
|
|
653
|
+
if (this._max.isSome())
|
|
654
|
+
definition.max = this._max.value;
|
|
655
|
+
if (this._every.isSome())
|
|
656
|
+
definition.every = this._every.value.toJSON();
|
|
657
|
+
if (this._tuple.isSome())
|
|
658
|
+
definition.tuple = this._tuple.value.map((validator) => validator.toJSON());
|
|
659
|
+
return definition;
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
// src/Validators/Object.ts
|
|
664
|
+
var import_json6 = __toESM(require_build());
|
|
665
|
+
var import_option5 = __toESM(require_Option());
|
|
611
666
|
var ObjectValidator = class _ObjectValidator extends Validator {
|
|
612
667
|
static fromJSON(definition, path = "definition") {
|
|
613
668
|
const validatorInstance = new _ObjectValidator();
|
|
614
669
|
if ("nullable" in definition) {
|
|
615
|
-
if (!
|
|
670
|
+
if (!import_json6.JSON.isBoolean(definition["nullable"]))
|
|
616
671
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
617
|
-
|
|
672
|
+
if (definition["nullable"])
|
|
673
|
+
validatorInstance._nullable = import_option5.Option.Some(null);
|
|
618
674
|
}
|
|
619
675
|
if ("exact" in definition) {
|
|
620
|
-
if (!
|
|
676
|
+
if (!import_json6.JSON.isBoolean(definition["exact"]))
|
|
621
677
|
throw new DefinitionError(`Expected ${path}.exact to be a Boolean`);
|
|
622
|
-
|
|
678
|
+
if (definition["exact"])
|
|
679
|
+
validatorInstance._exact = import_option5.Option.Some(null);
|
|
623
680
|
}
|
|
624
681
|
if ("min" in definition) {
|
|
625
|
-
if (!
|
|
682
|
+
if (!import_json6.JSON.isNumber(definition["min"]))
|
|
626
683
|
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
627
|
-
validatorInstance.
|
|
684
|
+
validatorInstance._min = import_option5.Option.Some(definition["min"]);
|
|
628
685
|
}
|
|
629
686
|
if ("max" in definition) {
|
|
630
|
-
if (!
|
|
687
|
+
if (!import_json6.JSON.isNumber(definition["max"]))
|
|
631
688
|
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
632
|
-
validatorInstance.
|
|
633
|
-
}
|
|
634
|
-
if ("every" in definition) {
|
|
635
|
-
validatorInstance.every(fromJSON(definition["every"], `${path}.every`));
|
|
689
|
+
validatorInstance._max = import_option5.Option.Some(definition["max"]);
|
|
636
690
|
}
|
|
691
|
+
if ("every" in definition)
|
|
692
|
+
validatorInstance._every = import_option5.Option.Some(fromJSON(definition["every"], `${path}.every`));
|
|
637
693
|
if ("shape" in definition) {
|
|
638
|
-
if (!
|
|
694
|
+
if (!import_json6.JSON.isShallowObject(definition["shape"]))
|
|
639
695
|
throw new DefinitionError(`Expected ${path}.shape to be an Object`);
|
|
640
696
|
const shape = {};
|
|
641
697
|
const errors = [];
|
|
@@ -651,68 +707,18 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
651
707
|
if (errors.length > 0) {
|
|
652
708
|
throw new DefinitionError(`Multiple Definition Errors detected at ${path}.shape (see cause)`, { cause: errors });
|
|
653
709
|
}
|
|
654
|
-
validatorInstance.
|
|
710
|
+
validatorInstance._shape = import_option5.Option.Some(shape);
|
|
655
711
|
}
|
|
656
712
|
return validatorInstance;
|
|
657
713
|
}
|
|
658
|
-
_nullable =
|
|
659
|
-
_min =
|
|
660
|
-
_max =
|
|
661
|
-
_every =
|
|
662
|
-
_shape =
|
|
663
|
-
_exact =
|
|
664
|
-
nullable(flag) {
|
|
665
|
-
this._nullable = flag ?? true ? import_option4.Option.Some(null) : import_option4.Option.None();
|
|
666
|
-
return this;
|
|
667
|
-
}
|
|
668
|
-
min(value, path = "min") {
|
|
669
|
-
if (this._max.isSome() && this._max.value < value)
|
|
670
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
671
|
-
if (this._shape.isSome() && value < Object.keys(this._shape.value).length)
|
|
672
|
-
throw new DefinitionError(`Expected Minimum Rule to be larger than or equal to Shape Key Count at Path ${path}`);
|
|
673
|
-
if (this._exact.isSome() && this._shape.isSome() && value > Object.keys(this._shape.value).length)
|
|
674
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Shape Key Count due to Exact Rule at Path ${path}`);
|
|
675
|
-
this._min = import_option4.Option.Some(value);
|
|
676
|
-
return this;
|
|
677
|
-
}
|
|
678
|
-
max(value, path = "max") {
|
|
679
|
-
if (this._min.isSome() && this._min.value > value)
|
|
680
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
681
|
-
if (this._shape.isSome() && value < Object.keys(this._shape.value).length)
|
|
682
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Shape Key Count at Path ${path}`);
|
|
683
|
-
this._max = import_option4.Option.Some(value);
|
|
684
|
-
return this;
|
|
685
|
-
}
|
|
686
|
-
every(validator) {
|
|
687
|
-
this._every = import_option4.Option.Some(validator);
|
|
688
|
-
return this;
|
|
689
|
-
}
|
|
690
|
-
shape(value, path = "shape") {
|
|
691
|
-
const shapeKeyCount = Object.keys(value).length;
|
|
692
|
-
if (this._min.isSome() && this._min.value < shapeKeyCount)
|
|
693
|
-
throw new DefinitionError(`Expected Shape Key Count to be smaller than or equal to Minimum Rule at Path ${path}`);
|
|
694
|
-
if (this._max.isSome() && this._max.value < shapeKeyCount)
|
|
695
|
-
throw new DefinitionError(`Expected Shape Key Count to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
696
|
-
if (this._exact.isSome() && this._min.isSome() && this._min.value > shapeKeyCount)
|
|
697
|
-
throw new DefinitionError(`Expected Shape Key Count to be larger than or equal to Minimum Rule due to Exact Rule at Path ${path}`);
|
|
698
|
-
if (this._exact.isSome() && this._max.isSome() && this._max.value < shapeKeyCount)
|
|
699
|
-
throw new DefinitionError(`Expected Shape Key Count to be smaller than or equal to Maximum Rule due to Exact Rule at Path ${path}`);
|
|
700
|
-
this._shape = import_option4.Option.Some(value);
|
|
701
|
-
return this;
|
|
702
|
-
}
|
|
703
|
-
exact(flag, path = "exact") {
|
|
704
|
-
if ((flag ?? true) && this._shape.isSome()) {
|
|
705
|
-
const shapeKeyCount = Object.keys(this._shape.value).length;
|
|
706
|
-
if (this._min.isSome() && this._min.value > shapeKeyCount)
|
|
707
|
-
throw new DefinitionError(`Expected Exact Rule to be false due to Minimum Rule requiring more Properties than Shape defines at Path ${path}`);
|
|
708
|
-
if (this._max.isSome() && this._max.value < shapeKeyCount)
|
|
709
|
-
throw new DefinitionError(`Expected Exact Rule to be false due to Maximum Rule allowing fewer Properties than Shape defines at Path ${path}`);
|
|
710
|
-
}
|
|
711
|
-
this._exact = flag ?? true ? import_option4.Option.Some(null) : import_option4.Option.None();
|
|
712
|
-
return this;
|
|
713
|
-
}
|
|
714
|
+
_nullable = import_option5.Option.None();
|
|
715
|
+
_min = import_option5.Option.None();
|
|
716
|
+
_max = import_option5.Option.None();
|
|
717
|
+
_every = import_option5.Option.None();
|
|
718
|
+
_shape = import_option5.Option.None();
|
|
719
|
+
_exact = import_option5.Option.None();
|
|
714
720
|
assert(data, path = "data") {
|
|
715
|
-
if (
|
|
721
|
+
if (import_json6.JSON.isShallowObject(data)) {
|
|
716
722
|
const keys = Object.keys(data);
|
|
717
723
|
if (this._min.isSome() && keys.length < this._min.value)
|
|
718
724
|
throw new AssertError(`Expected ${path} to have at least ${this._min.value} Properties`);
|
|
@@ -752,7 +758,7 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
752
758
|
}
|
|
753
759
|
if (errors.length > 0)
|
|
754
760
|
throw new AssertError(`Multiple Errors while asserting ${path} (see cause)`, { cause: errors });
|
|
755
|
-
} else if (
|
|
761
|
+
} else if (import_json6.JSON.isNull(data)) {
|
|
756
762
|
if (!this._nullable.isSome())
|
|
757
763
|
throw new AssertError(`Expected ${path} to be an Object${this._nullable.isSome() ? " or Null" : ""}`);
|
|
758
764
|
} else
|
|
@@ -827,35 +833,17 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
827
833
|
isEquals(other) {
|
|
828
834
|
if (!(other instanceof _ObjectValidator))
|
|
829
835
|
return false;
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
if (this._min.isSome() && other._min.isSome() && this._min.value !== other._min.value)
|
|
835
|
-
return false;
|
|
836
|
-
if (this._max.isSome() !== other._max.isSome())
|
|
837
|
-
return false;
|
|
838
|
-
if (this._max.isSome() && other._max.isSome() && this._max.value !== other._max.value)
|
|
839
|
-
return false;
|
|
840
|
-
if (this._every.isSome() !== other._every.isSome())
|
|
841
|
-
return false;
|
|
842
|
-
if (this._every.isSome() && other._every.isSome())
|
|
843
|
-
return isEquals(this._every.value, other._every.value);
|
|
844
|
-
if (this._shape.isSome() !== other._shape.isSome())
|
|
845
|
-
return false;
|
|
846
|
-
if (this._shape.isSome() && other._shape.isSome()) {
|
|
847
|
-
if (Object.keys(this._shape.value).length !== Object.keys(other._shape.value).length)
|
|
836
|
+
return this._nullable.equals(other._nullable) && this._min.equals(other._min) && this._max.equals(other._max) && this._exact.equals(other._exact) && this._every.equals(other._every, (a, b) => a.isEquals(b)) && this._shape.equals(other._shape, (a, b) => {
|
|
837
|
+
const keysA = Object.keys(a);
|
|
838
|
+
const keysB = Object.keys(b);
|
|
839
|
+
if (keysA.length !== keysB.length)
|
|
848
840
|
return false;
|
|
849
|
-
for (const
|
|
850
|
-
if (!(
|
|
851
|
-
return false;
|
|
852
|
-
if (!isEquals(v, other._shape.value[k]))
|
|
841
|
+
for (const key of keysA) {
|
|
842
|
+
if (!(key in b) || !a[key]?.isEquals(b[key]))
|
|
853
843
|
return false;
|
|
854
844
|
}
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
return false;
|
|
858
|
-
return true;
|
|
845
|
+
return true;
|
|
846
|
+
});
|
|
859
847
|
}
|
|
860
848
|
toJSON() {
|
|
861
849
|
const schema = { type: "object" };
|
|
@@ -878,238 +866,6 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
878
866
|
}
|
|
879
867
|
};
|
|
880
868
|
|
|
881
|
-
// src/lib/isEquals.ts
|
|
882
|
-
function isEquals(a, b) {
|
|
883
|
-
if (a instanceof JSONValidator)
|
|
884
|
-
return a.isEquals(b);
|
|
885
|
-
if (a instanceof BooleanValidator)
|
|
886
|
-
return a.isEquals(b);
|
|
887
|
-
if (a instanceof NumberValidator)
|
|
888
|
-
return a.isEquals(b);
|
|
889
|
-
if (a instanceof StringValidator)
|
|
890
|
-
return a.isEquals(b);
|
|
891
|
-
if (a instanceof ArrayValidator)
|
|
892
|
-
return a.isEquals(b);
|
|
893
|
-
if (a instanceof ObjectValidator)
|
|
894
|
-
return a.isEquals(b);
|
|
895
|
-
return false;
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
// src/validators/Array.ts
|
|
899
|
-
var ArrayValidator = class _ArrayValidator extends Validator {
|
|
900
|
-
static fromJSON(definition, path = "definition") {
|
|
901
|
-
const validatorInstance = new _ArrayValidator();
|
|
902
|
-
if ("nullable" in definition) {
|
|
903
|
-
if (!import_json6.JSON.isBoolean(definition["nullable"]))
|
|
904
|
-
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
905
|
-
validatorInstance.nullable(definition["nullable"]);
|
|
906
|
-
}
|
|
907
|
-
if ("min" in definition) {
|
|
908
|
-
if (!import_json6.JSON.isNumber(definition["min"]))
|
|
909
|
-
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
910
|
-
validatorInstance.min(definition["min"], `${path}.min`);
|
|
911
|
-
}
|
|
912
|
-
if ("max" in definition) {
|
|
913
|
-
if (!import_json6.JSON.isNumber(definition["max"]))
|
|
914
|
-
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
915
|
-
validatorInstance.max(definition["max"], `${path}.max`);
|
|
916
|
-
}
|
|
917
|
-
if ("every" in definition) {
|
|
918
|
-
if (!import_json6.JSON.isObject(definition["every"]))
|
|
919
|
-
throw new DefinitionError(`Expected ${path}.every to be an Object`);
|
|
920
|
-
validatorInstance.every(
|
|
921
|
-
fromJSON(definition["every"], `${path}.every`)
|
|
922
|
-
);
|
|
923
|
-
}
|
|
924
|
-
if ("tuple" in definition) {
|
|
925
|
-
if (!import_json6.JSON.isShallowArray(definition["tuple"]))
|
|
926
|
-
throw new DefinitionError(`Expected ${path}.tuple to be an Array`);
|
|
927
|
-
const tupleSchemas = [];
|
|
928
|
-
const errors = [];
|
|
929
|
-
definition["tuple"].forEach((tupleDef, index) => {
|
|
930
|
-
try {
|
|
931
|
-
tupleSchemas.push(fromJSON(tupleDef, `${path}.tuple[${index}]`));
|
|
932
|
-
} catch (e) {
|
|
933
|
-
if (!(e instanceof DefinitionError))
|
|
934
|
-
throw new DefinitionError(`Fatal Error: Undefined Error thrown by Domain.fromJSON at ${path}`);
|
|
935
|
-
errors.push(e);
|
|
936
|
-
}
|
|
937
|
-
});
|
|
938
|
-
if (errors.length > 0)
|
|
939
|
-
throw new DefinitionError(`Multiple Definition Errors detected at ${path} (see cause)`, { cause: errors });
|
|
940
|
-
validatorInstance.tuple(tupleSchemas, `${path}.tuple`);
|
|
941
|
-
}
|
|
942
|
-
return validatorInstance;
|
|
943
|
-
}
|
|
944
|
-
_nullable = import_option5.Option.None();
|
|
945
|
-
_every = import_option5.Option.None();
|
|
946
|
-
_tuple = import_option5.Option.None();
|
|
947
|
-
_min = import_option5.Option.None();
|
|
948
|
-
_max = import_option5.Option.None();
|
|
949
|
-
nullable(flag) {
|
|
950
|
-
this._nullable = flag ?? true ? import_option5.Option.Some(null) : import_option5.Option.None();
|
|
951
|
-
return this;
|
|
952
|
-
}
|
|
953
|
-
min(value, path = "min") {
|
|
954
|
-
if (this._max.isSome() && this._max.value < value)
|
|
955
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
956
|
-
if (this._tuple.isSome() && value < this._tuple.value.length)
|
|
957
|
-
throw new DefinitionError(`Expected Minimum Rule to be larger than or equal to Tuple Length at Path ${path}`);
|
|
958
|
-
this._min = import_option5.Option.Some(value);
|
|
959
|
-
return this;
|
|
960
|
-
}
|
|
961
|
-
max(value, path = "max") {
|
|
962
|
-
if (this._min.isSome() && this._min.value > value)
|
|
963
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
964
|
-
if (this._tuple.isSome() && value < this._tuple.value.length)
|
|
965
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Tuple Length at Path ${path}`);
|
|
966
|
-
this._max = import_option5.Option.Some(value);
|
|
967
|
-
return this;
|
|
968
|
-
}
|
|
969
|
-
every(validator) {
|
|
970
|
-
this._every = import_option5.Option.Some(validator);
|
|
971
|
-
return this;
|
|
972
|
-
}
|
|
973
|
-
/**
|
|
974
|
-
* Applies ONLY to prefix indices [0..validators.length - 1]
|
|
975
|
-
* If every() is set, prefix elements are effectively `T[i] & E`.
|
|
976
|
-
*/
|
|
977
|
-
tuple(validators, path = "tuple") {
|
|
978
|
-
if (this._min.isSome() && this._min.value < validators.length)
|
|
979
|
-
throw new DefinitionError(`Expected Tuple Length to be smaller than or equal to Minimum Rule at Path ${path}`);
|
|
980
|
-
if (this._max.isSome() && this._max.value < validators.length)
|
|
981
|
-
throw new DefinitionError(`Expected Tuple Length to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
982
|
-
this._tuple = import_option5.Option.Some(validators);
|
|
983
|
-
return this;
|
|
984
|
-
}
|
|
985
|
-
assert(data, path = "data") {
|
|
986
|
-
if (import_json6.JSON.isShallowArray(data)) {
|
|
987
|
-
if (this._min.isSome() && this._min.value > data.length)
|
|
988
|
-
throw new AssertError(`Expected ${path} to be at least ${this._min.value} Elements long`);
|
|
989
|
-
if (this._max.isSome() && this._max.value < data.length)
|
|
990
|
-
throw new AssertError(`Expected ${path} to be at most ${this._max.value} Elements long`);
|
|
991
|
-
const errors = [];
|
|
992
|
-
if (this._every.isSome()) {
|
|
993
|
-
const validator = this._every.value;
|
|
994
|
-
data.forEach((value, index) => {
|
|
995
|
-
try {
|
|
996
|
-
validator.assert(value, `${path}[${index}]`);
|
|
997
|
-
} catch (e) {
|
|
998
|
-
if (!(e instanceof AssertError))
|
|
999
|
-
throw new AssertError(`Fatal Error: Undefined Error thrown by an Assert Method at ${path}[${index}]`);
|
|
1000
|
-
errors.push(e);
|
|
1001
|
-
}
|
|
1002
|
-
});
|
|
1003
|
-
}
|
|
1004
|
-
if (this._tuple.isSome()) {
|
|
1005
|
-
if (data.length < this._tuple.value.length)
|
|
1006
|
-
throw new AssertError(`Expected ${path} to be at least ${this._tuple.value.length} Elements long (Tuple Constraint)`);
|
|
1007
|
-
this._tuple.value.forEach((validator, index) => {
|
|
1008
|
-
try {
|
|
1009
|
-
validator.assert(data[index], `${path}[${index}]`);
|
|
1010
|
-
} catch (e) {
|
|
1011
|
-
if (!(e instanceof AssertError))
|
|
1012
|
-
throw new AssertError(`Fatal Error: Undefined Error thrown by an Assert Method at ${path}[${index}]`);
|
|
1013
|
-
errors.push(e);
|
|
1014
|
-
}
|
|
1015
|
-
});
|
|
1016
|
-
}
|
|
1017
|
-
if (errors.length > 0)
|
|
1018
|
-
throw new AssertError(`Multiple Errors while asserting ${path} (see cause)`, { cause: errors });
|
|
1019
|
-
} else if (import_json6.JSON.isNull(data)) {
|
|
1020
|
-
if (!this._nullable.isSome())
|
|
1021
|
-
throw new AssertError(`Expected ${path} to be an Array${this._nullable.isSome() ? " or Null" : ""}`);
|
|
1022
|
-
} else
|
|
1023
|
-
throw new AssertError(`Expected ${path} to be an Array${this._nullable.isSome() ? " or Null" : ""}`);
|
|
1024
|
-
}
|
|
1025
|
-
isSubset(other) {
|
|
1026
|
-
if (other instanceof JSONValidator)
|
|
1027
|
-
return true;
|
|
1028
|
-
if (!(other instanceof _ArrayValidator))
|
|
1029
|
-
return false;
|
|
1030
|
-
if (this._nullable.isSome() && !other._nullable.isSome())
|
|
1031
|
-
return false;
|
|
1032
|
-
const thisTupleLen = this._tuple.isSome() ? this._tuple.value.length : 0;
|
|
1033
|
-
const otherTupleLen = other._tuple.isSome() ? other._tuple.value.length : 0;
|
|
1034
|
-
const thisMin = this._min.isSome() ? this._min.value : 0;
|
|
1035
|
-
const otherMin = other._min.isSome() ? other._min.value : 0;
|
|
1036
|
-
const thisMax = this._max.isSome() ? this._max.value : Infinity;
|
|
1037
|
-
const otherMax = other._max.isSome() ? other._max.value : Infinity;
|
|
1038
|
-
const thisEffectiveMin = Math.max(thisMin, thisTupleLen);
|
|
1039
|
-
const otherEffectiveMin = Math.max(otherMin, otherTupleLen);
|
|
1040
|
-
if (thisEffectiveMin < otherEffectiveMin)
|
|
1041
|
-
return false;
|
|
1042
|
-
if (thisMax > otherMax)
|
|
1043
|
-
return false;
|
|
1044
|
-
const indexConjuncts = (i) => {
|
|
1045
|
-
const conjuncts = [];
|
|
1046
|
-
if (this._every.isSome())
|
|
1047
|
-
conjuncts.push(this._every.value);
|
|
1048
|
-
if (this._tuple.isSome() && i < this._tuple.value.length)
|
|
1049
|
-
conjuncts.push(this._tuple.value[i]);
|
|
1050
|
-
return conjuncts;
|
|
1051
|
-
};
|
|
1052
|
-
const conjunctsSubset = (conjuncts, target) => {
|
|
1053
|
-
if (conjuncts.length === 0)
|
|
1054
|
-
return false;
|
|
1055
|
-
return conjuncts.some((c) => c.isSubset(target));
|
|
1056
|
-
};
|
|
1057
|
-
if (other._tuple.isSome()) {
|
|
1058
|
-
for (let i = 0; i < other._tuple.value.length; i++) {
|
|
1059
|
-
const aConj = indexConjuncts(i);
|
|
1060
|
-
if (!conjunctsSubset(aConj, other._tuple.value[i]))
|
|
1061
|
-
return false;
|
|
1062
|
-
if (other._every.isSome() && !conjunctsSubset(aConj, other._every.value))
|
|
1063
|
-
return false;
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
return !(other._every.isSome() && (thisMax === Infinity ? true : thisMax > Math.max(thisTupleLen, otherTupleLen)) && (!this._every.isSome() || !this._every.value.isSubset(other._every.value)));
|
|
1067
|
-
}
|
|
1068
|
-
isEquals(other) {
|
|
1069
|
-
if (!(other instanceof _ArrayValidator))
|
|
1070
|
-
return false;
|
|
1071
|
-
if (this._nullable.isSome() !== other._nullable.isSome())
|
|
1072
|
-
return false;
|
|
1073
|
-
if (this._min.isSome() !== other._min.isSome())
|
|
1074
|
-
return false;
|
|
1075
|
-
if (this._min.isSome() && other._min.isSome() && this._min.value !== other._min.value)
|
|
1076
|
-
return false;
|
|
1077
|
-
if (this._max.isSome() !== other._max.isSome())
|
|
1078
|
-
return false;
|
|
1079
|
-
if (this._max.isSome() && other._max.isSome() && this._max.value !== other._max.value)
|
|
1080
|
-
return false;
|
|
1081
|
-
if (this._every.isSome() !== other._every.isSome())
|
|
1082
|
-
return false;
|
|
1083
|
-
if (this._every.isSome() && other._every.isSome() && !isEquals(this._every.value, other._every.value))
|
|
1084
|
-
return false;
|
|
1085
|
-
if (this._tuple.isSome() !== other._tuple.isSome())
|
|
1086
|
-
return false;
|
|
1087
|
-
if (this._tuple.isSome() && other._tuple.isSome()) {
|
|
1088
|
-
if (this._tuple.value.length !== other._tuple.value.length)
|
|
1089
|
-
return false;
|
|
1090
|
-
for (let i = 0; i < this._tuple.value.length; i++) {
|
|
1091
|
-
if (!isEquals(this._tuple.value[i], other._tuple.value[i]))
|
|
1092
|
-
return false;
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
return true;
|
|
1096
|
-
}
|
|
1097
|
-
toJSON() {
|
|
1098
|
-
const definition = { type: "array" };
|
|
1099
|
-
if (this._nullable.isSome())
|
|
1100
|
-
definition.nullable = true;
|
|
1101
|
-
if (this._min.isSome())
|
|
1102
|
-
definition.min = this._min.value;
|
|
1103
|
-
if (this._max.isSome())
|
|
1104
|
-
definition.max = this._max.value;
|
|
1105
|
-
if (this._every.isSome())
|
|
1106
|
-
definition.every = this._every.value.toJSON();
|
|
1107
|
-
if (this._tuple.isSome())
|
|
1108
|
-
definition.tuple = this._tuple.value.map((validator) => validator.toJSON());
|
|
1109
|
-
return definition;
|
|
1110
|
-
}
|
|
1111
|
-
};
|
|
1112
|
-
|
|
1113
869
|
// src/lib/fromJSON.ts
|
|
1114
870
|
function fromJSON(definition, path) {
|
|
1115
871
|
if (!import_json7.JSON.isShallowObject(definition))
|
|
@@ -1134,31 +890,165 @@ function fromJSON(definition, path) {
|
|
|
1134
890
|
}
|
|
1135
891
|
}
|
|
1136
892
|
|
|
893
|
+
// src/Builders/JSON.ts
|
|
894
|
+
var JSONBuilder = class extends JSONValidator {
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
// src/Builders/Boolean.ts
|
|
898
|
+
var import_option6 = __toESM(require_Option());
|
|
899
|
+
var BooleanBuilder = class extends BooleanValidator {
|
|
900
|
+
nullable(flag) {
|
|
901
|
+
this._nullable = flag ?? true ? import_option6.Option.Some(null) : import_option6.Option.None();
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
equals(value) {
|
|
905
|
+
this._equals = import_option6.Option.Some(value);
|
|
906
|
+
return this;
|
|
907
|
+
}
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
// src/Builders/Number.ts
|
|
911
|
+
var import_option7 = __toESM(require_Option());
|
|
912
|
+
var NumberBuilder = class extends NumberValidator {
|
|
913
|
+
nullable(flag) {
|
|
914
|
+
this._nullable = flag ?? true ? import_option7.Option.Some(null) : import_option7.Option.None();
|
|
915
|
+
return this;
|
|
916
|
+
}
|
|
917
|
+
equals(value) {
|
|
918
|
+
this._equals = import_option7.Option.Some(value);
|
|
919
|
+
return this;
|
|
920
|
+
}
|
|
921
|
+
integer(flag = true) {
|
|
922
|
+
this._integer = flag ? import_option7.Option.Some(null) : import_option7.Option.None();
|
|
923
|
+
return this;
|
|
924
|
+
}
|
|
925
|
+
min(value) {
|
|
926
|
+
this._min = import_option7.Option.Some(value);
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
max(value) {
|
|
930
|
+
this._max = import_option7.Option.Some(value);
|
|
931
|
+
return this;
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
|
|
935
|
+
// src/Builders/String.ts
|
|
936
|
+
var import_option8 = __toESM(require_Option());
|
|
937
|
+
var StringBuilder = class extends StringValidator {
|
|
938
|
+
nullable(flag) {
|
|
939
|
+
this._nullable = flag ?? true ? import_option8.Option.Some(null) : import_option8.Option.None();
|
|
940
|
+
return this;
|
|
941
|
+
}
|
|
942
|
+
equals(value) {
|
|
943
|
+
this._equals = import_option8.Option.Some(value);
|
|
944
|
+
return this;
|
|
945
|
+
}
|
|
946
|
+
min(value) {
|
|
947
|
+
this._min = import_option8.Option.Some(value);
|
|
948
|
+
return this;
|
|
949
|
+
}
|
|
950
|
+
max(value) {
|
|
951
|
+
this._max = import_option8.Option.Some(value);
|
|
952
|
+
return this;
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
// src/Builders/Array.ts
|
|
957
|
+
var import_option9 = __toESM(require_Option());
|
|
958
|
+
var ArrayBuilder = class extends ArrayValidator {
|
|
959
|
+
nullable(flag) {
|
|
960
|
+
this._nullable = flag ?? true ? import_option9.Option.Some(null) : import_option9.Option.None();
|
|
961
|
+
return this;
|
|
962
|
+
}
|
|
963
|
+
min(value) {
|
|
964
|
+
this._min = import_option9.Option.Some(value);
|
|
965
|
+
return this;
|
|
966
|
+
}
|
|
967
|
+
max(value) {
|
|
968
|
+
this._max = import_option9.Option.Some(value);
|
|
969
|
+
return this;
|
|
970
|
+
}
|
|
971
|
+
every(validator) {
|
|
972
|
+
this._every = import_option9.Option.Some(validator);
|
|
973
|
+
return this;
|
|
974
|
+
}
|
|
975
|
+
tuple(validators) {
|
|
976
|
+
this._tuple = import_option9.Option.Some(validators);
|
|
977
|
+
return this;
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
// src/Builders/Object.ts
|
|
982
|
+
var import_option10 = __toESM(require_Option());
|
|
983
|
+
var ObjectBuilder = class extends ObjectValidator {
|
|
984
|
+
nullable(flag) {
|
|
985
|
+
this._nullable = flag ?? true ? import_option10.Option.Some(null) : import_option10.Option.None();
|
|
986
|
+
return this;
|
|
987
|
+
}
|
|
988
|
+
min(value) {
|
|
989
|
+
this._min = import_option10.Option.Some(value);
|
|
990
|
+
return this;
|
|
991
|
+
}
|
|
992
|
+
max(value) {
|
|
993
|
+
this._max = import_option10.Option.Some(value);
|
|
994
|
+
return this;
|
|
995
|
+
}
|
|
996
|
+
every(validator) {
|
|
997
|
+
this._every = import_option10.Option.Some(validator);
|
|
998
|
+
return this;
|
|
999
|
+
}
|
|
1000
|
+
shape(value) {
|
|
1001
|
+
this._shape = import_option10.Option.Some(value);
|
|
1002
|
+
return this;
|
|
1003
|
+
}
|
|
1004
|
+
exact(flag) {
|
|
1005
|
+
this._exact = flag ?? true ? import_option10.Option.Some(null) : import_option10.Option.None();
|
|
1006
|
+
return this;
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
|
|
1137
1010
|
// src/index.ts
|
|
1138
1011
|
var Schema = class {
|
|
1012
|
+
// Static Factories for Builders
|
|
1139
1013
|
static get JSON() {
|
|
1140
|
-
return new
|
|
1014
|
+
return new JSONBuilder();
|
|
1141
1015
|
}
|
|
1142
1016
|
static get Boolean() {
|
|
1143
|
-
return new
|
|
1017
|
+
return new BooleanBuilder();
|
|
1144
1018
|
}
|
|
1145
1019
|
static get Number() {
|
|
1146
|
-
return new
|
|
1020
|
+
return new NumberBuilder();
|
|
1147
1021
|
}
|
|
1148
1022
|
static get String() {
|
|
1149
|
-
return new
|
|
1023
|
+
return new StringBuilder();
|
|
1150
1024
|
}
|
|
1151
1025
|
static get Array() {
|
|
1152
|
-
return new
|
|
1026
|
+
return new ArrayBuilder();
|
|
1153
1027
|
}
|
|
1154
1028
|
static get Object() {
|
|
1155
|
-
return new
|
|
1029
|
+
return new ObjectBuilder();
|
|
1156
1030
|
}
|
|
1031
|
+
// Static fromJSON. Returns a basic Validator with no Type Inference
|
|
1157
1032
|
static fromJSON(definition, path = "definition") {
|
|
1158
1033
|
return fromJSON(definition, path);
|
|
1159
1034
|
}
|
|
1160
|
-
|
|
1161
|
-
|
|
1035
|
+
// Utility Method for Checking if a variable holds a valid Definition
|
|
1036
|
+
static assertDefinition(definition, path = "definition") {
|
|
1037
|
+
this.fromJSON(definition, path);
|
|
1038
|
+
}
|
|
1039
|
+
// Utility Method for validating a Definition
|
|
1040
|
+
static validateDefinition(definition, path = "definition") {
|
|
1041
|
+
this.assertDefinition(definition, path);
|
|
1042
|
+
return definition;
|
|
1043
|
+
}
|
|
1044
|
+
// Utility Method for Type-Guarding a Definition
|
|
1045
|
+
static isDefinition(definition, path = "definition") {
|
|
1046
|
+
try {
|
|
1047
|
+
this.assertDefinition(definition, path);
|
|
1048
|
+
return true;
|
|
1049
|
+
} catch (e) {
|
|
1050
|
+
return false;
|
|
1051
|
+
}
|
|
1162
1052
|
}
|
|
1163
1053
|
};
|
|
1164
1054
|
var Validators = {
|
|
@@ -1169,8 +1059,17 @@ var Validators = {
|
|
|
1169
1059
|
Array: ArrayValidator,
|
|
1170
1060
|
Object: ObjectValidator
|
|
1171
1061
|
};
|
|
1062
|
+
var Builders = {
|
|
1063
|
+
JSON: JSONBuilder,
|
|
1064
|
+
Boolean: BooleanBuilder,
|
|
1065
|
+
Number: NumberBuilder,
|
|
1066
|
+
String: StringBuilder,
|
|
1067
|
+
Array: ArrayBuilder,
|
|
1068
|
+
Object: ObjectBuilder
|
|
1069
|
+
};
|
|
1172
1070
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1173
1071
|
0 && (module.exports = {
|
|
1072
|
+
Builders,
|
|
1174
1073
|
Schema,
|
|
1175
1074
|
Validator,
|
|
1176
1075
|
Validators
|