justus 0.4.2 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -0
- package/dist/dts-generator.cjs +36 -22
- package/dist/dts-generator.cjs.map +1 -1
- package/dist/dts-generator.d.ts +1 -1
- package/dist/dts-generator.mjs +15 -22
- package/dist/dts-generator.mjs.map +1 -1
- package/dist/extra/arn.cjs +101 -0
- package/dist/extra/arn.cjs.map +6 -0
- package/dist/extra/arn.d.ts +73 -0
- package/dist/extra/arn.mjs +71 -0
- package/dist/extra/arn.mjs.map +6 -0
- package/dist/extra/ean13.cjs +52 -0
- package/dist/extra/ean13.cjs.map +6 -0
- package/dist/extra/ean13.d.ts +9 -0
- package/dist/extra/ean13.mjs +26 -0
- package/dist/extra/ean13.mjs.map +6 -0
- package/dist/{validators → extra}/url.cjs +4 -6
- package/dist/{validators → extra}/url.cjs.map +3 -3
- package/dist/{validators → extra}/url.d.ts +2 -2
- package/dist/{validators → extra}/url.mjs +4 -6
- package/dist/{validators → extra}/url.mjs.map +3 -3
- package/dist/extra/uuid.cjs +45 -0
- package/dist/extra/uuid.cjs.map +6 -0
- package/dist/extra/uuid.d.ts +10 -0
- package/dist/extra/uuid.mjs +18 -0
- package/dist/extra/uuid.mjs.map +6 -0
- package/dist/index.cjs +0 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.mjs +0 -3
- package/dist/index.mjs.map +1 -1
- package/dist/registry.cjs +44 -0
- package/dist/registry.cjs.map +6 -0
- package/dist/registry.d.ts +16 -0
- package/dist/registry.mjs +19 -0
- package/dist/registry.mjs.map +6 -0
- package/dist/schema.cjs +3 -5
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.ts +6 -5
- package/dist/schema.mjs +3 -4
- package/dist/schema.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +38 -12
- package/dist/types.mjs.map +1 -1
- package/dist/utilities.cjs +5 -7
- package/dist/utilities.cjs.map +1 -1
- package/dist/utilities.mjs +5 -7
- package/dist/utilities.mjs.map +1 -1
- package/dist/validators/array.cjs.map +1 -1
- package/dist/validators/array.d.ts +1 -1
- package/dist/validators/array.mjs.map +1 -1
- package/dist/validators/constant.cjs +4 -5
- package/dist/validators/constant.cjs.map +1 -1
- package/dist/validators/constant.d.ts +0 -2
- package/dist/validators/constant.mjs +3 -3
- package/dist/validators/constant.mjs.map +1 -1
- package/dist/validators/date.cjs +2 -6
- package/dist/validators/date.cjs.map +1 -1
- package/dist/validators/date.d.ts +1 -1
- package/dist/validators/date.mjs +2 -6
- package/dist/validators/date.mjs.map +1 -1
- package/dist/validators/number.cjs.map +1 -1
- package/dist/validators/number.d.ts +1 -1
- package/dist/validators/number.mjs.map +1 -1
- package/dist/validators/object.cjs +3 -2
- package/dist/validators/object.cjs.map +1 -1
- package/dist/validators/object.d.ts +4 -4
- package/dist/validators/object.mjs +4 -7
- package/dist/validators/object.mjs.map +1 -1
- package/dist/validators/optional.cjs.map +1 -1
- package/dist/validators/optional.d.ts +5 -4
- package/dist/validators/optional.mjs.map +1 -1
- package/dist/validators/string.cjs.map +1 -1
- package/dist/validators/string.d.ts +1 -1
- package/dist/validators/string.mjs.map +1 -1
- package/dist/validators/tuple.cjs +3 -2
- package/dist/validators/tuple.cjs.map +1 -1
- package/dist/validators/tuple.d.ts +3 -3
- package/dist/validators/tuple.mjs +3 -2
- package/dist/validators/tuple.mjs.map +1 -1
- package/dist/validators/union.cjs.map +1 -1
- package/dist/validators/union.d.ts +9 -7
- package/dist/validators/union.mjs.map +1 -1
- package/package.json +43 -3
- package/src/dts-generator.ts +17 -27
- package/src/extra/arn.ts +172 -0
- package/src/extra/ean13.ts +31 -0
- package/src/{validators → extra}/url.ts +4 -6
- package/src/extra/uuid.ts +18 -0
- package/src/index.ts +0 -2
- package/src/registry.ts +28 -0
- package/src/schema.ts +10 -11
- package/src/types.ts +80 -11
- package/src/utilities.ts +5 -9
- package/src/validators/array.ts +1 -1
- package/src/validators/constant.ts +3 -2
- package/src/validators/date.ts +7 -7
- package/src/validators/number.ts +1 -1
- package/src/validators/object.ts +13 -12
- package/src/validators/optional.ts +9 -7
- package/src/validators/string.ts +1 -1
- package/src/validators/tuple.ts +20 -6
- package/src/validators/union.ts +35 -10
|
@@ -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,SAAS,UAAU,yBAAyB;AACpE,WAAO;AAAA,EACT;AACF;AAGO,IAAM,kBAAN,
|
|
4
|
+
"mappings": ";AAAA,SAAS,cAAc,wBAAwB;AAC/C,SAAS,mBAAmB,4BAA4B;AAqBjD,IAAM,qBAAN,cAAiC,kBAA0B;AAAA,EAChE,SAAS,OAAwB;AAC/B,qBAAiB,OAAO,SAAS,UAAU,yBAAyB;AACpE,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,SAAS,UAAU,yBAAyB;AAEpE;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
|
}
|
|
@@ -25,9 +25,9 @@ __export(tuple_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(tuple_exports);
|
|
27
27
|
var import_errors = require("../errors.cjs");
|
|
28
|
+
var import_registry = require("../registry.cjs");
|
|
28
29
|
var import_types = require("../types.cjs");
|
|
29
30
|
var import_utilities = require("../utilities.cjs");
|
|
30
|
-
var import_constant = require("./constant.cjs");
|
|
31
31
|
var TupleValidator = class extends import_types.AbstractValidator {
|
|
32
32
|
members;
|
|
33
33
|
tuple;
|
|
@@ -36,7 +36,7 @@ var TupleValidator = class extends import_types.AbstractValidator {
|
|
|
36
36
|
const members = [];
|
|
37
37
|
for (const item of tuple2) {
|
|
38
38
|
if (item === null) {
|
|
39
|
-
members.push({ single: true, validator:
|
|
39
|
+
members.push({ single: true, validator: (0, import_utilities.getValidator)(null) });
|
|
40
40
|
} else if (typeof item === "object" && Symbol.justusRestValidator in item) {
|
|
41
41
|
members.push({ single: false, validator: item[Symbol.justusRestValidator] });
|
|
42
42
|
} else {
|
|
@@ -87,6 +87,7 @@ var TupleValidator = class extends import_types.AbstractValidator {
|
|
|
87
87
|
function tuple(tuple2) {
|
|
88
88
|
return new TupleValidator(tuple2);
|
|
89
89
|
}
|
|
90
|
+
import_registry.registry.set("tuple", TupleValidator);
|
|
90
91
|
// Annotate the CommonJS export names for ESM import in node:
|
|
91
92
|
0 && (module.exports = {
|
|
92
93
|
TupleValidator,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/tuple.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAkD;AAClD,mBAAkC;
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAkD;AAClD,sBAAyB;AACzB,mBAAkC;AAClC,uBAA6B;AAetB,IAAM,iBAAN,cAA8C,+BAAqD;AAAA,EAC/F;AAAA,EACA;AAAA,EAET,YAAYA,QAAU;AACpB,UAAM;AAEN,UAAM,UAAyB,CAAC;AAChC,eAAW,QAAQA,QAAO;AACxB,UAAI,SAAS,MAAM;AACjB,gBAAQ,KAAK,EAAE,QAAQ,MAAM,eAAW,+BAAa,IAAI,EAAE,CAAC;AAAA,MAC9D,WAAY,OAAO,SAAS,YAAc,OAAO,uBAAuB,MAAO;AAC7E,gBAAQ,KAAK,EAAE,QAAQ,OAAO,WAAiB,KAAM,OAAO,mBAAmB,EAAE,CAAC;AAAA,MACpF,OAAO;AACL,gBAAQ,KAAK,EAAE,QAAQ,MAAM,eAAW,+BAAa,IAAI,EAAE,CAAC;AAAA,MAC9D;AAAA,IACF;AAEA,SAAK,UAAU;AACf,SAAK,QAAQA;AAAA,EACf;AAAA,EAEA,SAAS,OAAgB,SAA4C;AACnE,wCAAiB,MAAM,QAAQ,KAAK,GAAG,yBAAyB;AAGhE,QAAI,KAAK,QAAQ,WAAW,GAAG;AAC7B,YAAM,OAAO,MAAM;AACnB,0CAAiB,SAAS,GAAG,SAAS,IAAI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'../types';
|
|
2
|
-
import type { InferTuple, Tuple, ValidationOptions, Validator } from '../types';
|
|
2
|
+
import type { InferInputTuple, InferTuple, Tuple, TupleRestParameter, Validation, ValidationOptions, Validator } from '../types';
|
|
3
3
|
export interface TupleMember {
|
|
4
4
|
single: boolean;
|
|
5
5
|
validator: Validator;
|
|
6
6
|
}
|
|
7
7
|
/** A `Validator` for _tuples_. */
|
|
8
|
-
export declare class TupleValidator<T extends Tuple> extends AbstractValidator<InferTuple<T>> {
|
|
8
|
+
export declare class TupleValidator<T extends Tuple> extends AbstractValidator<InferTuple<T>, InferInputTuple<T>> {
|
|
9
9
|
readonly members: readonly TupleMember[];
|
|
10
10
|
readonly tuple: T;
|
|
11
11
|
constructor(tuple: T);
|
|
12
12
|
validate(value: unknown, options?: ValidationOptions): InferTuple<T>;
|
|
13
13
|
}
|
|
14
14
|
/** Validate _tuples_. */
|
|
15
|
-
export declare function tuple<T extends Tuple>(tuple: T): Validator<InferTuple<T>>;
|
|
15
|
+
export declare function tuple<T extends readonly [Validation | TupleRestParameter<any, any>, ...Tuple] | readonly [...Tuple, Validation | TupleRestParameter<any, any>] | readonly [...Tuple]>(tuple: T): Validator<InferTuple<T>, InferInputTuple<T>>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// validators/tuple.ts
|
|
2
2
|
import { assertValidation, ValidationError } from "../errors.mjs";
|
|
3
|
+
import { registry } from "../registry.mjs";
|
|
3
4
|
import { AbstractValidator } from "../types.mjs";
|
|
4
5
|
import { getValidator } from "../utilities.mjs";
|
|
5
|
-
import { nullValidator } from "./constant.mjs";
|
|
6
6
|
var TupleValidator = class extends AbstractValidator {
|
|
7
7
|
members;
|
|
8
8
|
tuple;
|
|
@@ -11,7 +11,7 @@ var TupleValidator = class extends AbstractValidator {
|
|
|
11
11
|
const members = [];
|
|
12
12
|
for (const item of tuple2) {
|
|
13
13
|
if (item === null) {
|
|
14
|
-
members.push({ single: true, validator:
|
|
14
|
+
members.push({ single: true, validator: getValidator(null) });
|
|
15
15
|
} else if (typeof item === "object" && Symbol.justusRestValidator in item) {
|
|
16
16
|
members.push({ single: false, validator: item[Symbol.justusRestValidator] });
|
|
17
17
|
} else {
|
|
@@ -62,6 +62,7 @@ var TupleValidator = class extends AbstractValidator {
|
|
|
62
62
|
function tuple(tuple2) {
|
|
63
63
|
return new TupleValidator(tuple2);
|
|
64
64
|
}
|
|
65
|
+
registry.set("tuple", TupleValidator);
|
|
65
66
|
export {
|
|
66
67
|
TupleValidator,
|
|
67
68
|
tuple
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/tuple.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,kBAAkB,uBAAuB;AAClD,SAAS,
|
|
4
|
+
"mappings": ";AAAA,SAAS,kBAAkB,uBAAuB;AAClD,SAAS,gBAAgB;AACzB,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAetB,IAAM,iBAAN,cAA8C,kBAAqD;AAAA,EAC/F;AAAA,EACA;AAAA,EAET,YAAYA,QAAU;AACpB,UAAM;AAEN,UAAM,UAAyB,CAAC;AAChC,eAAW,QAAQA,QAAO;AACxB,UAAI,SAAS,MAAM;AACjB,gBAAQ,KAAK,EAAE,QAAQ,MAAM,WAAW,aAAa,IAAI,EAAE,CAAC;AAAA,MAC9D,WAAY,OAAO,SAAS,YAAc,OAAO,uBAAuB,MAAO;AAC7E,gBAAQ,KAAK,EAAE,QAAQ,OAAO,WAAiB,KAAM,OAAO,mBAAmB,EAAE,CAAC;AAAA,MACpF,OAAO;AACL,gBAAQ,KAAK,EAAE,QAAQ,MAAM,WAAW,aAAa,IAAI,EAAE,CAAC;AAAA,MAC9D;AAAA,IACF;AAEA,SAAK,UAAU;AACf,SAAK,QAAQA;AAAA,EACf;AAAA,EAEA,SAAS,OAAgB,SAA4C;AACnE,qBAAiB,MAAM,QAAQ,KAAK,GAAG,yBAAyB;AAGhE,QAAI,K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AO;AAAA,IAC3C;AACA,WAAO;AAAA,EACT;AACF;AAGO,SAAS,SAAmC,MAA4B;AAC7E,SAAO,IAAI,eAAe,IAAI;AAChC;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { AbstractValidator } from '../types';
|
|
2
|
-
import type { InferValidation, Validation, ValidationOptions, Validator } from '../types';
|
|
2
|
+
import type { InferInput, InferValidation, Validation, ValidationOptions, Validator } from '../types';
|
|
3
3
|
export type OneOfArguments = readonly Validation[];
|
|
4
|
-
export type
|
|
4
|
+
export type InferOneOfValidation<A extends OneOfArguments> = A extends readonly [infer First, ...infer Rest] ? First extends Validation ? Rest extends OneOfArguments ? InferValidation<First> | InferOneOfValidation<Rest> : InferValidation<First> : never : A extends readonly (infer Type)[] ? Type extends Validation ? InferValidation<Type> : never : never;
|
|
5
|
+
export type InferOneOfInput<A extends OneOfArguments> = A extends readonly [infer First, ...infer Rest] ? First extends Validation ? Rest extends OneOfArguments ? InferInput<First> | InferOneOfValidation<Rest> : InferInput<First> : never : A extends readonly (infer Type)[] ? Type extends Validation ? InferInput<Type> : never : never;
|
|
5
6
|
/** A `Validator` validating a value as _one of_ the specified arguments. */
|
|
6
|
-
export declare class OneOfValidator<A extends OneOfArguments> extends AbstractValidator<
|
|
7
|
+
export declare class OneOfValidator<A extends OneOfArguments> extends AbstractValidator<InferOneOfValidation<A>, InferOneOfInput<A>> {
|
|
7
8
|
readonly validators: readonly Validator[];
|
|
8
9
|
constructor(args: A);
|
|
9
|
-
validate(value: unknown, options?: ValidationOptions):
|
|
10
|
+
validate(value: unknown, options?: ValidationOptions): InferOneOfValidation<A>;
|
|
10
11
|
}
|
|
11
12
|
/** Validate a value as _one of_ the specified arguments */
|
|
12
13
|
export declare function oneOf<A extends OneOfArguments>(...args: A): OneOfValidator<A>;
|
|
13
14
|
export type AllOfArguments = readonly [Validation, ...Validation[]];
|
|
14
|
-
export type
|
|
15
|
+
export type InferAllOfValidation<A extends AllOfArguments> = A extends readonly [infer First, ...infer Rest] ? First extends Validation ? Rest extends AllOfArguments ? InferValidation<First> & InferAllOfValidation<Rest> : InferValidation<First> : never : never;
|
|
16
|
+
export type InferAllOfInput<A extends AllOfArguments> = A extends readonly [infer First, ...infer Rest] ? First extends Validation ? Rest extends AllOfArguments ? InferInput<First> & InferAllOfValidation<Rest> : InferInput<First> : never : never;
|
|
15
17
|
/** A `Validator` validating a value as _all of_ the specified arguments. */
|
|
16
|
-
export declare class AllOfValidator<A extends AllOfArguments> extends AbstractValidator<
|
|
18
|
+
export declare class AllOfValidator<A extends AllOfArguments> extends AbstractValidator<InferAllOfValidation<A>, InferAllOfInput<A>> {
|
|
17
19
|
readonly validators: readonly Validator[];
|
|
18
20
|
constructor(args: A);
|
|
19
|
-
validate(value: unknown, options?: ValidationOptions):
|
|
21
|
+
validate(value: unknown, options?: ValidationOptions): InferAllOfValidation<A>;
|
|
20
22
|
}
|
|
21
23
|
/** Validate a value as _all of_ the specified arguments */
|
|
22
24
|
export declare function allOf<A extends AllOfArguments>(...args: A): AllOfValidator<A>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/union.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,cAAc,8BAA8B;AACrD,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;
|
|
4
|
+
"mappings": ";AAAA,SAAS,cAAc,8BAA8B;AACrD,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAyCtB,IAAM,iBAAN,cACG,kBAA+D;AAAA,EAC9D;AAAA,EAET,YAAY,MAAS;AACnB,UAAM;AACN,SAAK,aAAa,KAAK,IAAI,CAAC,eAAe,aAAa,UAAU,CAAC;AACnE,iBAAa,KAAK,WAAW,SAAS,GAAG,6CAA6C;AAAA,EACxF;AAAA,EAEA,SAAS,OAAgB,SAAsD;AAC7E,UAAM,UAAU,IAAI,uBAAuB;AAC3C,eAAW,aAAa,KAAK,YAAY;AACvC,UAAI;AACF,eAAO,UAAU,SAAS,OAAO,OAAO;AAAA,MAC1C,SAAS,OAAO;AACd,gBAAQ,OAAO,KAAK;AAAA,MACtB;AAAA,IACF;AACA,WAAO,QAAQ,OAAO,KAAgC;AAAA,EACxD;AACF;AAGO,SAAS,SAAmC,MAA4B;AAC7E,SAAO,IAAI,eAAe,IAAI;AAChC;AAyBO,IAAM,iBAAN,cACG,kBAA+D;AAAA,EAC9D;AAAA,EAET,YAAY,MAAS;AACnB,UAAM;AACN,SAAK,aAAa,KAAK,IAAI,CAAC,eAAe,aAAa,UAAU,CAAC;AACnE,iBAAa,KAAK,WAAW,SAAS,GAAG,6CAA6C;AAAA,EACxF;AAAA,EAEA,SAAS,OAAgB,SAAsD;AAC7E,eAAW,aAAa,KAAK,YAAY;AACvC,cAAQ,UAAU,SAAS,OAAO,OAAO;AAAA,IAC3C;AACA,WAAO;AAAA,EACT;AACF;AAGO,SAAS,SAAmC,MAA4B;AAC7E,SAAO,IAAI,eAAe,IAAI;AAChC;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "justus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "A JavaScript validation library, with types!",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -24,6 +24,46 @@
|
|
|
24
24
|
"types": "./dist/dts-generator.d.ts",
|
|
25
25
|
"default": "./dist/dts-generator.mjs"
|
|
26
26
|
}
|
|
27
|
+
},
|
|
28
|
+
"./extra/arn": {
|
|
29
|
+
"require": {
|
|
30
|
+
"types": "./dist/extra/arn.d.ts",
|
|
31
|
+
"default": "./dist/extra/arn.cjs"
|
|
32
|
+
},
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/extra/arn.d.ts",
|
|
35
|
+
"default": "./dist/extra/arn.mjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"./extra/ean13": {
|
|
39
|
+
"require": {
|
|
40
|
+
"types": "./dist/extra/ean13.d.ts",
|
|
41
|
+
"default": "./dist/extra/ean13.cjs"
|
|
42
|
+
},
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/extra/ean13.d.ts",
|
|
45
|
+
"default": "./dist/extra/ean13.mjs"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"./extra/url": {
|
|
49
|
+
"require": {
|
|
50
|
+
"types": "./dist/extra/url.d.ts",
|
|
51
|
+
"default": "./dist/extra/url.cjs"
|
|
52
|
+
},
|
|
53
|
+
"import": {
|
|
54
|
+
"types": "./dist/extra/url.d.ts",
|
|
55
|
+
"default": "./dist/extra/url.mjs"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"./extra/uuid": {
|
|
59
|
+
"require": {
|
|
60
|
+
"types": "./dist/extra/uuid.d.ts",
|
|
61
|
+
"default": "./dist/extra/uuid.cjs"
|
|
62
|
+
},
|
|
63
|
+
"import": {
|
|
64
|
+
"types": "./dist/extra/uuid.d.ts",
|
|
65
|
+
"default": "./dist/extra/uuid.mjs"
|
|
66
|
+
}
|
|
27
67
|
}
|
|
28
68
|
},
|
|
29
69
|
"types": "./dist/index.d.ts",
|
|
@@ -38,8 +78,8 @@
|
|
|
38
78
|
"author": "Juit Developers <developers@juit.com>",
|
|
39
79
|
"license": "Apache-2.0",
|
|
40
80
|
"devDependencies": {
|
|
41
|
-
"@plugjs/build": "^0.4.
|
|
42
|
-
"@plugjs/tsd": "^0.4.
|
|
81
|
+
"@plugjs/build": "^0.4.33",
|
|
82
|
+
"@plugjs/tsd": "^0.4.32",
|
|
43
83
|
"@types/chai": "^4.3.5",
|
|
44
84
|
"chai": "^4.3.8",
|
|
45
85
|
"typescript": "^5.2.2"
|
package/src/dts-generator.ts
CHANGED
|
@@ -1,32 +1,22 @@
|
|
|
1
1
|
import ts from 'typescript'
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
OptionalValidator,
|
|
21
|
-
StringValidator,
|
|
22
|
-
TupleValidator,
|
|
23
|
-
URLValidator,
|
|
24
|
-
} from './index'
|
|
25
|
-
|
|
26
|
-
import type {
|
|
27
|
-
Validation,
|
|
28
|
-
Validator,
|
|
29
|
-
} from './index'
|
|
3
|
+
import { assertSchema } from './errors'
|
|
4
|
+
import { URLValidator } from './extra/url'
|
|
5
|
+
import { getValidator } from './utilities'
|
|
6
|
+
import { AnyValidator } from './validators/any'
|
|
7
|
+
import { AnyArrayValidator, ArrayValidator } from './validators/array'
|
|
8
|
+
import { BooleanValidator } from './validators/boolean'
|
|
9
|
+
import { ConstantValidator } from './validators/constant'
|
|
10
|
+
import { DateValidator } from './validators/date'
|
|
11
|
+
import { NeverValidator } from './validators/never'
|
|
12
|
+
import { AnyNumberValidator, NumberValidator } from './validators/number'
|
|
13
|
+
import { AnyObjectValidator, ObjectValidator } from './validators/object'
|
|
14
|
+
import { OptionalValidator } from './validators/optional'
|
|
15
|
+
import { AnyStringValidator, StringValidator } from './validators/string'
|
|
16
|
+
import { TupleValidator } from './validators/tuple'
|
|
17
|
+
import { AllOfValidator, OneOfValidator } from './validators/union'
|
|
18
|
+
|
|
19
|
+
import type { Validation, Validator } from './types'
|
|
30
20
|
|
|
31
21
|
/* ========================================================================== *
|
|
32
22
|
* LOCAL TYPES *
|
package/src/extra/arn.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { assertValidation } from '../errors'
|
|
2
|
+
import { AbstractValidator, makeValidatorFactory } from '../types'
|
|
3
|
+
|
|
4
|
+
/* ===== TYPES ============================================================= */
|
|
5
|
+
|
|
6
|
+
/** The type for a validated _string_ ARN (Amazon Resource Name) */
|
|
7
|
+
export type ArnString<
|
|
8
|
+
Service extends string = string,
|
|
9
|
+
ResourceType extends string = string,
|
|
10
|
+
> = string & { __arn: never }
|
|
11
|
+
& ( string extends Service ? string : { [ k in `__arn_service_${Service}`] : never })
|
|
12
|
+
& ( string extends ResourceType ? string : { [ k in `__arn_resource_${ResourceType}`] : never })
|
|
13
|
+
|
|
14
|
+
/** The type for a _parsed_ ARN (Amazon Resource Name) */
|
|
15
|
+
export interface ParsedArn<
|
|
16
|
+
Service extends string = string,
|
|
17
|
+
ResourceType extends string = string,
|
|
18
|
+
> {
|
|
19
|
+
/** The full */
|
|
20
|
+
Arn: ArnString<Service, ResourceType>,
|
|
21
|
+
Partition: string,
|
|
22
|
+
Service: Service,
|
|
23
|
+
Region: string,
|
|
24
|
+
Account: string,
|
|
25
|
+
Resource: [ ResourceType, ...string[] ],
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* ===== INTERNALS ========================================================== */
|
|
29
|
+
|
|
30
|
+
function assertComponent<T extends string>(
|
|
31
|
+
actual: string,
|
|
32
|
+
expected: T | undefined,
|
|
33
|
+
kind: string,
|
|
34
|
+
): asserts actual is T {
|
|
35
|
+
if (expected === undefined) return
|
|
36
|
+
assertValidation(actual === expected, `ARN ${kind} "${actual}" mismatch (expected "${expected}")`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Validate a string and convert it into into an {@link ParsedArn} */
|
|
40
|
+
function parse<Service extends string, ResourceType extends string>(
|
|
41
|
+
value: unknown,
|
|
42
|
+
service?: Service,
|
|
43
|
+
type?: ResourceType,
|
|
44
|
+
): ParsedArn<Service, ResourceType> {
|
|
45
|
+
assertValidation(typeof value == 'string', 'Value is not a "string"')
|
|
46
|
+
|
|
47
|
+
const segments = value.split(':')
|
|
48
|
+
|
|
49
|
+
assertValidation(segments.length >= 6, 'Invalid components in ARN')
|
|
50
|
+
|
|
51
|
+
const [ pfx, prt, svc, rgn, act, ...res ] = segments
|
|
52
|
+
|
|
53
|
+
assertValidation(pfx === 'arn', 'ARN must start with "arn:"')
|
|
54
|
+
assertValidation(!! prt, 'Missing partition in ARN')
|
|
55
|
+
assertValidation(!! svc, 'Missing service in ARN')
|
|
56
|
+
assertValidation(!! act, 'Missing account ID in ARN')
|
|
57
|
+
assertValidation(!! res[0], 'Missing resource ID in ARN')
|
|
58
|
+
|
|
59
|
+
const [ [ resType, ...resArray ], resString ] = res[0].includes('/') ?
|
|
60
|
+
[ res[0].split('/'), res[0] ] :
|
|
61
|
+
[ res, res.join(':') ]
|
|
62
|
+
|
|
63
|
+
assertValidation(!! resType, 'Invalid resource ID in ARN')
|
|
64
|
+
|
|
65
|
+
assertComponent(svc, service, 'Service')
|
|
66
|
+
assertComponent(resType, type, 'Resource Type')
|
|
67
|
+
|
|
68
|
+
const arn = `arn:${prt}:${svc}:${rgn}:${act}:${resString}`
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
Arn: arn as ArnString<Service, ResourceType>,
|
|
72
|
+
Partition: prt,
|
|
73
|
+
Service: svc, // as Service,
|
|
74
|
+
Region: rgn || '',
|
|
75
|
+
Account: act,
|
|
76
|
+
Resource: [ resType, ...resArray ], // as [ ResourceType, ...string[] ],
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* ===== VALIDATOR CLASSES ================================================== */
|
|
81
|
+
|
|
82
|
+
/** Validator parsing an ARN (Amazon Resource Name) and returning its components */
|
|
83
|
+
export class ParsedArnValidator<
|
|
84
|
+
Service extends string = string,
|
|
85
|
+
ResourceType extends string = string,
|
|
86
|
+
> extends AbstractValidator<ParsedArn<Service, ResourceType>, string> {
|
|
87
|
+
/**
|
|
88
|
+
* Create a new {@link ParsedArnValidator} instance.
|
|
89
|
+
*
|
|
90
|
+
* @param service The (optional) service the ARN should be pointing to
|
|
91
|
+
* (e.g. `iam` or `elasticloadbalancing`)
|
|
92
|
+
* @param resourceType The (optional) resource _type_ the ARN should be
|
|
93
|
+
* representing (e.g. `role` in the `iam` service, or
|
|
94
|
+
* `targetgroup` in the `elasticloadbalancing` service)
|
|
95
|
+
*/
|
|
96
|
+
constructor(service?: Service, resourceType?: ResourceType)
|
|
97
|
+
constructor(private _service?: Service, private _type?: ResourceType) {
|
|
98
|
+
super()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
validate(value: unknown): ParsedArn<Service, ResourceType> {
|
|
102
|
+
return parse(value, this._service, this._type)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Validator validating an ARN (Amazon Resource Name) _string_ */
|
|
107
|
+
export class ArnValidator<
|
|
108
|
+
Service extends string = string,
|
|
109
|
+
ResourceType extends string = string,
|
|
110
|
+
> extends AbstractValidator<ArnString<Service, ResourceType>, string> {
|
|
111
|
+
/**
|
|
112
|
+
* Create a new {@link ArnValidator} instance.
|
|
113
|
+
*
|
|
114
|
+
* @param service The (optional) service the ARN should be pointing to
|
|
115
|
+
* (e.g. `iam` or `elasticloadbalancing`)
|
|
116
|
+
* @param resourceType The (optional) resource _type_ the ARN should be
|
|
117
|
+
* representing (e.g. `role` in the `iam` service, or
|
|
118
|
+
* `targetgroup` in the `elasticloadbalancing` service)
|
|
119
|
+
*/
|
|
120
|
+
constructor(service?: Service, resourceType?: ResourceType)
|
|
121
|
+
constructor(private _service?: Service, private _type?: ResourceType) {
|
|
122
|
+
super()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
validate(value: unknown): ArnString<Service, ResourceType> {
|
|
126
|
+
return parse(value, this._service, this._type).Arn
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ===== VALIDATOR FACTORIES ================================================ */
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Create a new {@link ParsedArnValidator} parsing an ARN referring to the
|
|
134
|
+
* specified `service` (e.g. `iam` or `elasticloadbalancing`).
|
|
135
|
+
*
|
|
136
|
+
* An (optional) resource _type_ can be specified, and will validate the first
|
|
137
|
+
* component of the ARN's resource (e.g. `role` in the `iam` service, or
|
|
138
|
+
* `targetgroup` in the `elasticloadbalancing` service)
|
|
139
|
+
*/
|
|
140
|
+
export function parseArnFactory<
|
|
141
|
+
Service extends string = string,
|
|
142
|
+
ResourceType extends string = string>(
|
|
143
|
+
service: Service,
|
|
144
|
+
resourceType?: ResourceType,
|
|
145
|
+
): ParsedArnValidator<Service, ResourceType> {
|
|
146
|
+
return new ParsedArnValidator(service, resourceType)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Create a new {@link ArnValidator} validating an ARN referring to the
|
|
151
|
+
* specified `service` (e.g. `iam` or `elasticloadbalancing`).
|
|
152
|
+
*
|
|
153
|
+
* An (optional) resource _type_ can be specified, and will validate the first
|
|
154
|
+
* component of the ARN's resource (e.g. `role` in the `iam` service, or
|
|
155
|
+
* `targetgroup` in the `elasticloadbalancing` service)
|
|
156
|
+
*/
|
|
157
|
+
export function arnFactory<
|
|
158
|
+
Service extends string = string,
|
|
159
|
+
ResourceType extends string = string>(
|
|
160
|
+
service: Service,
|
|
161
|
+
resourceType?: ResourceType,
|
|
162
|
+
): ArnValidator<Service, ResourceType> {
|
|
163
|
+
return new ArnValidator(service, resourceType)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/* ===== VALIDATOR CONSTANTS ================================================ */
|
|
167
|
+
|
|
168
|
+
/** Validate a string and parse it into into an {@link ParsedArn} */
|
|
169
|
+
export const parseArn = makeValidatorFactory(new ParsedArnValidator(), parseArnFactory)
|
|
170
|
+
|
|
171
|
+
/** Validate a ARN (Amazon Resource Name) string */
|
|
172
|
+
export const arn = makeValidatorFactory(new ArnValidator(), arnFactory)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { assertValidation } from '../errors'
|
|
2
|
+
import { StringValidator } from '../validators/string'
|
|
3
|
+
|
|
4
|
+
export type EAN13String = string & { __ean_13: never }
|
|
5
|
+
|
|
6
|
+
export class EAN13Validator extends StringValidator<EAN13String, string | number> {
|
|
7
|
+
constructor() {
|
|
8
|
+
super({ minLength: 13, maxLength: 13, pattern: /^\d{13}$/ })
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
validate(value: unknown): EAN13String
|
|
12
|
+
validate(value: unknown): string {
|
|
13
|
+
if (typeof value === 'number') value = `${value}`
|
|
14
|
+
const ean13 = super.validate(value).toLowerCase()
|
|
15
|
+
|
|
16
|
+
/* Calculate the weighted sum for the first 12 digits */
|
|
17
|
+
let sum = 0
|
|
18
|
+
for (let i = 0, w = 1; i < 12; i ++, w = i % 2 ? 3 : 1) {
|
|
19
|
+
sum += (ean13.charCodeAt(i) - 0x30) * w
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* 10 - sum should match our 13th digit */
|
|
23
|
+
const valid = (10 - (sum % 10)) % 10 === ean13.charCodeAt(12) - 0x30
|
|
24
|
+
|
|
25
|
+
/* Assert validity of the checksum and return */
|
|
26
|
+
assertValidation(valid, `Invalid checksum in EAN-13 "${ean13}"`)
|
|
27
|
+
return ean13
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const ean13 = new EAN13Validator()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ValidationError, ValidationErrorBuilder } from '../errors'
|
|
2
2
|
import { AbstractValidator, makeValidatorFactory } from '../types'
|
|
3
|
-
import { ConstantValidator } from '
|
|
4
|
-
import { ObjectValidator } from '
|
|
3
|
+
import { ConstantValidator } from '../validators/constant'
|
|
4
|
+
import { ObjectValidator } from '../validators/object'
|
|
5
5
|
|
|
6
6
|
import type { ValidationOptions } from '..'
|
|
7
7
|
import type { Schema, Validator } from '../types'
|
|
@@ -60,7 +60,7 @@ export interface URLConstraints {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/** A `Validator` validating URLs and converting them to `URL` instances. */
|
|
63
|
-
export class URLValidator extends AbstractValidator<URL> {
|
|
63
|
+
export class URLValidator extends AbstractValidator<URL, URL | string> {
|
|
64
64
|
readonly href?: Validator<string>
|
|
65
65
|
readonly origin?: Validator<string>
|
|
66
66
|
readonly protocol?: Validator<string>
|
|
@@ -115,9 +115,7 @@ export class URLValidator extends AbstractValidator<URL> {
|
|
|
115
115
|
|
|
116
116
|
if (this.searchParams) {
|
|
117
117
|
const parameters: Record<string, string> = {}
|
|
118
|
-
|
|
119
|
-
parameters[param] = url.searchParams.get(param) as string
|
|
120
|
-
}
|
|
118
|
+
url.searchParams.forEach((value, key) => parameters[key] = value)
|
|
121
119
|
|
|
122
120
|
try {
|
|
123
121
|
this.searchParams.validate(parameters, OPTIONS)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { StringValidator } from '../validators/string'
|
|
2
|
+
|
|
3
|
+
export const UUID_EXPR = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
4
|
+
|
|
5
|
+
export type UUIDString = string & { __uuid: never }
|
|
6
|
+
|
|
7
|
+
export class UUIDValidator extends StringValidator<UUIDString> {
|
|
8
|
+
constructor() {
|
|
9
|
+
super({ minLength: 36, maxLength: 36, pattern: UUID_EXPR })
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
validate(value: unknown): UUIDString
|
|
13
|
+
validate(value: unknown): string {
|
|
14
|
+
return super.validate(value).toLowerCase()
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const uuid = new UUIDValidator()
|
package/src/index.ts
CHANGED
|
@@ -21,7 +21,6 @@ export { OptionalValidator, optional } from './validators/optional'
|
|
|
21
21
|
export { AnyStringValidator, StringValidator, string } from './validators/string'
|
|
22
22
|
export { TupleValidator, tuple } from './validators/tuple'
|
|
23
23
|
export { AllOfValidator, OneOfValidator, allOf, oneOf } from './validators/union'
|
|
24
|
-
export { URLValidator, url } from './validators/url'
|
|
25
24
|
|
|
26
25
|
// Validator Types
|
|
27
26
|
export type { ArrayConstraints, arrayFactory } from './validators/array'
|
|
@@ -31,7 +30,6 @@ export type { BrandedNumberConstraints, numberFactory } from './validators/numbe
|
|
|
31
30
|
export type { objectFactory } from './validators/object'
|
|
32
31
|
export type { BrandedStringConstraints, StringConstraints, stringFactory } from './validators/string'
|
|
33
32
|
export type { TupleMember } from './validators/tuple'
|
|
34
|
-
export type { URLConstraints, urlFactory } from './validators/url'
|
|
35
33
|
|
|
36
34
|
/* ========================================================================== *
|
|
37
35
|
* VALIDATE FUNCTION (our main entry point) *
|
package/src/registry.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { assertSchema } from './errors'
|
|
2
|
+
|
|
3
|
+
import type { ConstantValidator } from './validators/constant'
|
|
4
|
+
import type { ObjectValidator } from './validators/object'
|
|
5
|
+
import type { TupleValidator } from './validators/tuple'
|
|
6
|
+
|
|
7
|
+
type RegistryTypes = {
|
|
8
|
+
constant: typeof ConstantValidator,
|
|
9
|
+
object: typeof ObjectValidator,
|
|
10
|
+
tuple: typeof TupleValidator,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const _registry: Partial<RegistryTypes> = {}
|
|
14
|
+
|
|
15
|
+
/** Internal registry for validator constructors (avoids circular imports) */
|
|
16
|
+
export const registry = {
|
|
17
|
+
/** Registers the specified validator constructor */
|
|
18
|
+
set<K extends keyof RegistryTypes>(key: K, constructor: RegistryTypes[K]): void {
|
|
19
|
+
_registry[key] = constructor
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
/** Retrieves the specified validator constructor */
|
|
23
|
+
get<K extends keyof RegistryTypes>(key: K): RegistryTypes[K] {
|
|
24
|
+
const value = _registry[key]
|
|
25
|
+
assertSchema(!! value, `No validator found for "${key}"`)
|
|
26
|
+
return value
|
|
27
|
+
},
|
|
28
|
+
}
|
package/src/schema.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
// import { additionalValidator } from './types'
|
|
2
|
-
// eslint-disable-next-line import/no-cycle
|
|
3
1
|
import { getValidator } from './utilities'
|
|
4
2
|
import { any } from './validators/any'
|
|
5
3
|
|
|
6
4
|
import type {
|
|
7
|
-
AdditionalProperties,
|
|
5
|
+
AdditionalProperties,
|
|
6
|
+
InferValidation,
|
|
8
7
|
Validation,
|
|
9
8
|
Validator,
|
|
10
9
|
} from './types'
|
|
@@ -14,12 +13,12 @@ import type {
|
|
|
14
13
|
* ========================================================================== */
|
|
15
14
|
|
|
16
15
|
/** Internal definition of `allowAdditionalProperties(...)` */
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
function additionalProperties(): AdditionalProperties<Validator<any>>
|
|
17
|
+
function additionalProperties(allow: true): AdditionalProperties<Validator<any>>
|
|
18
|
+
function additionalProperties(allow: false): AdditionalProperties<false>
|
|
19
|
+
function additionalProperties<V extends Validation>(validation: V): AdditionalProperties<Validator<InferValidation<V>>>
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
function additionalProperties(options?: Validation | boolean): AdditionalProperties<Validator | false> {
|
|
23
22
|
if (options === false) return { [Symbol.justusAdditionalValidator]: false }
|
|
24
23
|
if (options === true) return { [Symbol.justusAdditionalValidator]: any }
|
|
25
24
|
|
|
@@ -35,9 +34,9 @@ export function _allowAdditionalProperties(options?: Validation | boolean): Addi
|
|
|
35
34
|
*
|
|
36
35
|
* @param allow - A `boolean` or a `Validator` instance
|
|
37
36
|
*/
|
|
38
|
-
export const allowAdditionalProperties =
|
|
39
|
-
typeof
|
|
37
|
+
export const allowAdditionalProperties = additionalProperties as
|
|
38
|
+
typeof additionalProperties & AdditionalProperties<Validator<any>>
|
|
40
39
|
|
|
41
40
|
// Remember to inject our `any` validator as the default for when
|
|
42
41
|
// `allowAdditionalProperties` is _not_ used as a function
|
|
43
|
-
|
|
42
|
+
additionalProperties[Symbol.justusAdditionalValidator] = any
|