justus 0.5.3 → 0.5.4
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/dist/dts-generator.cjs +21 -3
- package/dist/dts-generator.cjs.map +1 -1
- package/dist/dts-generator.mjs +21 -3
- package/dist/dts-generator.mjs.map +1 -1
- package/dist/extra/arn.cjs +1 -1
- package/dist/extra/arn.cjs.map +1 -1
- package/dist/extra/arn.mjs +1 -1
- package/dist/extra/arn.mjs.map +1 -1
- package/dist/index.cjs +13 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +9 -2
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs +0 -7
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +7 -9
- package/dist/types.mjs +0 -6
- package/dist/types.mjs.map +1 -1
- package/dist/utilities.cjs +1 -0
- package/dist/utilities.cjs.map +1 -1
- package/dist/utilities.mjs +1 -0
- package/dist/utilities.mjs.map +1 -1
- package/dist/validators/bigint.cjs +146 -0
- package/dist/validators/bigint.cjs.map +6 -0
- package/dist/validators/bigint.d.ts +46 -0
- package/dist/validators/bigint.mjs +118 -0
- package/dist/validators/bigint.mjs.map +6 -0
- package/dist/validators/boolean.cjs +1 -1
- package/dist/validators/boolean.cjs.map +1 -1
- package/dist/validators/boolean.mjs +1 -1
- package/dist/validators/boolean.mjs.map +1 -1
- package/dist/validators/constant.cjs +2 -1
- package/dist/validators/constant.cjs.map +1 -1
- package/dist/validators/constant.d.ts +2 -2
- package/dist/validators/constant.mjs +2 -1
- package/dist/validators/constant.mjs.map +1 -1
- package/dist/validators/never.cjs +1 -1
- package/dist/validators/never.cjs.map +1 -1
- package/dist/validators/never.mjs +2 -2
- package/dist/validators/never.mjs.map +1 -1
- package/dist/validators/number.cjs +5 -5
- package/dist/validators/number.cjs.map +1 -1
- package/dist/validators/number.mjs +5 -5
- package/dist/validators/number.mjs.map +1 -1
- package/dist/validators/object.cjs +7 -5
- package/dist/validators/object.cjs.map +1 -1
- package/dist/validators/object.mjs +8 -6
- package/dist/validators/object.mjs.map +1 -1
- package/dist/validators/optional.cjs +1 -1
- package/dist/validators/optional.cjs.map +1 -1
- package/dist/validators/optional.mjs +2 -2
- package/dist/validators/optional.mjs.map +1 -1
- package/dist/validators/string.cjs +2 -2
- package/dist/validators/string.cjs.map +1 -1
- package/dist/validators/string.mjs +2 -2
- package/dist/validators/string.mjs.map +1 -1
- package/package.json +3 -5
- package/src/dts-generator.ts +22 -3
- package/src/extra/arn.ts +1 -1
- package/src/index.ts +20 -2
- package/src/types.ts +7 -12
- package/src/utilities.ts +1 -0
- package/src/validators/bigint.ts +147 -0
- package/src/validators/boolean.ts +1 -1
- package/src/validators/constant.ts +4 -3
- package/src/validators/never.ts +2 -2
- package/src/validators/number.ts +5 -5
- package/src/validators/object.ts +8 -7
- package/src/validators/optional.ts +2 -2
- package/src/validators/string.ts +2 -2
|
@@ -33,7 +33,7 @@ var import_types = require("../types.cjs");
|
|
|
33
33
|
var import_utilities = require("../utilities.cjs");
|
|
34
34
|
var AnyObjectValidator = class extends import_types.AbstractValidator {
|
|
35
35
|
validate(value) {
|
|
36
|
-
(0, import_errors.assertValidation)(typeof value
|
|
36
|
+
(0, import_errors.assertValidation)(typeof value === "object", 'Value is not an "object"');
|
|
37
37
|
(0, import_errors.assertValidation)(value !== null, 'Value is "null"');
|
|
38
38
|
return value;
|
|
39
39
|
}
|
|
@@ -52,10 +52,10 @@ var ObjectValidator = class extends import_types.AbstractValidator {
|
|
|
52
52
|
}
|
|
53
53
|
this.schema = schema;
|
|
54
54
|
}
|
|
55
|
-
validate(value, options =
|
|
55
|
+
validate(value, options = {}) {
|
|
56
56
|
(0, import_errors.assertValidation)(typeof value === "object", 'Value is not an "object"');
|
|
57
57
|
(0, import_errors.assertValidation)(value !== null, 'Value is "null"');
|
|
58
|
-
const { stripAdditionalProperties, stripOptionalNulls } = options;
|
|
58
|
+
const { stripAdditionalProperties, stripOptionalNulls, partialValidation } = options;
|
|
59
59
|
const record = value;
|
|
60
60
|
const builder = new import_errors.ValidationErrorBuilder();
|
|
61
61
|
const clone = {};
|
|
@@ -66,9 +66,11 @@ var ObjectValidator = class extends import_types.AbstractValidator {
|
|
|
66
66
|
continue;
|
|
67
67
|
}
|
|
68
68
|
if (original === void 0) {
|
|
69
|
+
if (partialValidation)
|
|
70
|
+
continue;
|
|
69
71
|
try {
|
|
70
72
|
const validated = validator.validate(original, options);
|
|
71
|
-
if (!(optional && validated
|
|
73
|
+
if (!(optional && validated === void 0))
|
|
72
74
|
clone[key] = validated;
|
|
73
75
|
} catch (error) {
|
|
74
76
|
if (optional)
|
|
@@ -79,7 +81,7 @@ var ObjectValidator = class extends import_types.AbstractValidator {
|
|
|
79
81
|
}
|
|
80
82
|
try {
|
|
81
83
|
const validated = validator.validate(original, options);
|
|
82
|
-
if (!(optional && validated
|
|
84
|
+
if (!(optional && validated === void 0))
|
|
83
85
|
clone[key] = validated;
|
|
84
86
|
} catch (error) {
|
|
85
87
|
builder.record(error, key);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/object.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyD;AACzD,sBAAyB;AACzB,
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyD;AACzD,sBAAyB;AACzB,mBAAwD;AACxD,uBAA6B;AActB,IAAM,qBAAN,cAAiC,+BAAuC;AAAA,EAC7E,SAAS,OAAqC;AAC5C,wCAAiB,OAAO,UAAU,UAAU,0BAA0B;AACtE,wCAAiB,UAAU,MAAM,iBAAiB;AAClD,WAAO;AAAA,EACT;AACF;AAGO,IAAM,kBAAN,cAAgD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idation, ValidationErrorBuilder } from "../errors.mjs";
|
|
3
3
|
import { registry } from "../registry.mjs";
|
|
4
|
-
import { AbstractValidator,
|
|
4
|
+
import { AbstractValidator, makeValidatorFactory } from "../types.mjs";
|
|
5
5
|
import { getValidator } from "../utilities.mjs";
|
|
6
6
|
var AnyObjectValidator = class extends AbstractValidator {
|
|
7
7
|
validate(value) {
|
|
8
|
-
assertValidation(typeof value
|
|
8
|
+
assertValidation(typeof value === "object", 'Value is not an "object"');
|
|
9
9
|
assertValidation(value !== null, 'Value is "null"');
|
|
10
10
|
return value;
|
|
11
11
|
}
|
|
@@ -24,10 +24,10 @@ var ObjectValidator = class extends AbstractValidator {
|
|
|
24
24
|
}
|
|
25
25
|
this.schema = schema;
|
|
26
26
|
}
|
|
27
|
-
validate(value, options =
|
|
27
|
+
validate(value, options = {}) {
|
|
28
28
|
assertValidation(typeof value === "object", 'Value is not an "object"');
|
|
29
29
|
assertValidation(value !== null, 'Value is "null"');
|
|
30
|
-
const { stripAdditionalProperties, stripOptionalNulls } = options;
|
|
30
|
+
const { stripAdditionalProperties, stripOptionalNulls, partialValidation } = options;
|
|
31
31
|
const record = value;
|
|
32
32
|
const builder = new ValidationErrorBuilder();
|
|
33
33
|
const clone = {};
|
|
@@ -38,9 +38,11 @@ var ObjectValidator = class extends AbstractValidator {
|
|
|
38
38
|
continue;
|
|
39
39
|
}
|
|
40
40
|
if (original === void 0) {
|
|
41
|
+
if (partialValidation)
|
|
42
|
+
continue;
|
|
41
43
|
try {
|
|
42
44
|
const validated = validator.validate(original, options);
|
|
43
|
-
if (!(optional && validated
|
|
45
|
+
if (!(optional && validated === void 0))
|
|
44
46
|
clone[key] = validated;
|
|
45
47
|
} catch (error) {
|
|
46
48
|
if (optional)
|
|
@@ -51,7 +53,7 @@ var ObjectValidator = class extends AbstractValidator {
|
|
|
51
53
|
}
|
|
52
54
|
try {
|
|
53
55
|
const validated = validator.validate(original, options);
|
|
54
|
-
if (!(optional && validated
|
|
56
|
+
if (!(optional && validated === void 0))
|
|
55
57
|
clone[key] = validated;
|
|
56
58
|
} catch (error) {
|
|
57
59
|
builder.record(error, key);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/object.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,kBAAkB,8BAA8B;AACzD,SAAS,gBAAgB;AACzB,SAAS,mBAAmB,
|
|
4
|
+
"mappings": ";AAAA,SAAS,kBAAkB,8BAA8B;AACzD,SAAS,gBAAgB;AACzB,SAAS,mBAAmB,4BAA4B;AACxD,SAAS,oBAAoB;AActB,IAAM,qBAAN,cAAiC,kBAAuC;AAAA,EAC7E,SAAS,OAAqC;AAC5C,qBAAiB,OAAO,UAAU,UAAU,0BAA0B;AACtE,qBAAiB,UAAU,MAAM,iBAAiB;AAClD,WAAO;AAAA,EACT;AACF;AAGO,IAAM,kBAAN,cAAgD,kBAAuD;AAAA,EACnG;AAAA,EAET,aAAa,oBAAI,IAAuB;AAAA,EACxC;AAAA,EAEA,YAAY,QAAW;AACrB,UAAM;AACN,UAAM,EAAE,CAAC,OAAO,yBAAyB,GAAG,YAAY,GAAG,WAAW,IAAI;AAE1E,QAAI;AAAY,WAAK,uBAAuB;AAE5C,eAAW,OAAO,OAAO,KAAK,UAAU,GAAG;AACzC,WAAK,WAAW,IAAI,KAAK,aAAa,WAAW,GAAG,CAAC,CAAC;AAAA,IACxD;AAEA,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,SAAS,OAAgB,UAA6B,CAAC,GAAmB;AACxE,qBAAiB,OAAO,UAAU,UAAU,0BAA0B;AACtE,qBAAiB,UAAU,MAAM,iBAAiB;AAElD,UAAM,EAAE,2BAA2B,oBAAoB,kBAAkB,IAAI;AAE7E,UAAM,SAA0D;AAChE,UAAM,UAAU,IAAI,uBAAuB;AAC3C,UAAM,QAA6B,CAAC;AAEpC,eAAW,CAAE,KAAK,SAAU,KAAK,KAAK,WAAW,Q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AAE5C,YAAU,WAAoD;AAC5D,UAAM,EAAE,CAAC,OAAO,mBAAmB,GAAG,UAAU;AAAA,EAClD;AAEA,SAAO,OAAO,iBAAiB,QAAQ;AAAA,IACrC,CAAC,OAAO,eAAe,GAAG,EAAE,OAAO,WAAW,YAAY,MAAM;AAAA,IAChE,CAAC,OAAO,QAAQ,GAAG,EAAE,OAAO,UAAU,YAAY,MAAM;AAAA,EAC1D,CAAC;AACH;AAGO,IAAM,SAAS,qBAAqB,IAAI,mBAAmB,GAAG,aAAa;AAG3E,SAAS,SAA+B,YAA8D;AAC3G,SAAO,IAAI,gBAAgB,EAAE,CAAC,OAAO,yBAAyB,GAAG,aAAa,UAAU,EAAE,CAAC;AAC7F;AAGA,SAAS,IAAI,UAAU,eAAe;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -38,7 +38,7 @@ var OptionalValidator = class extends import_types.AbstractValidator {
|
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
40
|
try {
|
|
41
|
-
this.defaultValue = validator.validate(defaultValue,
|
|
41
|
+
this.defaultValue = validator.validate(defaultValue, {});
|
|
42
42
|
} catch (cause) {
|
|
43
43
|
throw new TypeError("Default value does not match validator", { cause });
|
|
44
44
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/optional.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkC;AAClC,uBAA6B;AAOtB,IAAM,oBAAN,cAIG,+BAA0E;AAAA,EAClF;AAAA,EACA;AAAA,EAKA,YAAY,WAAyB,cAAkB;AACrD,UAAM;AACN,SAAK,YAAY;AACjB,SAAK,WAAY,iBAAiB;AAClC,QAAI,KAAK,UAAU;AACjB,WAAK,eAAe;AACpB;AAAA,IACF;AAEA,QAAI;AACF,WAAK,eAAe,UAAU,SAAS,cAAc,CAAC,CAAC;AAAA,IACzD,SAAS,OAAO;AACd,YAAM,IAAI,UAAU,0CAA0C,EAAE,MAAM,CAAC;AAAA,IACzE;AAAA,EACF;AAAA,EAGA,SAAS,OAAgB,SAA4C;AACnE,QAAI,UAAU;AAAW,aAAO,KAAK;AACrC,WAAO,KAAK,UAAU,SAAS,OAAO,OAAO;AAAA,EAC/C;AACF;AAeO,SAAS,SAEd,YAAe,cAA2E;AAC1F,QAAM,gBAAY,+BAAa,UAAU;AACzC,SAAO,IAAI,kBAAkB,WAAW,YAAY;AACtD;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// validators/optional.ts
|
|
2
|
-
import { AbstractValidator
|
|
2
|
+
import { AbstractValidator } from "../types.mjs";
|
|
3
3
|
import { getValidator } from "../utilities.mjs";
|
|
4
4
|
var OptionalValidator = class extends AbstractValidator {
|
|
5
5
|
validator;
|
|
@@ -13,7 +13,7 @@ var OptionalValidator = class extends AbstractValidator {
|
|
|
13
13
|
return;
|
|
14
14
|
}
|
|
15
15
|
try {
|
|
16
|
-
this.defaultValue = validator.validate(defaultValue,
|
|
16
|
+
this.defaultValue = validator.validate(defaultValue, {});
|
|
17
17
|
} catch (cause) {
|
|
18
18
|
throw new TypeError("Default value does not match validator", { cause });
|
|
19
19
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/optional.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,
|
|
4
|
+
"mappings": ";AAAA,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAOtB,IAAM,oBAAN,cAIG,kBAA0E;AAAA,EAClF;AAAA,EACA;AAAA,EAKA,YAAY,WAAyB,cAAkB;AACrD,UAAM;AACN,SAAK,YAAY;AACjB,SAAK,WAAY,iBAAiB;AAClC,QAAI,KAAK,UAAU;AACjB,WAAK,eAAe;AACpB;AAAA,IACF;AAEA,QAAI;AACF,WAAK,eAAe,UAAU,SAAS,cAAc,CAAC,CAAC;AAAA,IACzD,SAAS,OAAO;AACd,YAAM,IAAI,UAAU,0CAA0C,EAAE,MAAM,CAAC;AAAA,IACzE;AAAA,EACF;AAAA,EAGA,SAAS,OAAgB,SAA4C;AACnE,QAAI,UAAU;AAAW,aAAO,KAAK;AACrC,WAAO,KAAK,UAAU,SAAS,OAAO,OAAO;AAAA,EAC/C;AACF;AAeO,SAAS,SAEd,YAAe,cAA2E;AAC1F,QAAM,YAAY,aAAa,UAAU;AACzC,SAAO,IAAI,kBAAkB,WAAW,YAAY;AACtD;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -30,7 +30,7 @@ var import_errors = require("../errors.cjs");
|
|
|
30
30
|
var import_types = require("../types.cjs");
|
|
31
31
|
var AnyStringValidator = class extends import_types.AbstractValidator {
|
|
32
32
|
validate(value) {
|
|
33
|
-
(0, import_errors.assertValidation)(typeof value
|
|
33
|
+
(0, import_errors.assertValidation)(typeof value === "string", 'Value is not a "string"');
|
|
34
34
|
return value;
|
|
35
35
|
}
|
|
36
36
|
};
|
|
@@ -56,7 +56,7 @@ var StringValidator = class extends import_types.AbstractValidator {
|
|
|
56
56
|
this.pattern = pattern;
|
|
57
57
|
}
|
|
58
58
|
validate(value) {
|
|
59
|
-
(0, import_errors.assertValidation)(typeof value
|
|
59
|
+
(0, import_errors.assertValidation)(typeof value === "string", 'Value is not a "string"');
|
|
60
60
|
(0, import_errors.assertValidation)(
|
|
61
61
|
value.length >= this.minLength,
|
|
62
62
|
`String must have a minimum length of ${this.minLength}`
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/string.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;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,WAAO;AAAA,EACT;AACF;AAKO,SAAS,cAAc,aAAmD;AAC/E,SAAO,IAAI,gBAAgB,WAAW;AACxC;AAGO,IAAM,aAAS,mCAAqB,IAAI,mBAAmB,GAAG,aAAa;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -3,7 +3,7 @@ import { assertSchema, assertValidation } from "../errors.mjs";
|
|
|
3
3
|
import { AbstractValidator, makeValidatorFactory } from "../types.mjs";
|
|
4
4
|
var AnyStringValidator = class extends AbstractValidator {
|
|
5
5
|
validate(value) {
|
|
6
|
-
assertValidation(typeof value
|
|
6
|
+
assertValidation(typeof value === "string", 'Value is not a "string"');
|
|
7
7
|
return value;
|
|
8
8
|
}
|
|
9
9
|
};
|
|
@@ -29,7 +29,7 @@ var StringValidator = class extends AbstractValidator {
|
|
|
29
29
|
this.pattern = pattern;
|
|
30
30
|
}
|
|
31
31
|
validate(value) {
|
|
32
|
-
assertValidation(typeof value
|
|
32
|
+
assertValidation(typeof value === "string", 'Value is not a "string"');
|
|
33
33
|
assertValidation(
|
|
34
34
|
value.length >= this.minLength,
|
|
35
35
|
`String must have a minimum length of ${this.minLength}`
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/string.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,cAAc,wBAAwB;AAC/C,SAAS,mBAAmB,4BAA4B;AAqBjD,IAAM,qBAAN,cAAiC,kBAA0B;AAAA,EAChE,SAAS,OAAwB;AAC/B,qBAAiB,OAAO,
|
|
4
|
+
"mappings": ";AAAA,SAAS,cAAc,wBAAwB;AAC/C,SAAS,mBAAmB,4BAA4B;AAqBjD,IAAM,qBAAN,cAAiC,kBAA0B;AAAA,EAChE,SAAS,OAAwB;AAC/B,qBAAiB,OAAO,UAAU,UAAU,yBAAyB;AACrE,WAAO;AAAA,EACT;AACF;AAGO,IAAM,kBAAN,cAAqE,kBAAwB;AAAA,EACzF;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY,cAAiC,CAAC,GAAG;AAC/C,UAAM;AAEN,UAAM;AAAA,MACJ,YAAY;AAAA,MACZ,YAAY,OAAO;AAAA,MACnB;AAAA,IACF,IAAI;AAEJ,QAAI,WAAW;AAAa,WAAK,QAAe,YAAa;AAE7D,iBAAa,aAAa,GAAG,2BAA2B,SAAS,wBAAwB;AACzF,iBAAa,aAAa,GAAG,2BAA2B,SAAS,wBAAwB;AACzF,iBAAa,aAAa,WAAW,2BAA2B,SAAS,kCAAkC,SAAS,GAAG;AAEvH,SAAK,YAAY;AACjB,SAAK,YAAY;AACjB,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,SAAS,OAAmB;AAC1B,qBAAiB,OAAO,UAAU,UAAU,yBAAyB;AAErE;AAAA,MAAiB,MAAM,UAAU,KAAK;AAAA,MAClC,wCAAwC,KAAK,SAAS;AAAA,IAAE;AAE5D;AAAA,MAAiB,MAAM,UAAU,KAAK;AAAA,MAClC,wCAAwC,KAAK,SAAS;AAAA,IAAE;AAE5D;AAAA,MAAiB,KAAK,UAAU,KAAK,QAAQ,KAAK,KAAK,IAAI;AAAA,MACvD,0CAA0C,KAAK,OAAO;AAAA,IAAE;AAE5D,WAAO;AAAA,EACT;AACF;AAKO,SAAS,cAAc,aAAmD;AAC/E,SAAO,IAAI,gBAAgB,WAAW;AACxC;AAGO,IAAM,SAAS,qBAAqB,IAAI,mBAAmB,GAAG,aAAa;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "justus",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "A JavaScript validation library, with types!",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -78,10 +78,8 @@
|
|
|
78
78
|
"author": "Juit Developers <developers@juit.com>",
|
|
79
79
|
"license": "Apache-2.0",
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@plugjs/build": "^0.5.
|
|
82
|
-
"@plugjs/tsd": "^0.5.
|
|
83
|
-
"@types/chai": "^4.3.9",
|
|
84
|
-
"chai": "^4.3.10",
|
|
81
|
+
"@plugjs/build": "^0.5.9",
|
|
82
|
+
"@plugjs/tsd": "^0.5.9",
|
|
85
83
|
"typescript": "^5.2.2"
|
|
86
84
|
},
|
|
87
85
|
"directories": {
|
package/src/dts-generator.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { UUIDValidator } from './extra/uuid'
|
|
|
7
7
|
import { getValidator } from './utilities'
|
|
8
8
|
import { AnyValidator } from './validators/any'
|
|
9
9
|
import { AnyArrayValidator, ArrayValidator } from './validators/array'
|
|
10
|
+
import { AnyBigIntValidator, BigIntValidator } from './validators/bigint'
|
|
10
11
|
import { BooleanValidator } from './validators/boolean'
|
|
11
12
|
import { ConstantValidator } from './validators/constant'
|
|
12
13
|
import { DateValidator } from './validators/date'
|
|
@@ -29,8 +30,8 @@ import type { Validation, Validator } from './types'
|
|
|
29
30
|
/** Check that two of our generated types are equal */
|
|
30
31
|
function typeEqual(a: TypeNode, b: TypeNode): boolean {
|
|
31
32
|
function eq(a: any, b: any): boolean {
|
|
32
|
-
if ((typeof a
|
|
33
|
-
(typeof b
|
|
33
|
+
if ((typeof a === 'object' && a != null) &&
|
|
34
|
+
(typeof b === 'object' && b != null) ) {
|
|
34
35
|
for (const key in a) {
|
|
35
36
|
if (! eq(a[key], b[key])) return false
|
|
36
37
|
}
|
|
@@ -426,6 +427,7 @@ function generateTypeNode(
|
|
|
426
427
|
|
|
427
428
|
const anyType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
|
|
428
429
|
const anyArrayType = ts.factory.createArrayTypeNode(anyType)
|
|
430
|
+
const bigintType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword)
|
|
429
431
|
const booleanType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword)
|
|
430
432
|
const dateType = ts.factory.createTypeReferenceNode('Date')
|
|
431
433
|
const numberType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
|
|
@@ -453,6 +455,7 @@ const readonlyModifiers = [ ts.factory.createModifier(ts.SyntaxKind.ReadonlyKeyw
|
|
|
453
455
|
|
|
454
456
|
registerTypeGenerator(AnyValidator, () => anyType)
|
|
455
457
|
registerTypeGenerator(AnyArrayValidator, () => anyArrayType)
|
|
458
|
+
registerTypeGenerator(AnyBigIntValidator, () => bigintType)
|
|
456
459
|
registerTypeGenerator(AnyNumberValidator, () => numberType)
|
|
457
460
|
registerTypeGenerator(AnyObjectValidator, () => recordType)
|
|
458
461
|
registerTypeGenerator(AnyStringValidator, () => stringType)
|
|
@@ -467,6 +470,21 @@ registerTypeGenerator(ArrayValidator, (validator, references, isInput) => {
|
|
|
467
470
|
return ts.factory.createArrayTypeNode(itemType)
|
|
468
471
|
})
|
|
469
472
|
|
|
473
|
+
registerTypeGenerator(BigIntValidator, (validator: BigIntValidator, _references, isInput) => {
|
|
474
|
+
if (isInput) {
|
|
475
|
+
const types: ts.TypeNode[] = [ bigintType ]
|
|
476
|
+
if (validator.fromNumber) types.push(numberType)
|
|
477
|
+
if (validator.fromString) types.push(stringType)
|
|
478
|
+
return types.length === 1 ? types[0] : ts.factory.createUnionTypeNode(types)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (! validator.brand) return bigintType
|
|
482
|
+
|
|
483
|
+
const signature = ts.factory.createPropertySignature(undefined, `__brand_${validator.brand}`, undefined, neverType)
|
|
484
|
+
const literal = ts.factory.createTypeLiteralNode([ signature ])
|
|
485
|
+
return ts.factory.createIntersectionTypeNode([ bigintType, literal ])
|
|
486
|
+
})
|
|
487
|
+
|
|
470
488
|
registerTypeGenerator(BooleanValidator, (validator, _references, isInput) => {
|
|
471
489
|
return (isInput && validator.fromString) ?
|
|
472
490
|
ts.factory.createUnionTypeNode([
|
|
@@ -481,6 +499,7 @@ registerTypeGenerator(ConstantValidator, (validator) => {
|
|
|
481
499
|
const literal =
|
|
482
500
|
typeof validator.constant === 'number' ? ts.factory.createNumericLiteral(validator.constant) :
|
|
483
501
|
typeof validator.constant === 'string' ? ts.factory.createStringLiteral(validator.constant) :
|
|
502
|
+
typeof validator.constant === 'bigint' ? ts.factory.createBigIntLiteral(`${validator.constant}n`) :
|
|
484
503
|
validator.constant === false ? ts.factory.createFalse() :
|
|
485
504
|
validator.constant === true ? ts.factory.createTrue() :
|
|
486
505
|
validator.constant === null ? ts.factory.createNull() :
|
|
@@ -611,7 +630,7 @@ registerTypeGenerator(ObjectValidator, (validator, references, isInput) => {
|
|
|
611
630
|
propertyType, // type
|
|
612
631
|
undefined) // members
|
|
613
632
|
|
|
614
|
-
if (properties.length
|
|
633
|
+
if (properties.length === 0) return extra
|
|
615
634
|
|
|
616
635
|
const type = ts.factory.createTypeLiteralNode(properties)
|
|
617
636
|
return ts.factory.createIntersectionTypeNode([ type, extra ])
|
package/src/extra/arn.ts
CHANGED
|
@@ -42,7 +42,7 @@ function parse<Service extends string, ResourceType extends string>(
|
|
|
42
42
|
service?: Service,
|
|
43
43
|
type?: ResourceType,
|
|
44
44
|
): ParsedArn<Service, ResourceType> {
|
|
45
|
-
assertValidation(typeof value
|
|
45
|
+
assertValidation(typeof value === 'string', 'Value is not a "string"')
|
|
46
46
|
|
|
47
47
|
const segments = value.split(':')
|
|
48
48
|
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ export * from './utilities'
|
|
|
11
11
|
// Validators
|
|
12
12
|
export { AnyValidator, any } from './validators/any'
|
|
13
13
|
export { AnyArrayValidator, ArrayValidator, array, arrayOf } from './validators/array'
|
|
14
|
+
export { AnyBigIntValidator, BigIntValidator, bigint } from './validators/bigint'
|
|
14
15
|
export { BooleanValidator, boolean } from './validators/boolean'
|
|
15
16
|
export { ConstantValidator, constant } from './validators/constant'
|
|
16
17
|
export { DateValidator, date } from './validators/date'
|
|
@@ -24,6 +25,7 @@ export { AllOfValidator, OneOfValidator, allOf, oneOf } from './validators/union
|
|
|
24
25
|
|
|
25
26
|
// Validator Types
|
|
26
27
|
export type { ArrayConstraints, arrayFactory } from './validators/array'
|
|
28
|
+
export type { BrandedBigIntConstraints, bigintFactory } from './validators/bigint'
|
|
27
29
|
export type { BooleanConstraints, booleanFactory } from './validators/boolean'
|
|
28
30
|
export type { DateConstraints, dateFactory } from './validators/date'
|
|
29
31
|
export type { BrandedNumberConstraints, numberFactory } from './validators/number'
|
|
@@ -35,7 +37,6 @@ export type { TupleMember } from './validators/tuple'
|
|
|
35
37
|
* VALIDATE FUNCTION (our main entry point) *
|
|
36
38
|
* ========================================================================== */
|
|
37
39
|
|
|
38
|
-
import { defaultValidationOptions } from './types'
|
|
39
40
|
import { getValidator } from './utilities'
|
|
40
41
|
|
|
41
42
|
import type { InferValidation, Validation, ValidationOptions } from './types'
|
|
@@ -51,7 +52,7 @@ export function validate<V extends Validation>(
|
|
|
51
52
|
value: any,
|
|
52
53
|
options?: ValidationOptions,
|
|
53
54
|
): InferValidation<V> {
|
|
54
|
-
const opts: ValidationOptions = { ...
|
|
55
|
+
const opts: ValidationOptions = { ...options }
|
|
55
56
|
return getValidator(validation).validate(value, opts)
|
|
56
57
|
}
|
|
57
58
|
|
|
@@ -83,3 +84,20 @@ export function strip<V extends Validation>(
|
|
|
83
84
|
|
|
84
85
|
return getValidator(validation).validate(value, opts)
|
|
85
86
|
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Validate a _value_ using the specified `Validation`, automatically stripping
|
|
90
|
+
* additional properties and optional `null`s (but not forbidden ones), and
|
|
91
|
+
* treating all properties as optional.
|
|
92
|
+
*
|
|
93
|
+
* This is equivalent to setting the `partialValidation` option to `true` in
|
|
94
|
+
* `validate(...)`, but this function correctly represents the returned type as
|
|
95
|
+
* a `Partial<...>` type.
|
|
96
|
+
*/
|
|
97
|
+
export function partial<V extends Validation>(
|
|
98
|
+
validation: V,
|
|
99
|
+
value: any,
|
|
100
|
+
options?: ValidationOptions,
|
|
101
|
+
): Partial<InferValidation<V>> {
|
|
102
|
+
return getValidator(validation).validate(value, { ...options, partialValidation: true })
|
|
103
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -34,15 +34,8 @@ export interface ValidationOptions {
|
|
|
34
34
|
stripOptionalNulls?: boolean,
|
|
35
35
|
/** Ignore and strip forbidden (`never`) properties from objects (default: `false`) */
|
|
36
36
|
stripForbiddenProperties?: boolean,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Default validation options.
|
|
41
|
-
*/
|
|
42
|
-
export const defaultValidationOptions: Readonly<Required<ValidationOptions>> = {
|
|
43
|
-
stripAdditionalProperties: false,
|
|
44
|
-
stripForbiddenProperties: false,
|
|
45
|
-
stripOptionalNulls: false,
|
|
37
|
+
/** Perform a _partial_ validation, treating all properties as optional (default: `false`) */
|
|
38
|
+
partialValidation?: boolean,
|
|
46
39
|
}
|
|
47
40
|
|
|
48
41
|
/**
|
|
@@ -117,13 +110,13 @@ implements Validator<T, I>, Iterable<TupleRestParameter<T, I>> {
|
|
|
117
110
|
* Those are:
|
|
118
111
|
*
|
|
119
112
|
* * A `Validator` instance or a _zero-arguments_ function returning one
|
|
120
|
-
* * A `Tuple` or a `Schema`, validated as arrays or
|
|
121
|
-
* * Either `null`, a `boolean`, a `number` or a `string` for constants
|
|
113
|
+
* * A `Tuple` or a `Schema`, validated as arrays or objects
|
|
114
|
+
* * Either `null`, a `boolean`, a `number`, a `bigint` or a `string` for constants
|
|
122
115
|
*/
|
|
123
116
|
export type Validation =
|
|
124
117
|
Validator | // Validator instances
|
|
125
118
|
Tuple | Schema | // Tuples or schemas (arrays, objects)
|
|
126
|
-
null | boolean | number | string // Primitives, mapped as constants
|
|
119
|
+
null | boolean | bigint | number | string // Primitives, mapped as constants
|
|
127
120
|
|
|
128
121
|
/**
|
|
129
122
|
* Infer the type returned by a `Validation` when validating.
|
|
@@ -135,6 +128,7 @@ export type InferValidation<V> =
|
|
|
135
128
|
// Primitives are returned as constants
|
|
136
129
|
V extends undefined ? V :
|
|
137
130
|
V extends boolean ? V :
|
|
131
|
+
V extends bigint ? V :
|
|
138
132
|
V extends number ? V :
|
|
139
133
|
V extends string ? V :
|
|
140
134
|
V extends null ? V :
|
|
@@ -155,6 +149,7 @@ export type InferInput<V> =
|
|
|
155
149
|
// Primitives are returned as constants
|
|
156
150
|
V extends undefined ? V :
|
|
157
151
|
V extends boolean ? V :
|
|
152
|
+
V extends bigint ? V :
|
|
158
153
|
V extends number ? V :
|
|
159
154
|
V extends string ? V :
|
|
160
155
|
V extends null ? V :
|
package/src/utilities.ts
CHANGED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { assertSchema, assertValidation, ValidationError } from '../errors'
|
|
2
|
+
import { AbstractValidator, makeValidatorFactory } from '../types'
|
|
3
|
+
|
|
4
|
+
import type { Branding, Validator } from '../types'
|
|
5
|
+
|
|
6
|
+
/* ========================================================================== */
|
|
7
|
+
|
|
8
|
+
/** Constraints to validate a `bigint` with. */
|
|
9
|
+
export interface BigIntConstraints {
|
|
10
|
+
/** The value for which a `bigint` must be multiple of for it to be valid */
|
|
11
|
+
multipleOf?: bigint | number,
|
|
12
|
+
/** The _inclusive_ maximum value for a valid `bigint`: `value <= maximum` */
|
|
13
|
+
maximum?: bigint | number,
|
|
14
|
+
/** The _inclusive_ minimum value for a valid `bigint`: `value >= minimum` */
|
|
15
|
+
minimum?: bigint | number,
|
|
16
|
+
/** The _exclusive_ maximum value for a valid `bigint`: `value < exclusiveMaximum` */
|
|
17
|
+
exclusiveMaximum?: bigint | number,
|
|
18
|
+
/** The _exclusive_ minimum value for a valid `bigint`: `value > exclusiveMaximum` */
|
|
19
|
+
exclusiveMinimum?: bigint | number,
|
|
20
|
+
/** Allow bigints to be parsed from strings (e.g. `123.456` or `0x0CAFE`, default: `false`) */
|
|
21
|
+
fromString?: boolean,
|
|
22
|
+
/** Allow bigints to be parsed from numbers (default: `true`) */
|
|
23
|
+
fromNumber?: boolean,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Constraints to validate a `bigint` with extra branding information. */
|
|
27
|
+
export interface BrandedBigIntConstraints<B extends string> extends BigIntConstraints {
|
|
28
|
+
/** The _brand_ of the string (will generate a `__brand_${B}` type property */
|
|
29
|
+
brand: B
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A `Validator` validating any `bigint`. */
|
|
33
|
+
export class AnyBigIntValidator extends AbstractValidator<bigint> {
|
|
34
|
+
validate(value: unknown): bigint {
|
|
35
|
+
if (typeof value === 'number') {
|
|
36
|
+
try {
|
|
37
|
+
value = BigInt(value)
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw new ValidationError('BigInt can not be parsed from number')
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
assertValidation(typeof value === 'bigint', 'Value is not a "bigint"')
|
|
43
|
+
return value
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A `Validator` validating `bigint`s with constaints. */
|
|
48
|
+
export class BigIntValidator<N extends bigint = bigint> extends AbstractValidator<N, bigint> {
|
|
49
|
+
readonly fromString: boolean
|
|
50
|
+
readonly fromNumber: boolean
|
|
51
|
+
readonly exclusiveMaximum?: bigint
|
|
52
|
+
readonly exclusiveMinimum?: bigint
|
|
53
|
+
readonly maximum?: bigint
|
|
54
|
+
readonly minimum?: bigint
|
|
55
|
+
readonly multipleOf?: bigint
|
|
56
|
+
readonly brand?: string
|
|
57
|
+
|
|
58
|
+
constructor(constraints: BigIntConstraints = {}) {
|
|
59
|
+
super()
|
|
60
|
+
|
|
61
|
+
const {
|
|
62
|
+
exclusiveMaximum,
|
|
63
|
+
exclusiveMinimum,
|
|
64
|
+
maximum,
|
|
65
|
+
minimum,
|
|
66
|
+
multipleOf,
|
|
67
|
+
fromString = false,
|
|
68
|
+
fromNumber = true,
|
|
69
|
+
} = constraints
|
|
70
|
+
|
|
71
|
+
if ('brand' in constraints) this.brand = (<any> constraints).brand
|
|
72
|
+
this.fromString = fromString
|
|
73
|
+
this.fromNumber = fromNumber
|
|
74
|
+
|
|
75
|
+
const _exclusiveMaximum = exclusiveMaximum === undefined ? undefined : BigInt(exclusiveMaximum)
|
|
76
|
+
const _exclusiveMinimum = exclusiveMinimum === undefined ? undefined : BigInt(exclusiveMinimum)
|
|
77
|
+
const _maximum = maximum === undefined ? undefined : BigInt(maximum)
|
|
78
|
+
const _minimum = minimum === undefined ? undefined : BigInt(minimum)
|
|
79
|
+
const _multipleOf = multipleOf === undefined ? undefined : BigInt(multipleOf)
|
|
80
|
+
|
|
81
|
+
if ((_maximum !== undefined) && (_minimum !== undefined)) {
|
|
82
|
+
assertSchema(_maximum >= _minimum, `Constraint "minimum" (${_minimum}) is greater than "maximum" (${_maximum})`)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if ((_exclusiveMaximum !== undefined) && (_minimum !== undefined)) {
|
|
86
|
+
assertSchema(_exclusiveMaximum > _minimum,
|
|
87
|
+
`Constraint "exclusiveMaximum" (${_exclusiveMaximum}) must be greater than "minimum" (${_minimum})`)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if ((_exclusiveMinimum !== undefined) && (_maximum != undefined)) {
|
|
91
|
+
assertSchema(_maximum > _exclusiveMinimum,
|
|
92
|
+
`Constraint "maximum" (${_maximum}) must be greater than "exclusiveMinimum" (${_exclusiveMinimum})`)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if ((_exclusiveMinimum != undefined) && (_exclusiveMaximum !== undefined)) {
|
|
96
|
+
assertSchema(_exclusiveMaximum > _exclusiveMinimum,
|
|
97
|
+
`Constraint "exclusiveMaximum" (${_exclusiveMaximum}) must be greater than "exclusiveMinimum" (${_exclusiveMinimum})`)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (_multipleOf !== undefined) {
|
|
101
|
+
assertSchema(_multipleOf > 0, `Constraint "multipleOf" (${_multipleOf}) must be greater than zero`)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.exclusiveMaximum = _exclusiveMaximum
|
|
105
|
+
this.exclusiveMinimum = _exclusiveMinimum
|
|
106
|
+
this.maximum = _maximum
|
|
107
|
+
this.minimum = _minimum
|
|
108
|
+
this.multipleOf = _multipleOf
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
validate(value: unknown): N {
|
|
112
|
+
// Allow parsing from strings or numbers
|
|
113
|
+
if (((typeof value === 'string') && (this.fromString)) ||
|
|
114
|
+
((typeof value === 'number') && (this.fromNumber))) {
|
|
115
|
+
try {
|
|
116
|
+
value = BigInt(value)
|
|
117
|
+
} catch (error) {
|
|
118
|
+
throw new ValidationError('BigInt can not be parsed from ' + typeof value)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
assertValidation(typeof value === 'bigint', 'Value is not a "bigint"')
|
|
123
|
+
|
|
124
|
+
assertValidation(((this.minimum === undefined) || (value >= this.minimum)),
|
|
125
|
+
`BigInt is less than ${this.minimum}`)
|
|
126
|
+
assertValidation(((this.maximum === undefined) || (value <= this.maximum)),
|
|
127
|
+
`BigInt is greater than ${this.maximum}`)
|
|
128
|
+
assertValidation((this.exclusiveMinimum === undefined) || (value > this.exclusiveMinimum),
|
|
129
|
+
`BigInt is less than or equal to ${this.exclusiveMinimum}`)
|
|
130
|
+
assertValidation((this.exclusiveMaximum === undefined) || (value < this.exclusiveMaximum),
|
|
131
|
+
`BigInt is greater than or equal to ${this.exclusiveMaximum}`)
|
|
132
|
+
assertValidation((this.multipleOf === undefined) || (!(value % this.multipleOf)),
|
|
133
|
+
`BigInt is not a multiple of ${this.multipleOf}`)
|
|
134
|
+
|
|
135
|
+
return value as N
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function bigintFactory(constraints: BigIntConstraints): BigIntValidator<bigint>
|
|
140
|
+
export function bigintFactory<N extends bigint>(constraints: BigIntConstraints): BigIntValidator<N>
|
|
141
|
+
export function bigintFactory<B extends string>(constraints: BrandedBigIntConstraints<B>): BigIntValidator<bigint & Branding<B>>
|
|
142
|
+
export function bigintFactory(constraints: BigIntConstraints): Validator<bigint> {
|
|
143
|
+
return new BigIntValidator(constraints)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Validate `bigint`s. */
|
|
147
|
+
export const bigint = makeValidatorFactory(new AnyBigIntValidator(), bigintFactory)
|
|
@@ -25,7 +25,7 @@ export class BooleanValidator extends AbstractValidator<boolean> {
|
|
|
25
25
|
|
|
26
26
|
validate(value: unknown): boolean {
|
|
27
27
|
// Allow parsing from strings
|
|
28
|
-
if ((typeof value
|
|
28
|
+
if ((typeof value === 'string') && (this.fromString)) {
|
|
29
29
|
const string = value.toLowerCase()
|
|
30
30
|
const parsed = string === 'true' ? true : string === 'false' ? false : undefined
|
|
31
31
|
assertValidation(parsed !== undefined, 'Boolean can not be parsed from string')
|
|
@@ -5,7 +5,7 @@ import { AbstractValidator } from '../types'
|
|
|
5
5
|
import type { Validator } from '../types'
|
|
6
6
|
|
|
7
7
|
/** A `Validator` for _constants_. */
|
|
8
|
-
export class ConstantValidator<T extends string | number | boolean | null> extends AbstractValidator<T> {
|
|
8
|
+
export class ConstantValidator<T extends string | number | boolean | bigint | null> extends AbstractValidator<T> {
|
|
9
9
|
readonly constant: T
|
|
10
10
|
|
|
11
11
|
constructor(constant: T) {
|
|
@@ -14,13 +14,14 @@ export class ConstantValidator<T extends string | number | boolean | null> exten
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
validate(value: unknown): T {
|
|
17
|
-
|
|
17
|
+
const extra = this.constant === null ? '' : ` (${typeof this.constant})`
|
|
18
|
+
assertValidation(value === this.constant, `Value does not match constant "${this.constant}"${extra}`)
|
|
18
19
|
return value as T
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
/** Validate _constants_. */
|
|
23
|
-
export function constant<T extends string | number | boolean | null>(constant: T): Validator<T> {
|
|
24
|
+
export function constant<T extends string | number | boolean | bigint | null>(constant: T): Validator<T> {
|
|
24
25
|
return new ConstantValidator(constant)
|
|
25
26
|
}
|
|
26
27
|
|