pema 0.1.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/lib/private/AbstractConstructor.d.ts +3 -0
- package/lib/private/AbstractConstructor.js +2 -0
- package/lib/private/ArrayType.d.ts +12 -0
- package/lib/private/ArrayType.js +48 -0
- package/lib/private/BigIntType.d.ts +5 -0
- package/lib/private/BigIntType.js +7 -0
- package/lib/private/BlobType.d.ts +5 -0
- package/lib/private/BlobType.js +7 -0
- package/lib/private/BooleanType.d.ts +5 -0
- package/lib/private/BooleanType.js +7 -0
- package/lib/private/BuiltinType.d.ts +10 -0
- package/lib/private/BuiltinType.js +26 -0
- package/lib/private/ConstructorType.d.ts +12 -0
- package/lib/private/ConstructorType.js +28 -0
- package/lib/private/DateType.d.ts +5 -0
- package/lib/private/DateType.js +7 -0
- package/lib/private/DefaultType.d.ts +10 -0
- package/lib/private/DefaultType.js +27 -0
- package/lib/private/FileType.d.ts +5 -0
- package/lib/private/FileType.js +7 -0
- package/lib/private/GenericType.d.ts +7 -0
- package/lib/private/GenericType.js +10 -0
- package/lib/private/Infer.d.ts +4 -0
- package/lib/private/Infer.js +2 -0
- package/lib/private/InferSchema.d.ts +20 -0
- package/lib/private/InferSchema.js +2 -0
- package/lib/private/IntType.d.ts +7 -0
- package/lib/private/IntType.js +16 -0
- package/lib/private/LiteralType.d.ts +12 -0
- package/lib/private/LiteralType.js +24 -0
- package/lib/private/MaybeInt.d.ts +3 -0
- package/lib/private/MaybeInt.js +2 -0
- package/lib/private/NormalizeSchema.d.ts +16 -0
- package/lib/private/NormalizeSchema.js +2 -0
- package/lib/private/NullType.d.ts +7 -0
- package/lib/private/NullType.js +20 -0
- package/lib/private/NumberType.d.ts +5 -0
- package/lib/private/NumberType.js +7 -0
- package/lib/private/OptionalType.d.ts +10 -0
- package/lib/private/OptionalType.js +20 -0
- package/lib/private/PrimitiveType.d.ts +11 -0
- package/lib/private/PrimitiveType.js +31 -0
- package/lib/private/RecordType.d.ts +13 -0
- package/lib/private/RecordType.js +58 -0
- package/lib/private/RecordTypeKey.d.ts +6 -0
- package/lib/private/RecordTypeKey.js +2 -0
- package/lib/private/Schema.d.ts +7 -0
- package/lib/private/Schema.js +2 -0
- package/lib/private/SchemaType.d.ts +13 -0
- package/lib/private/SchemaType.js +63 -0
- package/lib/private/StringType.d.ts +8 -0
- package/lib/private/StringType.js +23 -0
- package/lib/private/SymbolType.d.ts +5 -0
- package/lib/private/SymbolType.js +7 -0
- package/lib/private/TupleType.d.ts +17 -0
- package/lib/private/TupleType.js +40 -0
- package/lib/private/Type.d.ts +10 -0
- package/lib/private/Type.js +15 -0
- package/lib/private/URLType.d.ts +5 -0
- package/lib/private/URLType.js +7 -0
- package/lib/private/UintType.d.ts +9 -0
- package/lib/private/UintType.js +16 -0
- package/lib/private/UndefinedType.d.ts +7 -0
- package/lib/private/UndefinedType.js +7 -0
- package/lib/private/UnionType.d.ts +18 -0
- package/lib/private/UnionType.js +63 -0
- package/lib/private/Validated.d.ts +8 -0
- package/lib/private/Validated.js +10 -0
- package/lib/private/ValidatedKey.d.ts +4 -0
- package/lib/private/ValidatedKey.js +3 -0
- package/lib/private/Validator.d.ts +3 -0
- package/lib/private/Validator.js +2 -0
- package/lib/private/array.d.ts +5 -0
- package/lib/private/array.js +3 -0
- package/lib/private/bigint.d.ts +4 -0
- package/lib/private/bigint.js +3 -0
- package/lib/private/blob.d.ts +4 -0
- package/lib/private/blob.js +3 -0
- package/lib/private/boolean.d.ts +4 -0
- package/lib/private/boolean.js +3 -0
- package/lib/private/constructor.d.ts +5 -0
- package/lib/private/constructor.js +3 -0
- package/lib/private/date.d.ts +4 -0
- package/lib/private/date.js +3 -0
- package/lib/private/error.d.ts +3 -0
- package/lib/private/error.js +4 -0
- package/lib/private/expect.d.ts +21 -0
- package/lib/private/expect.js +21 -0
- package/lib/private/expected.d.ts +3 -0
- package/lib/private/expected.js +2 -0
- package/lib/private/file.d.ts +4 -0
- package/lib/private/file.js +3 -0
- package/lib/private/index.d.ts +5 -0
- package/lib/private/index.js +43 -0
- package/lib/private/int.d.ts +4 -0
- package/lib/private/int.js +3 -0
- package/lib/private/is-int.d.ts +3 -0
- package/lib/private/is-int.js +5 -0
- package/lib/private/is_validated_type.d.ts +4 -0
- package/lib/private/is_validated_type.js +5 -0
- package/lib/private/literal.d.ts +5 -0
- package/lib/private/literal.js +3 -0
- package/lib/private/null.d.ts +4 -0
- package/lib/private/null.js +3 -0
- package/lib/private/number.d.ts +4 -0
- package/lib/private/number.js +3 -0
- package/lib/private/optional.d.ts +5 -0
- package/lib/private/optional.js +3 -0
- package/lib/private/record.d.ts +6 -0
- package/lib/private/record.js +3 -0
- package/lib/private/string.d.ts +4 -0
- package/lib/private/string.js +3 -0
- package/lib/private/symbol.d.ts +4 -0
- package/lib/private/symbol.js +3 -0
- package/lib/private/tuple.d.ts +5 -0
- package/lib/private/tuple.js +3 -0
- package/lib/private/uint.d.ts +4 -0
- package/lib/private/uint.js +3 -0
- package/lib/private/undefined.d.ts +4 -0
- package/lib/private/undefined.js +3 -0
- package/lib/private/union.d.ts +9 -0
- package/lib/private/union.js +3 -0
- package/lib/private/url.d.ts +4 -0
- package/lib/private/url.js +3 -0
- package/lib/public/Schema.d.ts +2 -0
- package/lib/public/Schema.js +2 -0
- package/lib/public/blob.d.ts +2 -0
- package/lib/public/blob.js +2 -0
- package/lib/public/boolean.d.ts +2 -0
- package/lib/public/boolean.js +2 -0
- package/lib/public/constructor.d.ts +2 -0
- package/lib/public/constructor.js +2 -0
- package/lib/public/date.d.ts +2 -0
- package/lib/public/date.js +2 -0
- package/lib/public/file.d.ts +2 -0
- package/lib/public/file.js +2 -0
- package/lib/public/index.d.ts +2 -0
- package/lib/public/index.js +2 -0
- package/lib/public/int.d.ts +2 -0
- package/lib/public/int.js +2 -0
- package/lib/public/number.d.ts +2 -0
- package/lib/public/number.js +2 -0
- package/lib/public/object.d.ts +2 -0
- package/lib/public/object.js +2 -0
- package/lib/public/optional.d.ts +2 -0
- package/lib/public/optional.js +2 -0
- package/lib/public/record.d.ts +2 -0
- package/lib/public/record.js +2 -0
- package/lib/public/string.d.ts +2 -0
- package/lib/public/string.js +2 -0
- package/lib/public/symbol.d.ts +2 -0
- package/lib/public/symbol.js +2 -0
- package/lib/public/union.d.ts +2 -0
- package/lib/public/union.js +2 -0
- package/package.json +45 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import type Validated from "#Validated";
|
|
5
|
+
export default class ArrayType<T extends Validated<unknown>> extends GenericType<T, Infer<T>[], "ArrayType"> {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(subtype: T);
|
|
8
|
+
optional(): OptionalType<this>;
|
|
9
|
+
get name(): string;
|
|
10
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=ArrayType.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import expected from "#expected";
|
|
2
|
+
import GenericType from "#GenericType";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
const member_error = (i, key) => {
|
|
5
|
+
return key === undefined
|
|
6
|
+
? `[${i}]`
|
|
7
|
+
: `${key}[${i}]`;
|
|
8
|
+
};
|
|
9
|
+
const error = (message, key) => {
|
|
10
|
+
return key === undefined
|
|
11
|
+
? message
|
|
12
|
+
: `${key}: ${message}`;
|
|
13
|
+
};
|
|
14
|
+
const is = (x, validator) => validator(x);
|
|
15
|
+
export default class ArrayType extends GenericType {
|
|
16
|
+
#subtype;
|
|
17
|
+
constructor(subtype) {
|
|
18
|
+
super();
|
|
19
|
+
this.#subtype = subtype;
|
|
20
|
+
}
|
|
21
|
+
optional() {
|
|
22
|
+
return new OptionalType(this);
|
|
23
|
+
}
|
|
24
|
+
get name() {
|
|
25
|
+
return "array";
|
|
26
|
+
}
|
|
27
|
+
validate(x, key) {
|
|
28
|
+
if (!is(x, _ => !!x && Array.isArray(x))) {
|
|
29
|
+
throw new Error(error(expected("array", x), key));
|
|
30
|
+
}
|
|
31
|
+
let last = 0;
|
|
32
|
+
x.forEach((v, i) => {
|
|
33
|
+
// sparse array check
|
|
34
|
+
if (i > last) {
|
|
35
|
+
throw new Error(error(expected(this.#subtype.name, undefined), `[${last}]`));
|
|
36
|
+
}
|
|
37
|
+
const validator = this.#subtype;
|
|
38
|
+
validator.validate(v, `${member_error(i, key)}`);
|
|
39
|
+
last++;
|
|
40
|
+
});
|
|
41
|
+
// sparse array with end slots
|
|
42
|
+
if (x.length > last) {
|
|
43
|
+
throw new Error(error(expected(this.#subtype.name, undefined), `[${last}]`));
|
|
44
|
+
}
|
|
45
|
+
return x;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=ArrayType.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import Type from "#Type";
|
|
3
|
+
import type AbstractorController from "@rcompat/type/AbstractConstructor";
|
|
4
|
+
export default class BuiltinType<StaticType, Name extends string> extends Type<StaticType, Name> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(name: string, type: AbstractorController);
|
|
7
|
+
get name(): string;
|
|
8
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=BuiltinType.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Type from "#Type";
|
|
2
|
+
const error_message = (name, x, key) => {
|
|
3
|
+
const base = `expected ${name}, got \`${x}\` (${(typeof x)})`;
|
|
4
|
+
return key === undefined
|
|
5
|
+
? base
|
|
6
|
+
: `${key}: ${base}`;
|
|
7
|
+
};
|
|
8
|
+
export default class BuiltinType extends Type {
|
|
9
|
+
#name;
|
|
10
|
+
#type;
|
|
11
|
+
constructor(name, type) {
|
|
12
|
+
super();
|
|
13
|
+
this.#name = name;
|
|
14
|
+
this.#type = type;
|
|
15
|
+
}
|
|
16
|
+
get name() {
|
|
17
|
+
return this.#name;
|
|
18
|
+
}
|
|
19
|
+
validate(x, key) {
|
|
20
|
+
if (!(x instanceof this.#type)) {
|
|
21
|
+
throw new Error(error_message(this.#name, x, key));
|
|
22
|
+
}
|
|
23
|
+
return x;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=BuiltinType.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import GenericType from "#GenericType";
|
|
3
|
+
import type Infer from "#Infer";
|
|
4
|
+
import type AbstractConstructor from "@rcompat/type/AbstractConstructor";
|
|
5
|
+
export default class ConstructorType<C extends AbstractConstructor> extends GenericType<C, InstanceType<C>, "InstanceType"> {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(t: C);
|
|
8
|
+
default(value: InstanceType<C> | (() => InstanceType<C>)): DefaultType<this, InstanceType<C>>;
|
|
9
|
+
get name(): string;
|
|
10
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=ConstructorType.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import GenericType from "#GenericType";
|
|
3
|
+
const error_message = (name, x, key) => {
|
|
4
|
+
const base = `expected ${name}, got \`${x}\` (${(typeof x)})`;
|
|
5
|
+
return key === undefined
|
|
6
|
+
? base
|
|
7
|
+
: `${key}: ${base}`;
|
|
8
|
+
};
|
|
9
|
+
export default class ConstructorType extends GenericType {
|
|
10
|
+
#type;
|
|
11
|
+
constructor(t) {
|
|
12
|
+
super();
|
|
13
|
+
this.#type = t;
|
|
14
|
+
}
|
|
15
|
+
default(value) {
|
|
16
|
+
return new DefaultType(this, value);
|
|
17
|
+
}
|
|
18
|
+
get name() {
|
|
19
|
+
return "constructor";
|
|
20
|
+
}
|
|
21
|
+
validate(x, key) {
|
|
22
|
+
if (!(x instanceof this.#type)) {
|
|
23
|
+
throw new Error(error_message(this.name, x, key));
|
|
24
|
+
}
|
|
25
|
+
return x;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=ConstructorType.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import type Validated from "#Validated";
|
|
4
|
+
export default class DefaultType<S extends Validated<unknown>, D extends Infer<S>> extends GenericType<S, Infer<S>, "DefaultType"> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(s: S, d: D | (() => D));
|
|
7
|
+
get name(): string;
|
|
8
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=DefaultType.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
const is_default_function = (x) => {
|
|
3
|
+
return typeof x === "function";
|
|
4
|
+
};
|
|
5
|
+
export default class DefaultType extends GenericType {
|
|
6
|
+
#schema;
|
|
7
|
+
#default;
|
|
8
|
+
constructor(s, d) {
|
|
9
|
+
super();
|
|
10
|
+
this.#schema = s;
|
|
11
|
+
this.#default = d;
|
|
12
|
+
}
|
|
13
|
+
get name() {
|
|
14
|
+
return "default";
|
|
15
|
+
}
|
|
16
|
+
validate(x, key) {
|
|
17
|
+
// default fallback
|
|
18
|
+
if (x === undefined) {
|
|
19
|
+
if (is_default_function(this.#default)) {
|
|
20
|
+
return this.#default();
|
|
21
|
+
}
|
|
22
|
+
return this.#default;
|
|
23
|
+
}
|
|
24
|
+
return this.#schema.validate(x, key);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=DefaultType.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Validated from "#Validated";
|
|
2
|
+
import type PrintableGeneric from "@rcompat/type/PrintableGeneric";
|
|
3
|
+
export default abstract class GenericType<Type, Inferred, Name extends string> extends Validated<Inferred> implements PrintableGeneric<Type> {
|
|
4
|
+
get Name(): Name;
|
|
5
|
+
get Type(): Type;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=GenericType.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type ArrayType from "#ArrayType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import type NullType from "#NullType";
|
|
4
|
+
import type Schema from "#Schema";
|
|
5
|
+
import type TupleType from "#TupleType";
|
|
6
|
+
import type UndefinedType from "#UndefinedType";
|
|
7
|
+
import type Validated from "#Validated";
|
|
8
|
+
import type UndefinedToOptional from "@rcompat/type/UndefinedToOptional";
|
|
9
|
+
type DecrementDepth = [never, 0, 1, 2, 3, 4];
|
|
10
|
+
type InferSchema<S, Depth extends number = 4> = [
|
|
11
|
+
Depth
|
|
12
|
+
] extends [never] ? never : S extends Validated<unknown> ? Infer<S> : S extends null ? Infer<NullType> : S extends undefined ? Infer<UndefinedType> : S extends [infer Only] ? Only extends Validated<unknown> ? Infer<ArrayType<Only>> : never : S extends Schema[] ? InferSchema<TupleType<{
|
|
13
|
+
[K in keyof S]: S[K] extends Validated<unknown> ? InferSchema<S[K], DecrementDepth[Depth]> : never;
|
|
14
|
+
}>> : S extends {
|
|
15
|
+
[key: string]: Schema;
|
|
16
|
+
} ? UndefinedToOptional<{
|
|
17
|
+
[K in keyof S]: InferSchema<S[K], DecrementDepth[Depth]>;
|
|
18
|
+
}> : never;
|
|
19
|
+
export type { InferSchema as default };
|
|
20
|
+
//# sourceMappingURL=InferSchema.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import PrimitiveType from "#PrimitiveType";
|
|
3
|
+
export default class IntType extends PrimitiveType<number | bigint, "IntType"> {
|
|
4
|
+
constructor();
|
|
5
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=IntType.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import PrimitiveType from "#PrimitiveType";
|
|
2
|
+
import error from "#error";
|
|
3
|
+
import expected from "#expected";
|
|
4
|
+
import is_int from "#is-int";
|
|
5
|
+
export default class IntType extends PrimitiveType {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("int");
|
|
8
|
+
}
|
|
9
|
+
validate(x, key) {
|
|
10
|
+
if (!is_int(x)) {
|
|
11
|
+
throw new Error(error(expected("int", x), key));
|
|
12
|
+
}
|
|
13
|
+
return x;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=IntType.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
type Literal = string;
|
|
4
|
+
type InferLiteral<T extends Literal> = T;
|
|
5
|
+
export default class LiteralType<T extends Literal> extends GenericType<T, InferLiteral<T>, "LiteralType"> {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(literal: T);
|
|
8
|
+
get name(): string;
|
|
9
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=LiteralType.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import expected from "#expected";
|
|
3
|
+
const error = (message, key) => {
|
|
4
|
+
return key === undefined
|
|
5
|
+
? message
|
|
6
|
+
: `${key}: ${message}`;
|
|
7
|
+
};
|
|
8
|
+
export default class LiteralType extends GenericType {
|
|
9
|
+
#literal;
|
|
10
|
+
constructor(literal) {
|
|
11
|
+
super();
|
|
12
|
+
this.#literal = literal;
|
|
13
|
+
}
|
|
14
|
+
get name() {
|
|
15
|
+
return "literal";
|
|
16
|
+
}
|
|
17
|
+
validate(x, key) {
|
|
18
|
+
if (x !== this.#literal) {
|
|
19
|
+
throw new Error(error(expected(`literal '${this.#literal}'`, x), key));
|
|
20
|
+
}
|
|
21
|
+
return x;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=LiteralType.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type ArrayType from "#ArrayType";
|
|
2
|
+
import type ConstructorType from "#ConstructorType";
|
|
3
|
+
import type LiteralType from "#LiteralType";
|
|
4
|
+
import type NullType from "#NullType";
|
|
5
|
+
import type Schema from "#Schema";
|
|
6
|
+
import type TupleType from "#TupleType";
|
|
7
|
+
import type UndefinedType from "#UndefinedType";
|
|
8
|
+
import type Validated from "#Validated";
|
|
9
|
+
import type AbstractConstructor from "@rcompat/type/AbstractConstructor";
|
|
10
|
+
type NormalizeSchema<S> = S extends [] ? TupleType<[]> : S extends Validated<unknown> ? S : S extends null ? NullType : S extends undefined ? UndefinedType : S extends AbstractConstructor ? ConstructorType<S> : S extends string ? LiteralType<S> : S extends [infer O] ? O extends Validated<unknown> ? ArrayType<NormalizeSchema<O>> : never : S extends Schema[] ? TupleType<S> : S extends {
|
|
11
|
+
[K: string]: Schema;
|
|
12
|
+
} ? {
|
|
13
|
+
-readonly [K in keyof S]: NormalizeSchema<S[K]>;
|
|
14
|
+
} : never;
|
|
15
|
+
export type { NormalizeSchema as default };
|
|
16
|
+
//# sourceMappingURL=NormalizeSchema.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import expected from "#expected";
|
|
2
|
+
import PrimitiveType from "#PrimitiveType";
|
|
3
|
+
const error_message = (name, x, key) => {
|
|
4
|
+
const base = expected(name, x);
|
|
5
|
+
return key === undefined
|
|
6
|
+
? base
|
|
7
|
+
: `${key}: ${base}`;
|
|
8
|
+
};
|
|
9
|
+
export default class NullType extends PrimitiveType {
|
|
10
|
+
constructor() {
|
|
11
|
+
super("null");
|
|
12
|
+
}
|
|
13
|
+
validate(x, key) {
|
|
14
|
+
if (x !== null) {
|
|
15
|
+
throw new Error(error_message(this.name, x, key));
|
|
16
|
+
}
|
|
17
|
+
return x;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=NullType.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import type Validated from "#Validated";
|
|
4
|
+
export default class OptionalType<S extends Validated<unknown>> extends GenericType<S | undefined, Infer<S> | undefined, "OptionalType"> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(s: S);
|
|
7
|
+
get name(): string;
|
|
8
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=OptionalType.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
export default class OptionalType extends GenericType {
|
|
3
|
+
#schema;
|
|
4
|
+
constructor(s) {
|
|
5
|
+
super();
|
|
6
|
+
this.#schema = s;
|
|
7
|
+
}
|
|
8
|
+
get name() {
|
|
9
|
+
return "optional";
|
|
10
|
+
}
|
|
11
|
+
validate(x, key) {
|
|
12
|
+
const s = this.#schema;
|
|
13
|
+
// optional
|
|
14
|
+
if (x === undefined) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
return s.validate(x, key);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=OptionalType.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import Type from "#Type";
|
|
3
|
+
import type Validator from "#Validator";
|
|
4
|
+
export default class PrimitiveType<StaticType, Name extends string> extends Type<StaticType, Name> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(name: string, validators?: Validator<StaticType>[]);
|
|
7
|
+
get validators(): Validator<StaticType>[];
|
|
8
|
+
get name(): string;
|
|
9
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=PrimitiveType.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import expected from "#expected";
|
|
2
|
+
import Type from "#Type";
|
|
3
|
+
const error_message = (name, x, key) => {
|
|
4
|
+
const base = expected(name, x);
|
|
5
|
+
return key === undefined
|
|
6
|
+
? base
|
|
7
|
+
: `${key}: ${base}`;
|
|
8
|
+
};
|
|
9
|
+
export default class PrimitiveType extends Type {
|
|
10
|
+
#name;
|
|
11
|
+
#validators;
|
|
12
|
+
constructor(name, validators = []) {
|
|
13
|
+
super();
|
|
14
|
+
this.#name = name;
|
|
15
|
+
this.#validators = validators;
|
|
16
|
+
}
|
|
17
|
+
get validators() {
|
|
18
|
+
return this.#validators;
|
|
19
|
+
}
|
|
20
|
+
get name() {
|
|
21
|
+
return this.#name;
|
|
22
|
+
}
|
|
23
|
+
validate(x, key) {
|
|
24
|
+
if (typeof x !== this.name) {
|
|
25
|
+
throw new Error(error_message(this.name, x, key));
|
|
26
|
+
}
|
|
27
|
+
this.#validators.forEach(validator => validator(x));
|
|
28
|
+
return x;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=PrimitiveType.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import OptionalType from "#OptionalType";
|
|
4
|
+
import type RecordTypeKey from "#RecordTypeKey";
|
|
5
|
+
import type Validated from "#Validated";
|
|
6
|
+
export default class RecordType<Key extends RecordTypeKey, Value extends Validated<unknown>> extends GenericType<Value, Record<Infer<Key>, Infer<Value>>, "RecordType"> {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(k: Key, v: Value);
|
|
9
|
+
optional(): OptionalType<this>;
|
|
10
|
+
get name(): string;
|
|
11
|
+
validate(x: unknown, key?: string): Infer<this>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=RecordType.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import GenericType from "#GenericType";
|
|
2
|
+
import OptionalType from "#OptionalType";
|
|
3
|
+
import expected from "#expected";
|
|
4
|
+
const error = (k, key) => {
|
|
5
|
+
return key === undefined
|
|
6
|
+
? `.${k}`
|
|
7
|
+
: `${key}.${k}`;
|
|
8
|
+
};
|
|
9
|
+
const is_numeric = (string) => /^-?\d+(\.\d+)?$/.test(string);
|
|
10
|
+
export default class RecordType extends GenericType {
|
|
11
|
+
#key;
|
|
12
|
+
#value;
|
|
13
|
+
constructor(k, v) {
|
|
14
|
+
super();
|
|
15
|
+
this.#key = k;
|
|
16
|
+
this.#value = v;
|
|
17
|
+
}
|
|
18
|
+
optional() {
|
|
19
|
+
return new OptionalType(this);
|
|
20
|
+
}
|
|
21
|
+
get name() {
|
|
22
|
+
return "record";
|
|
23
|
+
}
|
|
24
|
+
validate(x, key) {
|
|
25
|
+
if (typeof x !== "object" || x === null) {
|
|
26
|
+
throw new Error("Expected object");
|
|
27
|
+
}
|
|
28
|
+
const key_name = this.#key.name;
|
|
29
|
+
const keys = Object.keys(x);
|
|
30
|
+
const symbols = Object.getOwnPropertySymbols(x);
|
|
31
|
+
if (key_name === "string" || key_name === "number") {
|
|
32
|
+
// no key may be a symbol
|
|
33
|
+
if (symbols.length > 0) {
|
|
34
|
+
throw new Error(expected(`${key_name} key`, symbols[0]));
|
|
35
|
+
}
|
|
36
|
+
keys.forEach(k => {
|
|
37
|
+
if (key_name === "string" && is_numeric(k)) {
|
|
38
|
+
throw new Error(expected("string key", +k));
|
|
39
|
+
}
|
|
40
|
+
if (key_name === "number" && !is_numeric(k)) {
|
|
41
|
+
throw new Error(expected("number key", k));
|
|
42
|
+
}
|
|
43
|
+
this.#value.validate(x[k], error(k.toString(), key));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (key_name === "symbol") {
|
|
47
|
+
// no key may not be a symbol
|
|
48
|
+
if (keys.length > 0) {
|
|
49
|
+
throw new Error(expected("symbol key", keys[0]));
|
|
50
|
+
}
|
|
51
|
+
symbols.forEach(k => {
|
|
52
|
+
this.#value.validate(x[k], error(k.toString(), key));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return x;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=RecordType.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type NumberType from "#NumberType";
|
|
2
|
+
import type StringType from "#StringType";
|
|
3
|
+
import type SymbolType from "#SymbolType";
|
|
4
|
+
type KeyType = StringType | NumberType | SymbolType;
|
|
5
|
+
export type { KeyType as default };
|
|
6
|
+
//# sourceMappingURL=RecordTypeKey.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type Validated from "#Validated";
|
|
2
|
+
import type AbstractConstructor from "@rcompat/type/AbstractConstructor";
|
|
3
|
+
type Schema = Validated<unknown> | Schema[] | null | undefined | string | AbstractConstructor | {
|
|
4
|
+
[k: string]: Schema;
|
|
5
|
+
};
|
|
6
|
+
export type { Schema as default };
|
|
7
|
+
//# sourceMappingURL=Schema.d.ts.map
|