@serum-enterprises/schema 3.2.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 +248 -226
- 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 -2
- package/types/index.d.ts +26 -21
- package/types/lib/util.d.ts +0 -3
- package/types/validators/Array.d.ts +5 -25
- package/types/validators/Boolean.d.ts +5 -12
- package/types/validators/JSON.d.ts +3 -5
- package/types/validators/Number.d.ts +6 -18
- package/types/validators/Object.d.ts +6 -38
- package/types/validators/String.d.ts +6 -16
- package/types/validators/Union.d.ts +13 -0
- package/types/validators/index.d.ts +6 -0
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();
|
|
@@ -221,6 +227,9 @@ var JSONValidator = class _JSONValidator extends Validator {
|
|
|
221
227
|
isSubset(other) {
|
|
222
228
|
return other instanceof _JSONValidator;
|
|
223
229
|
}
|
|
230
|
+
isEquals(other) {
|
|
231
|
+
return other instanceof _JSONValidator;
|
|
232
|
+
}
|
|
224
233
|
toJSON() {
|
|
225
234
|
return {
|
|
226
235
|
type: "json"
|
|
@@ -228,7 +237,7 @@ var JSONValidator = class _JSONValidator extends Validator {
|
|
|
228
237
|
}
|
|
229
238
|
};
|
|
230
239
|
|
|
231
|
-
// src/
|
|
240
|
+
// src/Validators/Boolean.ts
|
|
232
241
|
var import_json2 = __toESM(require_build());
|
|
233
242
|
var import_option = __toESM(require_Option());
|
|
234
243
|
var BooleanValidator = class _BooleanValidator extends Validator {
|
|
@@ -237,25 +246,18 @@ var BooleanValidator = class _BooleanValidator extends Validator {
|
|
|
237
246
|
if ("nullable" in definition) {
|
|
238
247
|
if (!import_json2.JSON.isBoolean(definition["nullable"]))
|
|
239
248
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
240
|
-
|
|
249
|
+
if (definition["nullable"])
|
|
250
|
+
validatorInstance._nullable = import_option.Option.Some(null);
|
|
241
251
|
}
|
|
242
252
|
if ("equals" in definition) {
|
|
243
253
|
if (!import_json2.JSON.isBoolean(definition["equals"]))
|
|
244
254
|
throw new DefinitionError(`Expected ${path}.equals to be a Boolean`);
|
|
245
|
-
validatorInstance.
|
|
255
|
+
validatorInstance._equals = import_option.Option.Some(definition["equals"]);
|
|
246
256
|
}
|
|
247
257
|
return validatorInstance;
|
|
248
258
|
}
|
|
249
259
|
_nullable = import_option.Option.None();
|
|
250
260
|
_equals = import_option.Option.None();
|
|
251
|
-
nullable(flag) {
|
|
252
|
-
this._nullable = flag ?? true ? import_option.Option.Some(null) : import_option.Option.None();
|
|
253
|
-
return this;
|
|
254
|
-
}
|
|
255
|
-
equals(value) {
|
|
256
|
-
this._equals = import_option.Option.Some(value);
|
|
257
|
-
return this;
|
|
258
|
-
}
|
|
259
261
|
assert(data, path = "data") {
|
|
260
262
|
if (import_json2.JSON.isBoolean(data)) {
|
|
261
263
|
if (this._equals.isSome() && this._equals.value !== data)
|
|
@@ -281,6 +283,11 @@ var BooleanValidator = class _BooleanValidator extends Validator {
|
|
|
281
283
|
}
|
|
282
284
|
return true;
|
|
283
285
|
}
|
|
286
|
+
isEquals(other) {
|
|
287
|
+
if (!(other instanceof _BooleanValidator))
|
|
288
|
+
return false;
|
|
289
|
+
return this._nullable.equals(other._nullable) && this._equals.equals(other._equals);
|
|
290
|
+
}
|
|
284
291
|
toJSON() {
|
|
285
292
|
const definition = {
|
|
286
293
|
type: "boolean"
|
|
@@ -293,7 +300,7 @@ var BooleanValidator = class _BooleanValidator extends Validator {
|
|
|
293
300
|
}
|
|
294
301
|
};
|
|
295
302
|
|
|
296
|
-
// src/
|
|
303
|
+
// src/Validators/Number.ts
|
|
297
304
|
var import_json3 = __toESM(require_build());
|
|
298
305
|
var import_option2 = __toESM(require_Option());
|
|
299
306
|
var NumberValidator = class _NumberValidator extends Validator {
|
|
@@ -302,71 +309,37 @@ var NumberValidator = class _NumberValidator extends Validator {
|
|
|
302
309
|
if ("nullable" in definition) {
|
|
303
310
|
if (!import_json3.JSON.isBoolean(definition["nullable"]))
|
|
304
311
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
305
|
-
|
|
312
|
+
if (definition["nullable"])
|
|
313
|
+
validatorInstance._nullable = import_option2.Option.Some(null);
|
|
306
314
|
}
|
|
307
315
|
if ("equals" in definition) {
|
|
308
316
|
if (!import_json3.JSON.isNumber(definition["equals"]))
|
|
309
317
|
throw new DefinitionError(`Expected ${path}.equals to be a Number`);
|
|
310
|
-
validatorInstance.
|
|
318
|
+
validatorInstance._equals = import_option2.Option.Some(definition["equals"]);
|
|
311
319
|
}
|
|
312
320
|
if ("integer" in definition) {
|
|
313
321
|
if (!import_json3.JSON.isBoolean(definition["integer"]))
|
|
314
322
|
throw new DefinitionError(`Expected ${path}.integer to be a Boolean`);
|
|
315
|
-
|
|
323
|
+
if (definition["integer"])
|
|
324
|
+
validatorInstance._integer = import_option2.Option.Some(null);
|
|
316
325
|
}
|
|
317
326
|
if ("min" in definition) {
|
|
318
327
|
if (!import_json3.JSON.isNumber(definition["min"]))
|
|
319
328
|
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
320
|
-
validatorInstance.
|
|
329
|
+
validatorInstance._min = import_option2.Option.Some(definition["min"]);
|
|
321
330
|
}
|
|
322
331
|
if ("max" in definition) {
|
|
323
332
|
if (!import_json3.JSON.isNumber(definition["max"]))
|
|
324
333
|
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
325
|
-
validatorInstance.
|
|
334
|
+
validatorInstance._max = import_option2.Option.Some(definition["max"]);
|
|
326
335
|
}
|
|
327
336
|
return validatorInstance;
|
|
328
337
|
}
|
|
329
338
|
_nullable = import_option2.Option.None();
|
|
330
|
-
_equals = import_option2.Option.None();
|
|
331
339
|
_integer = import_option2.Option.None();
|
|
332
340
|
_min = import_option2.Option.None();
|
|
333
341
|
_max = import_option2.Option.None();
|
|
334
|
-
|
|
335
|
-
this._nullable = flag ?? true ? import_option2.Option.Some(null) : import_option2.Option.None();
|
|
336
|
-
return this;
|
|
337
|
-
}
|
|
338
|
-
equals(value, path = "equals") {
|
|
339
|
-
if (this._integer.isSome() && !Number.isSafeInteger(value))
|
|
340
|
-
throw new DefinitionError(`Expected Equals Rule to be an Integer according to the Integer Rule at Path ${path}`);
|
|
341
|
-
if (this._min.isSome() && this._min.value > value)
|
|
342
|
-
throw new DefinitionError(`Expected Equals Rule to be larger than or equal to the Minimum Rule at Path ${path}`);
|
|
343
|
-
if (this._max.isSome() && this._max.value < value)
|
|
344
|
-
throw new DefinitionError(`Expected Equals Rule to be smaller than or equal to the Maximum Rule at Path ${path}`);
|
|
345
|
-
this._equals = import_option2.Option.Some(value);
|
|
346
|
-
return this;
|
|
347
|
-
}
|
|
348
|
-
integer(flag = true, path = "integer") {
|
|
349
|
-
if (flag && this._equals.isSome() && !Number.isSafeInteger(this._equals.value))
|
|
350
|
-
throw new DefinitionError(`Expected Integer Rule to be a false due to the Equals Rule being a Float at Path ${path}`);
|
|
351
|
-
this._integer = flag ? import_option2.Option.Some(null) : import_option2.Option.None();
|
|
352
|
-
return this;
|
|
353
|
-
}
|
|
354
|
-
min(value, path = "min") {
|
|
355
|
-
if (this._max.isSome() && this._max.value < value)
|
|
356
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
357
|
-
if (this._equals.isSome() && this._equals.value < value)
|
|
358
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to the Equals Rule at Path ${path}`);
|
|
359
|
-
this._min = import_option2.Option.Some(value);
|
|
360
|
-
return this;
|
|
361
|
-
}
|
|
362
|
-
max(value, path = "max") {
|
|
363
|
-
if (this._min.isSome() && this._min.value > value)
|
|
364
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
365
|
-
if (this._equals.isSome() && this._equals.value > value)
|
|
366
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to the Equals Rule at Path ${path}`);
|
|
367
|
-
this._max = import_option2.Option.Some(value);
|
|
368
|
-
return this;
|
|
369
|
-
}
|
|
342
|
+
_equals = import_option2.Option.None();
|
|
370
343
|
assert(data, path = "data") {
|
|
371
344
|
if (import_json3.JSON.isNumber(data)) {
|
|
372
345
|
if (this._equals.isSome() && this._equals.value !== data)
|
|
@@ -411,6 +384,11 @@ var NumberValidator = class _NumberValidator extends Validator {
|
|
|
411
384
|
return false;
|
|
412
385
|
return true;
|
|
413
386
|
}
|
|
387
|
+
isEquals(other) {
|
|
388
|
+
if (!(other instanceof _NumberValidator))
|
|
389
|
+
return false;
|
|
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);
|
|
391
|
+
}
|
|
414
392
|
toJSON() {
|
|
415
393
|
const definition = {
|
|
416
394
|
type: "number"
|
|
@@ -429,7 +407,7 @@ var NumberValidator = class _NumberValidator extends Validator {
|
|
|
429
407
|
}
|
|
430
408
|
};
|
|
431
409
|
|
|
432
|
-
// src/
|
|
410
|
+
// src/Validators/String.ts
|
|
433
411
|
var import_json4 = __toESM(require_build());
|
|
434
412
|
var import_option3 = __toESM(require_Option());
|
|
435
413
|
var StringValidator = class _StringValidator extends Validator {
|
|
@@ -438,61 +416,30 @@ var StringValidator = class _StringValidator extends Validator {
|
|
|
438
416
|
if ("nullable" in definition) {
|
|
439
417
|
if (!import_json4.JSON.isBoolean(definition["nullable"]))
|
|
440
418
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
441
|
-
|
|
419
|
+
if (definition["nullable"])
|
|
420
|
+
validatorInstance._nullable = import_option3.Option.Some(null);
|
|
442
421
|
}
|
|
443
422
|
if ("equals" in definition) {
|
|
444
423
|
if (!import_json4.JSON.isString(definition["equals"]))
|
|
445
424
|
throw new DefinitionError(`Expected ${path}.equals to be a String`);
|
|
446
|
-
validatorInstance.
|
|
425
|
+
validatorInstance._equals = import_option3.Option.Some(definition["equals"]);
|
|
447
426
|
}
|
|
448
427
|
if ("min" in definition) {
|
|
449
428
|
if (!import_json4.JSON.isNumber(definition["min"]))
|
|
450
|
-
throw new DefinitionError(`Expected ${path}.min to be a
|
|
451
|
-
validatorInstance.
|
|
429
|
+
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
430
|
+
validatorInstance._min = import_option3.Option.Some(definition["min"]);
|
|
452
431
|
}
|
|
453
432
|
if ("max" in definition) {
|
|
454
433
|
if (!import_json4.JSON.isNumber(definition["max"]))
|
|
455
|
-
throw new DefinitionError(`Expected ${path}.max to be a
|
|
456
|
-
validatorInstance.
|
|
434
|
+
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
435
|
+
validatorInstance._max = import_option3.Option.Some(definition["max"]);
|
|
457
436
|
}
|
|
458
437
|
return validatorInstance;
|
|
459
438
|
}
|
|
460
439
|
_nullable = import_option3.Option.None();
|
|
461
|
-
_equals = import_option3.Option.None();
|
|
462
440
|
_min = import_option3.Option.None();
|
|
463
441
|
_max = import_option3.Option.None();
|
|
464
|
-
|
|
465
|
-
this._nullable = flag ?? true ? import_option3.Option.Some(null) : import_option3.Option.None();
|
|
466
|
-
return this;
|
|
467
|
-
}
|
|
468
|
-
equals(value, path = "equals") {
|
|
469
|
-
if (this._min.isSome() && this._min.value > value.length)
|
|
470
|
-
throw new DefinitionError(`Expected the Equals Rules Length to be larger than or equal to the Minimum Rule at Path ${path}`);
|
|
471
|
-
if (this._max.isSome() && this._max.value < value.length)
|
|
472
|
-
throw new DefinitionError(`Expected the Equals Rules Length to be smaller than or equal to the Maximum Rule at Path ${path}`);
|
|
473
|
-
this._equals = import_option3.Option.Some(value);
|
|
474
|
-
return this;
|
|
475
|
-
}
|
|
476
|
-
min(value, path = "min") {
|
|
477
|
-
if (!Number.isSafeInteger(value) || value < 0)
|
|
478
|
-
throw new DefinitionError(`Expected ${path}.min to be a positive Integer`);
|
|
479
|
-
if (this._max.isSome() && this._max.value < value)
|
|
480
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
481
|
-
if (this._equals.isSome() && this._equals.value.length < value)
|
|
482
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to the Equals Rules Length at Path ${path}`);
|
|
483
|
-
this._min = import_option3.Option.Some(value);
|
|
484
|
-
return this;
|
|
485
|
-
}
|
|
486
|
-
max(value, path = "max") {
|
|
487
|
-
if (!Number.isSafeInteger(value) || value < 0)
|
|
488
|
-
throw new DefinitionError(`Expected ${path}.max to be a positive Integer`);
|
|
489
|
-
if (this._min.isSome() && this._min.value > value)
|
|
490
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
491
|
-
if (this._equals.isSome() && this._equals.value.length > value)
|
|
492
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to the Equals Rules Length at Path ${path}`);
|
|
493
|
-
this._max = import_option3.Option.Some(value);
|
|
494
|
-
return this;
|
|
495
|
-
}
|
|
442
|
+
_equals = import_option3.Option.None();
|
|
496
443
|
assert(data, path = "data") {
|
|
497
444
|
if (import_json4.JSON.isString(data)) {
|
|
498
445
|
if (this._equals.isSome() && this._equals.value !== data)
|
|
@@ -531,6 +478,11 @@ var StringValidator = class _StringValidator extends Validator {
|
|
|
531
478
|
return false;
|
|
532
479
|
return true;
|
|
533
480
|
}
|
|
481
|
+
isEquals(other) {
|
|
482
|
+
if (!(other instanceof _StringValidator))
|
|
483
|
+
return false;
|
|
484
|
+
return this._nullable.equals(other._nullable) && this._min.equals(other._min) && this._max.equals(other._max) && this._equals.equals(other._equals);
|
|
485
|
+
}
|
|
534
486
|
toJSON() {
|
|
535
487
|
const definition = {
|
|
536
488
|
type: "string"
|
|
@@ -547,7 +499,7 @@ var StringValidator = class _StringValidator extends Validator {
|
|
|
547
499
|
}
|
|
548
500
|
};
|
|
549
501
|
|
|
550
|
-
// src/
|
|
502
|
+
// src/Validators/Array.ts
|
|
551
503
|
var import_json5 = __toESM(require_build());
|
|
552
504
|
var import_option4 = __toESM(require_Option());
|
|
553
505
|
var ArrayValidator = class _ArrayValidator extends Validator {
|
|
@@ -556,25 +508,21 @@ var ArrayValidator = class _ArrayValidator extends Validator {
|
|
|
556
508
|
if ("nullable" in definition) {
|
|
557
509
|
if (!import_json5.JSON.isBoolean(definition["nullable"]))
|
|
558
510
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
559
|
-
|
|
511
|
+
if (definition["nullable"])
|
|
512
|
+
validatorInstance._nullable = import_option4.Option.Some(null);
|
|
560
513
|
}
|
|
561
514
|
if ("min" in definition) {
|
|
562
515
|
if (!import_json5.JSON.isNumber(definition["min"]))
|
|
563
516
|
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
564
|
-
validatorInstance.
|
|
517
|
+
validatorInstance._min = import_option4.Option.Some(definition["min"]);
|
|
565
518
|
}
|
|
566
519
|
if ("max" in definition) {
|
|
567
520
|
if (!import_json5.JSON.isNumber(definition["max"]))
|
|
568
521
|
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
569
|
-
validatorInstance.
|
|
570
|
-
}
|
|
571
|
-
if ("every" in definition) {
|
|
572
|
-
if (!import_json5.JSON.isObject(definition["every"]))
|
|
573
|
-
throw new DefinitionError(`Expected ${path}.every to be an Object`);
|
|
574
|
-
validatorInstance.every(
|
|
575
|
-
fromJSON(definition["every"], `${path}.every`)
|
|
576
|
-
);
|
|
522
|
+
validatorInstance._max = import_option4.Option.Some(definition["max"]);
|
|
577
523
|
}
|
|
524
|
+
if ("every" in definition)
|
|
525
|
+
validatorInstance._every = import_option4.Option.Some(fromJSON(definition["every"], `${path}.every`));
|
|
578
526
|
if ("tuple" in definition) {
|
|
579
527
|
if (!import_json5.JSON.isShallowArray(definition["tuple"]))
|
|
580
528
|
throw new DefinitionError(`Expected ${path}.tuple to be an Array`);
|
|
@@ -591,7 +539,7 @@ var ArrayValidator = class _ArrayValidator extends Validator {
|
|
|
591
539
|
});
|
|
592
540
|
if (errors.length > 0)
|
|
593
541
|
throw new DefinitionError(`Multiple Definition Errors detected at ${path} (see cause)`, { cause: errors });
|
|
594
|
-
validatorInstance.
|
|
542
|
+
validatorInstance._tuple = import_option4.Option.Some(tupleSchemas);
|
|
595
543
|
}
|
|
596
544
|
return validatorInstance;
|
|
597
545
|
}
|
|
@@ -600,42 +548,6 @@ var ArrayValidator = class _ArrayValidator extends Validator {
|
|
|
600
548
|
_tuple = import_option4.Option.None();
|
|
601
549
|
_min = import_option4.Option.None();
|
|
602
550
|
_max = import_option4.Option.None();
|
|
603
|
-
nullable(flag) {
|
|
604
|
-
this._nullable = flag ?? true ? import_option4.Option.Some(null) : import_option4.Option.None();
|
|
605
|
-
return this;
|
|
606
|
-
}
|
|
607
|
-
min(value, path = "min") {
|
|
608
|
-
if (this._max.isSome() && this._max.value < value)
|
|
609
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
610
|
-
if (this._tuple.isSome() && value < this._tuple.value.length)
|
|
611
|
-
throw new DefinitionError(`Expected Minimum Rule to be larger than or equal to Tuple Length at Path ${path}`);
|
|
612
|
-
this._min = import_option4.Option.Some(value);
|
|
613
|
-
return this;
|
|
614
|
-
}
|
|
615
|
-
max(value, path = "max") {
|
|
616
|
-
if (this._min.isSome() && this._min.value > value)
|
|
617
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
618
|
-
if (this._tuple.isSome() && value < this._tuple.value.length)
|
|
619
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Tuple Length at Path ${path}`);
|
|
620
|
-
this._max = import_option4.Option.Some(value);
|
|
621
|
-
return this;
|
|
622
|
-
}
|
|
623
|
-
every(validator) {
|
|
624
|
-
this._every = import_option4.Option.Some(validator);
|
|
625
|
-
return this;
|
|
626
|
-
}
|
|
627
|
-
/**
|
|
628
|
-
* Applies ONLY to prefix indices [0..validators.length - 1]
|
|
629
|
-
* If every() is set, prefix elements are effectively `T[i] & E`.
|
|
630
|
-
*/
|
|
631
|
-
tuple(validators, path = "tuple") {
|
|
632
|
-
if (this._min.isSome() && this._min.value < validators.length)
|
|
633
|
-
throw new DefinitionError(`Expected Tuple Length to be smaller than or equal to Minimum Rule at Path ${path}`);
|
|
634
|
-
if (this._max.isSome() && this._max.value < validators.length)
|
|
635
|
-
throw new DefinitionError(`Expected Tuple Length to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
636
|
-
this._tuple = import_option4.Option.Some(validators);
|
|
637
|
-
return this;
|
|
638
|
-
}
|
|
639
551
|
assert(data, path = "data") {
|
|
640
552
|
if (import_json5.JSON.isShallowArray(data)) {
|
|
641
553
|
if (this._min.isSome() && this._min.value > data.length)
|
|
@@ -719,6 +631,19 @@ var ArrayValidator = class _ArrayValidator extends Validator {
|
|
|
719
631
|
}
|
|
720
632
|
return !(other._every.isSome() && (thisMax === Infinity ? true : thisMax > Math.max(thisTupleLen, otherTupleLen)) && (!this._every.isSome() || !this._every.value.isSubset(other._every.value)));
|
|
721
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
|
+
}
|
|
722
647
|
toJSON() {
|
|
723
648
|
const definition = { type: "array" };
|
|
724
649
|
if (this._nullable.isSome())
|
|
@@ -735,7 +660,7 @@ var ArrayValidator = class _ArrayValidator extends Validator {
|
|
|
735
660
|
}
|
|
736
661
|
};
|
|
737
662
|
|
|
738
|
-
// src/
|
|
663
|
+
// src/Validators/Object.ts
|
|
739
664
|
var import_json6 = __toESM(require_build());
|
|
740
665
|
var import_option5 = __toESM(require_Option());
|
|
741
666
|
var ObjectValidator = class _ObjectValidator extends Validator {
|
|
@@ -744,26 +669,27 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
744
669
|
if ("nullable" in definition) {
|
|
745
670
|
if (!import_json6.JSON.isBoolean(definition["nullable"]))
|
|
746
671
|
throw new DefinitionError(`Expected ${path}.nullable to be a Boolean`);
|
|
747
|
-
|
|
672
|
+
if (definition["nullable"])
|
|
673
|
+
validatorInstance._nullable = import_option5.Option.Some(null);
|
|
748
674
|
}
|
|
749
675
|
if ("exact" in definition) {
|
|
750
676
|
if (!import_json6.JSON.isBoolean(definition["exact"]))
|
|
751
677
|
throw new DefinitionError(`Expected ${path}.exact to be a Boolean`);
|
|
752
|
-
|
|
678
|
+
if (definition["exact"])
|
|
679
|
+
validatorInstance._exact = import_option5.Option.Some(null);
|
|
753
680
|
}
|
|
754
681
|
if ("min" in definition) {
|
|
755
682
|
if (!import_json6.JSON.isNumber(definition["min"]))
|
|
756
683
|
throw new DefinitionError(`Expected ${path}.min to be a Number`);
|
|
757
|
-
validatorInstance.
|
|
684
|
+
validatorInstance._min = import_option5.Option.Some(definition["min"]);
|
|
758
685
|
}
|
|
759
686
|
if ("max" in definition) {
|
|
760
687
|
if (!import_json6.JSON.isNumber(definition["max"]))
|
|
761
688
|
throw new DefinitionError(`Expected ${path}.max to be a Number`);
|
|
762
|
-
validatorInstance.
|
|
763
|
-
}
|
|
764
|
-
if ("every" in definition) {
|
|
765
|
-
validatorInstance.every(fromJSON(definition["every"], `${path}.every`));
|
|
689
|
+
validatorInstance._max = import_option5.Option.Some(definition["max"]);
|
|
766
690
|
}
|
|
691
|
+
if ("every" in definition)
|
|
692
|
+
validatorInstance._every = import_option5.Option.Some(fromJSON(definition["every"], `${path}.every`));
|
|
767
693
|
if ("shape" in definition) {
|
|
768
694
|
if (!import_json6.JSON.isShallowObject(definition["shape"]))
|
|
769
695
|
throw new DefinitionError(`Expected ${path}.shape to be an Object`);
|
|
@@ -781,7 +707,7 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
781
707
|
if (errors.length > 0) {
|
|
782
708
|
throw new DefinitionError(`Multiple Definition Errors detected at ${path}.shape (see cause)`, { cause: errors });
|
|
783
709
|
}
|
|
784
|
-
validatorInstance.
|
|
710
|
+
validatorInstance._shape = import_option5.Option.Some(shape);
|
|
785
711
|
}
|
|
786
712
|
return validatorInstance;
|
|
787
713
|
}
|
|
@@ -791,56 +717,6 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
791
717
|
_every = import_option5.Option.None();
|
|
792
718
|
_shape = import_option5.Option.None();
|
|
793
719
|
_exact = import_option5.Option.None();
|
|
794
|
-
nullable(flag) {
|
|
795
|
-
this._nullable = flag ?? true ? import_option5.Option.Some(null) : import_option5.Option.None();
|
|
796
|
-
return this;
|
|
797
|
-
}
|
|
798
|
-
min(value, path = "min") {
|
|
799
|
-
if (this._max.isSome() && this._max.value < value)
|
|
800
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
801
|
-
if (this._shape.isSome() && value < Object.keys(this._shape.value).length)
|
|
802
|
-
throw new DefinitionError(`Expected Minimum Rule to be larger than or equal to Shape Key Count at Path ${path}`);
|
|
803
|
-
if (this._exact.isSome() && this._shape.isSome() && value > Object.keys(this._shape.value).length)
|
|
804
|
-
throw new DefinitionError(`Expected Minimum Rule to be smaller than or equal to Shape Key Count due to Exact Rule at Path ${path}`);
|
|
805
|
-
this._min = import_option5.Option.Some(value);
|
|
806
|
-
return this;
|
|
807
|
-
}
|
|
808
|
-
max(value, path = "max") {
|
|
809
|
-
if (this._min.isSome() && this._min.value > value)
|
|
810
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Minimum Rule at Path ${path}`);
|
|
811
|
-
if (this._shape.isSome() && value < Object.keys(this._shape.value).length)
|
|
812
|
-
throw new DefinitionError(`Expected Maximum Rule to be larger than or equal to Shape Key Count at Path ${path}`);
|
|
813
|
-
this._max = import_option5.Option.Some(value);
|
|
814
|
-
return this;
|
|
815
|
-
}
|
|
816
|
-
every(validator) {
|
|
817
|
-
this._every = import_option5.Option.Some(validator);
|
|
818
|
-
return this;
|
|
819
|
-
}
|
|
820
|
-
shape(value, path = "shape") {
|
|
821
|
-
const shapeKeyCount = Object.keys(value).length;
|
|
822
|
-
if (this._min.isSome() && this._min.value < shapeKeyCount)
|
|
823
|
-
throw new DefinitionError(`Expected Shape Key Count to be smaller than or equal to Minimum Rule at Path ${path}`);
|
|
824
|
-
if (this._max.isSome() && this._max.value < shapeKeyCount)
|
|
825
|
-
throw new DefinitionError(`Expected Shape Key Count to be smaller than or equal to Maximum Rule at Path ${path}`);
|
|
826
|
-
if (this._exact.isSome() && this._min.isSome() && this._min.value > shapeKeyCount)
|
|
827
|
-
throw new DefinitionError(`Expected Shape Key Count to be larger than or equal to Minimum Rule due to Exact Rule at Path ${path}`);
|
|
828
|
-
if (this._exact.isSome() && this._max.isSome() && this._max.value < shapeKeyCount)
|
|
829
|
-
throw new DefinitionError(`Expected Shape Key Count to be smaller than or equal to Maximum Rule due to Exact Rule at Path ${path}`);
|
|
830
|
-
this._shape = import_option5.Option.Some(value);
|
|
831
|
-
return this;
|
|
832
|
-
}
|
|
833
|
-
exact(flag, path = "exact") {
|
|
834
|
-
if ((flag ?? true) && this._shape.isSome()) {
|
|
835
|
-
const shapeKeyCount = Object.keys(this._shape.value).length;
|
|
836
|
-
if (this._min.isSome() && this._min.value > shapeKeyCount)
|
|
837
|
-
throw new DefinitionError(`Expected Exact Rule to be false due to Minimum Rule requiring more Properties than Shape defines at Path ${path}`);
|
|
838
|
-
if (this._max.isSome() && this._max.value < shapeKeyCount)
|
|
839
|
-
throw new DefinitionError(`Expected Exact Rule to be false due to Maximum Rule allowing fewer Properties than Shape defines at Path ${path}`);
|
|
840
|
-
}
|
|
841
|
-
this._exact = flag ?? true ? import_option5.Option.Some(null) : import_option5.Option.None();
|
|
842
|
-
return this;
|
|
843
|
-
}
|
|
844
720
|
assert(data, path = "data") {
|
|
845
721
|
if (import_json6.JSON.isShallowObject(data)) {
|
|
846
722
|
const keys = Object.keys(data);
|
|
@@ -954,6 +830,21 @@ var ObjectValidator = class _ObjectValidator extends Validator {
|
|
|
954
830
|
}
|
|
955
831
|
return true;
|
|
956
832
|
}
|
|
833
|
+
isEquals(other) {
|
|
834
|
+
if (!(other instanceof _ObjectValidator))
|
|
835
|
+
return false;
|
|
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)
|
|
840
|
+
return false;
|
|
841
|
+
for (const key of keysA) {
|
|
842
|
+
if (!(key in b) || !a[key]?.isEquals(b[key]))
|
|
843
|
+
return false;
|
|
844
|
+
}
|
|
845
|
+
return true;
|
|
846
|
+
});
|
|
847
|
+
}
|
|
957
848
|
toJSON() {
|
|
958
849
|
const schema = { type: "object" };
|
|
959
850
|
if (this._nullable.isSome())
|
|
@@ -999,36 +890,158 @@ function fromJSON(definition, path) {
|
|
|
999
890
|
}
|
|
1000
891
|
}
|
|
1001
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
|
+
|
|
1002
1010
|
// src/index.ts
|
|
1003
1011
|
var Schema = class {
|
|
1012
|
+
// Static Factories for Builders
|
|
1004
1013
|
static get JSON() {
|
|
1005
|
-
return new
|
|
1014
|
+
return new JSONBuilder();
|
|
1006
1015
|
}
|
|
1007
1016
|
static get Boolean() {
|
|
1008
|
-
return new
|
|
1017
|
+
return new BooleanBuilder();
|
|
1009
1018
|
}
|
|
1010
1019
|
static get Number() {
|
|
1011
|
-
return new
|
|
1020
|
+
return new NumberBuilder();
|
|
1012
1021
|
}
|
|
1013
1022
|
static get String() {
|
|
1014
|
-
return new
|
|
1023
|
+
return new StringBuilder();
|
|
1015
1024
|
}
|
|
1016
1025
|
static get Array() {
|
|
1017
|
-
return new
|
|
1026
|
+
return new ArrayBuilder();
|
|
1018
1027
|
}
|
|
1019
1028
|
static get Object() {
|
|
1020
|
-
return new
|
|
1029
|
+
return new ObjectBuilder();
|
|
1021
1030
|
}
|
|
1031
|
+
// Static fromJSON. Returns a basic Validator with no Type Inference
|
|
1022
1032
|
static fromJSON(definition, path = "definition") {
|
|
1023
1033
|
return fromJSON(definition, path);
|
|
1024
1034
|
}
|
|
1035
|
+
// Utility Method for Checking if a variable holds a valid Definition
|
|
1025
1036
|
static assertDefinition(definition, path = "definition") {
|
|
1026
1037
|
this.fromJSON(definition, path);
|
|
1027
1038
|
}
|
|
1039
|
+
// Utility Method for validating a Definition
|
|
1028
1040
|
static validateDefinition(definition, path = "definition") {
|
|
1029
1041
|
this.assertDefinition(definition, path);
|
|
1030
1042
|
return definition;
|
|
1031
1043
|
}
|
|
1044
|
+
// Utility Method for Type-Guarding a Definition
|
|
1032
1045
|
static isDefinition(definition, path = "definition") {
|
|
1033
1046
|
try {
|
|
1034
1047
|
this.assertDefinition(definition, path);
|
|
@@ -1046,8 +1059,17 @@ var Validators = {
|
|
|
1046
1059
|
Array: ArrayValidator,
|
|
1047
1060
|
Object: ObjectValidator
|
|
1048
1061
|
};
|
|
1062
|
+
var Builders = {
|
|
1063
|
+
JSON: JSONBuilder,
|
|
1064
|
+
Boolean: BooleanBuilder,
|
|
1065
|
+
Number: NumberBuilder,
|
|
1066
|
+
String: StringBuilder,
|
|
1067
|
+
Array: ArrayBuilder,
|
|
1068
|
+
Object: ObjectBuilder
|
|
1069
|
+
};
|
|
1049
1070
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1050
1071
|
0 && (module.exports = {
|
|
1072
|
+
Builders,
|
|
1051
1073
|
Schema,
|
|
1052
1074
|
Validator,
|
|
1053
1075
|
Validators
|