pema 0.4.0 → 0.6.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/ArrayType.d.ts +2 -1
- package/lib/private/ArrayType.js +29 -41
- package/lib/private/BigIntType.d.ts +2 -2
- package/lib/private/BigUintType.d.ts +2 -2
- package/lib/private/BlobType.d.ts +2 -2
- package/lib/private/BlobType.js +2 -2
- package/lib/private/BooleanType.d.ts +2 -2
- package/lib/private/BooleanType.js +4 -4
- package/lib/private/BuiltinType.d.ts +1 -2
- package/lib/private/BuiltinType.js +3 -8
- package/lib/private/ConstructorType.d.ts +5 -2
- package/lib/private/ConstructorType.js +7 -5
- package/lib/private/DataType.d.ts +5 -1
- package/lib/private/DateType.d.ts +2 -2
- package/lib/private/DateType.js +2 -2
- package/lib/private/DefaultType.js +3 -6
- package/lib/private/EnumType.d.ts +21 -0
- package/lib/private/EnumType.js +30 -0
- package/lib/private/FileType.d.ts +2 -2
- package/lib/private/FileType.js +2 -2
- package/lib/private/FunctionType.d.ts +15 -0
- package/lib/private/FunctionType.js +20 -0
- package/lib/private/GenericType.d.ts +1 -1
- package/lib/private/InferInputSchema.d.ts +7 -17
- package/lib/private/InferStore.d.ts +1 -1
- package/lib/private/IntType.d.ts +2 -2
- package/lib/private/IsType.d.ts +19 -0
- package/lib/private/IsType.js +29 -0
- package/lib/private/Issue.d.ts +3 -1
- package/lib/private/IssueType.d.ts +3 -0
- package/lib/private/IssueType.js +2 -0
- package/lib/private/JSONType.d.ts +26 -0
- package/lib/private/JSONType.js +41 -0
- package/lib/private/LiteralType.d.ts +5 -2
- package/lib/private/LiteralType.js +11 -5
- package/lib/private/NormalizeSchema.d.ts +4 -6
- package/lib/private/NullType.js +3 -5
- package/lib/private/NumberType.d.ts +1 -1
- package/lib/private/NumberType.js +1 -1
- package/lib/private/NumericType.d.ts +2 -2
- package/lib/private/ObjectType.d.ts +13 -4
- package/lib/private/ObjectType.js +24 -11
- package/lib/private/OmitType.d.ts +1 -1
- package/lib/private/OmitType.js +7 -6
- package/lib/private/ParseError.d.ts +6 -2
- package/lib/private/ParseError.js +25 -15
- package/lib/private/ParseOptions.d.ts +1 -1
- package/lib/private/Parsed.d.ts +2 -1
- package/lib/private/Parsed.js +3 -0
- package/lib/private/PartialType.js +8 -10
- package/lib/private/Partialable.d.ts +1 -1
- package/lib/private/PrimitiveType.d.ts +0 -1
- package/lib/private/PrimitiveType.js +23 -22
- package/lib/private/PureType.d.ts +1 -1
- package/lib/private/RecordType.js +14 -39
- package/lib/private/Schema.d.ts +1 -1
- package/lib/private/Serialized.d.ts +18 -4
- package/lib/private/{Storeable.d.ts → Storable.d.ts} +3 -3
- package/lib/private/{Storeable.js → Storable.js} +2 -2
- package/lib/private/StoreSchema.d.ts +3 -6
- package/lib/private/StoreType.d.ts +5 -1
- package/lib/private/StoreType.js +22 -2
- package/lib/private/StringType.d.ts +2 -3
- package/lib/private/StringType.js +7 -13
- package/lib/private/TupleType.d.ts +2 -2
- package/lib/private/TupleType.js +19 -14
- package/lib/private/Type.d.ts +1 -1
- package/lib/private/URLType.d.ts +2 -2
- package/lib/private/URLType.js +2 -2
- package/lib/private/UUIDType.d.ts +17 -0
- package/lib/private/UUIDType.js +33 -0
- package/lib/private/UUIDV4Type.d.ts +13 -0
- package/lib/private/UUIDV4Type.js +23 -0
- package/lib/private/UUIDV7Type.d.ts +13 -0
- package/lib/private/UUIDV7Type.js +23 -0
- package/lib/private/UintType.d.ts +2 -2
- package/lib/private/UnionType.d.ts +3 -3
- package/lib/private/UnionType.js +11 -13
- package/lib/private/VirtualType.js +2 -3
- package/lib/private/coerce/bigint.d.ts +1 -1
- package/lib/private/coerce/bigint.js +4 -6
- package/lib/private/coerce/float.d.ts +2 -0
- package/lib/private/coerce/float.js +8 -0
- package/lib/private/coerce/int.d.ts +1 -1
- package/lib/private/coerce/int.js +7 -5
- package/lib/private/constructor.d.ts +1 -1
- package/lib/private/dict.d.ts +11 -0
- package/lib/private/dict.js +6 -0
- package/lib/private/enum.d.ts +3 -0
- package/lib/private/enum.js +5 -0
- package/lib/private/errors.d.ts +21 -0
- package/lib/private/errors.js +53 -0
- package/lib/private/function.d.ts +7 -0
- package/lib/private/function.js +6 -0
- package/lib/private/index.d.ts +79 -30
- package/lib/private/index.js +20 -2
- package/lib/private/is.d.ts +4 -0
- package/lib/private/is.js +3 -0
- package/lib/private/json/JSONIssue.d.ts +2 -0
- package/lib/private/json/JSONPayload.d.ts +1 -1
- package/lib/private/json.d.ts +5 -0
- package/lib/private/json.js +5 -0
- package/lib/private/normalize.js +2 -3
- package/lib/private/object.d.ts +3 -4
- package/lib/private/object.js +2 -2
- package/lib/private/omit.d.ts +1 -1
- package/lib/private/path/join.d.ts +1 -1
- package/lib/private/path/next.d.ts +1 -1
- package/lib/private/path/rebase.d.ts +1 -1
- package/lib/private/schema-errors.d.ts +16 -0
- package/lib/private/schema-errors.js +17 -0
- package/lib/private/spec/bigint.d.ts +1 -1
- package/lib/private/spec/bigint.js +30 -41
- package/lib/private/spec/biguint.d.ts +1 -1
- package/lib/private/spec/biguint.js +29 -45
- package/lib/private/spec/int.d.ts +1 -1
- package/lib/private/spec/int.js +27 -38
- package/lib/private/spec/uint.d.ts +1 -1
- package/lib/private/spec/uint.js +31 -43
- package/lib/private/test.d.ts +34 -0
- package/lib/private/test.js +107 -0
- package/lib/private/uuid.d.ts +4 -0
- package/lib/private/uuid.js +3 -0
- package/lib/private/validator/ends-with.d.ts +1 -2
- package/lib/private/validator/ends-with.js +8 -10
- package/lib/private/validator/f32.js +3 -8
- package/lib/private/validator/integer.d.ts +1 -2
- package/lib/private/validator/integer.js +7 -11
- package/lib/private/validator/isotime.js +8 -10
- package/lib/private/validator/length.js +12 -20
- package/lib/private/validator/max.js +15 -16
- package/lib/private/validator/min.js +15 -16
- package/lib/private/validator/port.d.ts +1 -2
- package/lib/private/validator/port.js +6 -9
- package/lib/private/validator/range.js +7 -13
- package/lib/private/validator/regex.d.ts +1 -1
- package/lib/private/validator/regex.js +5 -9
- package/lib/private/validator/starts-with.d.ts +1 -2
- package/lib/private/validator/starts-with.js +8 -10
- package/lib/private/validator/unique-by.d.ts +1 -1
- package/lib/private/validator/unique-by.js +8 -14
- package/lib/private/validator/unique-with.d.ts +1 -1
- package/lib/private/validator/unique-with.js +3 -7
- package/lib/private/validator/unique.js +2 -6
- package/lib/private/validator/values.d.ts +1 -1
- package/lib/private/validator/values.js +5 -8
- package/lib/public/StoreType.d.ts +1 -1
- package/lib/public/index.d.ts +1 -9
- package/lib/public/index.js +0 -9
- package/package.json +18 -15
- package/lib/private/DecrementDepth.d.ts +0 -3
- package/lib/private/DecrementDepth.js +0 -2
- package/lib/private/InferSchema.d.ts +0 -20
- package/lib/private/InferSchema.js +0 -2
- package/lib/private/PrimaryType.d.ts +0 -14
- package/lib/private/PrimaryType.js +0 -26
- package/lib/private/SchemaError.d.ts +0 -4
- package/lib/private/SchemaError.js +0 -8
- package/lib/private/error/fail.d.ts +0 -3
- package/lib/private/error/fail.js +0 -5
- package/lib/private/error/schemafail.d.ts +0 -3
- package/lib/private/error/schemafail.js +0 -5
- package/lib/private/error.d.ts +0 -4
- package/lib/private/error.js +0 -11
- package/lib/private/expect.d.ts +0 -22
- package/lib/private/expect.js +0 -32
- package/lib/private/expected.d.ts +0 -3
- package/lib/private/expected.js +0 -11
- package/lib/private/primary.d.ts +0 -4
- package/lib/private/primary.js +0 -3
- package/lib/private/test/messages-of.d.ts +0 -3
- package/lib/private/test/messages-of.js +0 -4
- package/lib/private/test/paths-of.d.ts +0 -3
- package/lib/private/test/paths-of.js +0 -4
- package/lib/private/test/throws-issues.d.ts +0 -4
- package/lib/private/test/throws-issues.js +0 -13
- package/lib/private/validator/uuid.d.ts +0 -3
- package/lib/private/validator/uuid.js +0 -4
- package/lib/public/DataType.d.ts +0 -2
- package/lib/public/DataType.js +0 -2
- package/lib/public/Id.d.ts +0 -2
- package/lib/public/Id.js +0 -2
- package/lib/public/InferStore.d.ts +0 -2
- package/lib/public/InferStore.js +0 -2
- package/lib/public/InferStoreOut.d.ts +0 -2
- package/lib/public/InferStoreOut.js +0 -2
- package/lib/public/Issue.d.ts +0 -2
- package/lib/public/Issue.js +0 -2
- package/lib/public/JSONPayload.d.ts +0 -2
- package/lib/public/JSONPayload.js +0 -2
- package/lib/public/OmitType.d.ts +0 -2
- package/lib/public/OmitType.js +0 -2
- package/lib/public/Schema.d.ts +0 -2
- package/lib/public/Schema.js +0 -2
- package/lib/public/Serialized.d.ts +0 -2
- package/lib/public/Serialized.js +0 -2
- package/lib/public/StoreId.d.ts +0 -2
- package/lib/public/StoreId.js +0 -2
- package/lib/public/StoreSchema.d.ts +0 -2
- package/lib/public/StoreSchema.js +0 -2
- package/lib/public/array.d.ts +0 -3
- package/lib/public/array.js +0 -3
- package/lib/public/bigint.d.ts +0 -2
- package/lib/public/bigint.js +0 -2
- package/lib/public/biguint.d.ts +0 -2
- package/lib/public/biguint.js +0 -2
- package/lib/public/blob.d.ts +0 -2
- package/lib/public/blob.js +0 -2
- package/lib/public/boolean.d.ts +0 -3
- package/lib/public/boolean.js +0 -3
- package/lib/public/constructor.d.ts +0 -3
- package/lib/public/constructor.js +0 -3
- package/lib/public/date.d.ts +0 -2
- package/lib/public/date.js +0 -2
- package/lib/public/f32.d.ts +0 -2
- package/lib/public/f32.js +0 -2
- package/lib/public/f64.d.ts +0 -2
- package/lib/public/f64.js +0 -2
- package/lib/public/file.d.ts +0 -2
- package/lib/public/file.js +0 -2
- package/lib/public/i128.d.ts +0 -2
- package/lib/public/i128.js +0 -2
- package/lib/public/i16.d.ts +0 -2
- package/lib/public/i16.js +0 -2
- package/lib/public/i32.d.ts +0 -2
- package/lib/public/i32.js +0 -2
- package/lib/public/i64.d.ts +0 -2
- package/lib/public/i64.js +0 -2
- package/lib/public/i8.d.ts +0 -2
- package/lib/public/i8.js +0 -2
- package/lib/public/int.d.ts +0 -2
- package/lib/public/int.js +0 -2
- package/lib/public/number.d.ts +0 -3
- package/lib/public/number.js +0 -3
- package/lib/public/object.d.ts +0 -2
- package/lib/public/object.js +0 -2
- package/lib/public/optional.d.ts +0 -2
- package/lib/public/optional.js +0 -2
- package/lib/public/primary.d.ts +0 -2
- package/lib/public/primary.js +0 -2
- package/lib/public/pure.d.ts +0 -2
- package/lib/public/pure.js +0 -2
- package/lib/public/record.d.ts +0 -3
- package/lib/public/record.js +0 -3
- package/lib/public/string.d.ts +0 -3
- package/lib/public/string.js +0 -3
- package/lib/public/symbol.d.ts +0 -2
- package/lib/public/symbol.js +0 -2
- package/lib/public/u128.d.ts +0 -2
- package/lib/public/u128.js +0 -2
- package/lib/public/u16.d.ts +0 -2
- package/lib/public/u16.js +0 -2
- package/lib/public/u32.d.ts +0 -2
- package/lib/public/u32.js +0 -2
- package/lib/public/u64.d.ts +0 -2
- package/lib/public/u64.js +0 -2
- package/lib/public/u8.d.ts +0 -2
- package/lib/public/u8.js +0 -2
- package/lib/public/uint.d.ts +0 -3
- package/lib/public/uint.js +0 -3
- package/lib/public/union.d.ts +0 -3
- package/lib/public/union.js +0 -3
- package/lib/public/unknown.d.ts +0 -3
- package/lib/public/unknown.js +0 -2
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import expected from "#expected";
|
|
1
|
+
import E from "#errors";
|
|
3
2
|
import GenericType from "#GenericType";
|
|
4
3
|
import OptionalType from "#OptionalType";
|
|
5
4
|
import ParsedKey from "#ParsedKey";
|
|
6
|
-
import ParseError from "#ParseError";
|
|
7
5
|
import join from "#path/join";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return options === undefined
|
|
11
|
-
? { [ParsedKey]: join("", k) }
|
|
12
|
-
: { ...options, [ParsedKey]: join(base, k) };
|
|
13
|
-
};
|
|
14
|
-
const is_numeric = (string) => /^-?\d+(\.\d+)?$/.test(string);
|
|
6
|
+
import next from "#path/next";
|
|
7
|
+
import is from "@rcompat/is";
|
|
15
8
|
export default class RecordType extends GenericType {
|
|
16
9
|
#key;
|
|
17
10
|
#value;
|
|
@@ -27,48 +20,30 @@ export default class RecordType extends GenericType {
|
|
|
27
20
|
return "record";
|
|
28
21
|
}
|
|
29
22
|
parse(x, options = {}) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
const path = options[ParsedKey] ?? "";
|
|
24
|
+
if (!is.dict(x))
|
|
25
|
+
throw E.invalid_type(x, "object", path);
|
|
33
26
|
const key_name = this.#key.name;
|
|
34
27
|
const keys = Object.keys(x);
|
|
35
28
|
const symbols = Object.getOwnPropertySymbols(x);
|
|
36
|
-
const base = options[ParsedKey] ?? "";
|
|
37
29
|
if (key_name === "string" || key_name === "number") {
|
|
38
30
|
// no key may be a symbol
|
|
39
|
-
if (symbols.length > 0)
|
|
40
|
-
throw
|
|
41
|
-
input: x,
|
|
42
|
-
message: expected(`${key_name} key`, symbols[0]),
|
|
43
|
-
path: base,
|
|
44
|
-
}]);
|
|
45
|
-
}
|
|
31
|
+
if (symbols.length > 0)
|
|
32
|
+
throw E.invalid_type(x, `${key_name} key`, path);
|
|
46
33
|
keys.forEach(k => {
|
|
47
|
-
if (key_name === "string" &&
|
|
48
|
-
throw
|
|
49
|
-
input: x,
|
|
50
|
-
message: expected("string key", +k),
|
|
51
|
-
path: join(base, k),
|
|
52
|
-
}]);
|
|
34
|
+
if (key_name === "string" && is.numeric(k)) {
|
|
35
|
+
throw E.invalid_type(x, "string key", join(path, k));
|
|
53
36
|
}
|
|
54
|
-
if (key_name === "number" && !
|
|
55
|
-
throw
|
|
56
|
-
input: x,
|
|
57
|
-
message: expected("number key", k),
|
|
58
|
-
path: join(base, k),
|
|
59
|
-
}]);
|
|
37
|
+
if (key_name === "number" && !is.numeric(k)) {
|
|
38
|
+
throw E.invalid_type(x, "number key", join(path, k));
|
|
60
39
|
}
|
|
61
|
-
this.#value.parse(x[k],
|
|
40
|
+
this.#value.parse(x[k], next(k, options));
|
|
62
41
|
});
|
|
63
42
|
}
|
|
64
43
|
if (key_name === "symbol") {
|
|
65
44
|
// disallow any non-symbol keys
|
|
66
45
|
if (keys.length > 0) {
|
|
67
|
-
throw
|
|
68
|
-
input: x,
|
|
69
|
-
message: expected("symbol key", keys[0]),
|
|
70
|
-
path: join(base, keys[0]),
|
|
71
|
-
}]);
|
|
46
|
+
throw E.invalid_type(keys[0], "symbol key", join(path, keys[0]));
|
|
72
47
|
}
|
|
73
48
|
symbols.forEach(k => {
|
|
74
49
|
this.#value.parse(x[k], options);
|
package/lib/private/Schema.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type Parsed from "#Parsed";
|
|
2
|
-
import type AbstractNewable from "@rcompat/type
|
|
2
|
+
import type { AbstractNewable } from "@rcompat/type";
|
|
3
3
|
type Schema = Parsed<unknown> | AbstractNewable | null | undefined | string | number | boolean | Schema[] | {
|
|
4
4
|
[k: string]: Schema;
|
|
5
5
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type Dict from "@rcompat/type
|
|
2
|
-
import type JSONValue from "@rcompat/type/JSONValue";
|
|
1
|
+
import type { Dict, JSONValue } from "@rcompat/type";
|
|
3
2
|
type DT<D extends string = string> = {
|
|
4
3
|
datatype?: D;
|
|
5
4
|
};
|
|
@@ -21,7 +20,15 @@ type PrimitiveSerialized = {
|
|
|
21
20
|
type: "file";
|
|
22
21
|
} & DT | {
|
|
23
22
|
type: "url";
|
|
24
|
-
} & DT
|
|
23
|
+
} & DT | {
|
|
24
|
+
type: "uuid";
|
|
25
|
+
} & DT | {
|
|
26
|
+
type: "uuid_v4";
|
|
27
|
+
} & DT | {
|
|
28
|
+
type: "uuid_v7";
|
|
29
|
+
} & DT | {
|
|
30
|
+
type: "function";
|
|
31
|
+
};
|
|
25
32
|
type NumberSerialized = {
|
|
26
33
|
type: "number";
|
|
27
34
|
} & DT;
|
|
@@ -62,13 +69,20 @@ type StructuralSerialized = {
|
|
|
62
69
|
} | {
|
|
63
70
|
type: "schema";
|
|
64
71
|
of: Serialized;
|
|
72
|
+
} | {
|
|
73
|
+
type: "json";
|
|
74
|
+
datatype: "json";
|
|
75
|
+
of?: Serialized;
|
|
65
76
|
} | {
|
|
66
77
|
type: "omit";
|
|
67
78
|
properties: Dict<Serialized>;
|
|
79
|
+
} | {
|
|
80
|
+
type: "enum";
|
|
81
|
+
values: string[];
|
|
68
82
|
} | {
|
|
69
83
|
type: "pure";
|
|
70
84
|
} | {
|
|
71
|
-
type: "
|
|
85
|
+
type: "is";
|
|
72
86
|
};
|
|
73
87
|
type Serialized = PrimitiveSerialized | NumberSerialized | BigIntSerialized | StructuralSerialized;
|
|
74
88
|
export type { Serialized as default };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type K from "#DataKey";
|
|
2
2
|
import Parsed from "#Parsed";
|
|
3
3
|
import type Serialized from "#Serialized";
|
|
4
|
-
export default abstract class
|
|
5
|
-
abstract get datatype():
|
|
4
|
+
export default abstract class Storable<TKey extends K = K, TValue = unknown> extends Parsed<TValue> {
|
|
5
|
+
abstract get datatype(): TKey;
|
|
6
6
|
abstract toJSON(): Serialized;
|
|
7
7
|
static serialize<N extends string, T extends K>(s: {
|
|
8
8
|
name: N;
|
|
@@ -12,4 +12,4 @@ export default abstract class Storeable<T extends K = K> extends Parsed<unknown>
|
|
|
12
12
|
datatype: T;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
//# sourceMappingURL=
|
|
15
|
+
//# sourceMappingURL=Storable.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Parsed from "#Parsed";
|
|
2
|
-
export default class
|
|
2
|
+
export default class Storable extends Parsed {
|
|
3
3
|
static serialize(s) {
|
|
4
4
|
return { type: s.name, datatype: s.datatype };
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
|
-
//# sourceMappingURL=
|
|
7
|
+
//# sourceMappingURL=Storable.js.map
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
} & {
|
|
5
|
-
id: Storeable;
|
|
6
|
-
};
|
|
1
|
+
import type Storable from "#Storable";
|
|
2
|
+
import type { Dict } from "@rcompat/type";
|
|
3
|
+
type StoreSchema = Dict<Storable>;
|
|
7
4
|
export type { StoreSchema as default };
|
|
8
5
|
//# sourceMappingURL=StoreSchema.d.ts.map
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
1
2
|
import ObjectType from "#ObjectType";
|
|
3
|
+
import type ParseOptions from "#ParseOptions";
|
|
2
4
|
import PartialType from "#PartialType";
|
|
3
5
|
import type StoreSchema from "#StoreSchema";
|
|
4
6
|
export default class StoreType<S extends StoreSchema> extends ObjectType<S> {
|
|
5
|
-
|
|
7
|
+
#private;
|
|
8
|
+
constructor(properties: S, pk?: string | null);
|
|
6
9
|
get name(): string;
|
|
10
|
+
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
7
11
|
partial(): PartialType<any>;
|
|
8
12
|
}
|
|
9
13
|
//# sourceMappingURL=StoreType.d.ts.map
|
package/lib/private/StoreType.js
CHANGED
|
@@ -1,12 +1,32 @@
|
|
|
1
1
|
import ObjectType from "#ObjectType";
|
|
2
2
|
import PartialType from "#PartialType";
|
|
3
|
+
import E from "#errors";
|
|
4
|
+
import next from "#path/next";
|
|
5
|
+
import is from "@rcompat/is";
|
|
3
6
|
export default class StoreType extends ObjectType {
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
#pk;
|
|
8
|
+
constructor(properties, pk = null) {
|
|
9
|
+
super(properties);
|
|
10
|
+
this.#pk = pk;
|
|
6
11
|
}
|
|
7
12
|
get name() {
|
|
8
13
|
return "store";
|
|
9
14
|
}
|
|
15
|
+
parse(x, options = {}) {
|
|
16
|
+
const $options = { ...options };
|
|
17
|
+
if (x !== undefined && !is.dict(x))
|
|
18
|
+
throw E.invalid_type(x, "object", $options);
|
|
19
|
+
const input = x ?? {};
|
|
20
|
+
const out = {};
|
|
21
|
+
for (const k in this.properties) {
|
|
22
|
+
if (k === this.#pk && !(k in input))
|
|
23
|
+
continue;
|
|
24
|
+
const parsed = this.properties[k].parse(input[k], next(k, $options));
|
|
25
|
+
if (parsed !== undefined)
|
|
26
|
+
out[k] = parsed;
|
|
27
|
+
}
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
10
30
|
partial() {
|
|
11
31
|
return new PartialType(this.properties);
|
|
12
32
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import PrimitiveType from "#PrimitiveType";
|
|
2
|
-
import
|
|
3
|
-
export default class StringType extends PrimitiveType<string, "StringType"> implements
|
|
2
|
+
import Storable from "#Storable";
|
|
3
|
+
export default class StringType extends PrimitiveType<string, "StringType"> implements Storable<"string"> {
|
|
4
4
|
get name(): "string";
|
|
5
5
|
get datatype(): "string";
|
|
6
6
|
isotime(): this;
|
|
7
7
|
regex(pattern: RegExp): this;
|
|
8
8
|
email(): this;
|
|
9
|
-
uuid(): this;
|
|
10
9
|
startsWith(prefix: string): this;
|
|
11
10
|
endsWith(suffix: string): this;
|
|
12
11
|
min(limit: number): this;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import schemafail from "#error/schemafail";
|
|
2
1
|
import PrimitiveType from "#PrimitiveType";
|
|
3
|
-
import
|
|
2
|
+
import E from "#schema-errors";
|
|
3
|
+
import Storable from "#Storable";
|
|
4
4
|
import email from "#validator/email";
|
|
5
5
|
import ends_with from "#validator/ends-with";
|
|
6
6
|
import isotime from "#validator/isotime";
|
|
@@ -9,7 +9,6 @@ import max from "#validator/max";
|
|
|
9
9
|
import min from "#validator/min";
|
|
10
10
|
import regex from "#validator/regex";
|
|
11
11
|
import starts_with from "#validator/starts-with";
|
|
12
|
-
import uuid from "#validator/uuid";
|
|
13
12
|
export default class StringType extends PrimitiveType {
|
|
14
13
|
get name() {
|
|
15
14
|
return "string";
|
|
@@ -26,9 +25,6 @@ export default class StringType extends PrimitiveType {
|
|
|
26
25
|
email() {
|
|
27
26
|
return this.derive({ validators: [email] });
|
|
28
27
|
}
|
|
29
|
-
uuid() {
|
|
30
|
-
return this.derive({ validators: [uuid] });
|
|
31
|
-
}
|
|
32
28
|
startsWith(prefix) {
|
|
33
29
|
return this.derive({ validators: [starts_with(prefix)] });
|
|
34
30
|
}
|
|
@@ -36,22 +32,20 @@ export default class StringType extends PrimitiveType {
|
|
|
36
32
|
return this.derive({ validators: [ends_with(suffix)] });
|
|
37
33
|
}
|
|
38
34
|
min(limit) {
|
|
39
|
-
if (limit < 0)
|
|
40
|
-
throw
|
|
41
|
-
}
|
|
35
|
+
if (limit < 0)
|
|
36
|
+
throw E.min_negative(limit);
|
|
42
37
|
return this.derive({ validators: [min(limit)] });
|
|
43
38
|
}
|
|
44
39
|
max(limit) {
|
|
45
|
-
if (limit < 0)
|
|
46
|
-
throw
|
|
47
|
-
}
|
|
40
|
+
if (limit < 0)
|
|
41
|
+
throw E.max_negative(limit);
|
|
48
42
|
return this.derive({ validators: [max(limit)] });
|
|
49
43
|
}
|
|
50
44
|
length(from, to) {
|
|
51
45
|
return this.derive({ validators: [length(from, to)] });
|
|
52
46
|
}
|
|
53
47
|
toJSON() {
|
|
54
|
-
return
|
|
48
|
+
return Storable.serialize(this);
|
|
55
49
|
}
|
|
56
50
|
}
|
|
57
51
|
//# sourceMappingURL=StringType.js.map
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import GenericType from "#GenericType";
|
|
2
2
|
import type Infer from "#Infer";
|
|
3
|
-
import type
|
|
3
|
+
import type NormalizeSchema from "#NormalizeSchema";
|
|
4
4
|
import OptionalType from "#OptionalType";
|
|
5
5
|
import type Parsed from "#Parsed";
|
|
6
6
|
import type ParseOptions from "#ParseOptions";
|
|
7
7
|
import type Schema from "#Schema";
|
|
8
8
|
import type OptionalTrait from "#trait/Optional";
|
|
9
9
|
type InferTuple<T extends Schema[]> = {
|
|
10
|
-
[K in keyof T]: T[K] extends Schema ?
|
|
10
|
+
[K in keyof T]: T[K] extends Schema ? NormalizeSchema<T[K]>["infer"] : "tuple-never";
|
|
11
11
|
};
|
|
12
12
|
export default class TupleType<T extends Parsed<unknown>[]> extends GenericType<T, InferTuple<T>, "TupleType"> implements OptionalTrait {
|
|
13
13
|
#private;
|
package/lib/private/TupleType.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import E from "#errors";
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
3
|
import OptionalType from "#OptionalType";
|
|
4
|
-
import ParseError from "#ParseError";
|
|
5
4
|
import next from "#path/next";
|
|
6
5
|
export default class TupleType extends GenericType {
|
|
7
6
|
#items;
|
|
@@ -16,19 +15,25 @@ export default class TupleType extends GenericType {
|
|
|
16
15
|
return new OptionalType(this);
|
|
17
16
|
}
|
|
18
17
|
parse(x, options = {}) {
|
|
19
|
-
if (!
|
|
20
|
-
throw
|
|
18
|
+
if (!Array.isArray(x))
|
|
19
|
+
throw E.invalid_type(x, "array", options);
|
|
20
|
+
const items = this.#items;
|
|
21
|
+
const n = items.length;
|
|
22
|
+
const out = new Array(n);
|
|
23
|
+
// validate each expected item
|
|
24
|
+
for (let i = 0; i < n; i++) {
|
|
25
|
+
out[i] = items[i].parse(x[i], next(i, options));
|
|
21
26
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
// reject extra items
|
|
28
|
+
if (x.length > n)
|
|
29
|
+
throw E.invalid_type(x[n], "undefined", next(n, options));
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
toJSON() {
|
|
33
|
+
return {
|
|
34
|
+
type: this.name,
|
|
35
|
+
of: this.#items.map(i => i.toJSON()),
|
|
36
|
+
};
|
|
31
37
|
}
|
|
32
|
-
toJSON() { return { type: this.name, of: this.#items.map(i => i.toJSON()) }; }
|
|
33
38
|
}
|
|
34
39
|
//# sourceMappingURL=TupleType.js.map
|
package/lib/private/Type.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import OptionalType from "#OptionalType";
|
|
|
3
3
|
import Parsed from "#Parsed";
|
|
4
4
|
import type DefaultTrait from "#trait/Default";
|
|
5
5
|
import type OptionalTrait from "#trait/Optional";
|
|
6
|
-
import type Printable from "@rcompat/type
|
|
6
|
+
import type { Printable } from "@rcompat/type";
|
|
7
7
|
export default abstract class Type<T, Name extends string> extends Parsed<T> implements Printable, DefaultTrait<T>, OptionalTrait {
|
|
8
8
|
optional(): OptionalType<this>;
|
|
9
9
|
default<S extends T>(value: (() => S) | S): DefaultType<this, S>;
|
package/lib/private/URLType.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import BuiltinType from "#BuiltinType";
|
|
2
|
-
import
|
|
3
|
-
export default class URLType extends BuiltinType<URL, "URLType"> implements
|
|
2
|
+
import Storable from "#Storable";
|
|
3
|
+
export default class URLType extends BuiltinType<URL, "URLType"> implements Storable<"url"> {
|
|
4
4
|
get Type(): {
|
|
5
5
|
new (url: string | URL, base?: string | URL): URL;
|
|
6
6
|
prototype: URL;
|
package/lib/private/URLType.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BuiltinType from "#BuiltinType";
|
|
2
|
-
import
|
|
2
|
+
import Storable from "#Storable";
|
|
3
3
|
export default class URLType extends BuiltinType {
|
|
4
4
|
get Type() {
|
|
5
5
|
return URL;
|
|
@@ -11,7 +11,7 @@ export default class URLType extends BuiltinType {
|
|
|
11
11
|
return "url";
|
|
12
12
|
}
|
|
13
13
|
toJSON() {
|
|
14
|
-
return
|
|
14
|
+
return Storable.serialize(this);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
//# sourceMappingURL=URLType.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import PrimitiveType from "#PrimitiveType";
|
|
3
|
+
import Storable from "#Storable";
|
|
4
|
+
import UUIDV4Type from "#UUIDV4Type";
|
|
5
|
+
import UUIDV7Type from "#UUIDV7Type";
|
|
6
|
+
export default class UUIDType extends PrimitiveType<string, "UUIDType"> implements Storable<"uuid"> {
|
|
7
|
+
get name(): "string";
|
|
8
|
+
get datatype(): "uuid";
|
|
9
|
+
parse(x: unknown): Infer<this>;
|
|
10
|
+
toJSON(): {
|
|
11
|
+
type: "string";
|
|
12
|
+
datatype: "uuid";
|
|
13
|
+
};
|
|
14
|
+
v4(): UUIDV4Type;
|
|
15
|
+
v7(): UUIDV7Type;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=UUIDType.d.ts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import PrimitiveType from "#PrimitiveType";
|
|
2
|
+
import Storable from "#Storable";
|
|
3
|
+
import UUIDV4Type from "#UUIDV4Type";
|
|
4
|
+
import UUIDV7Type from "#UUIDV7Type";
|
|
5
|
+
import E from "#errors";
|
|
6
|
+
import is from "@rcompat/is";
|
|
7
|
+
const re = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i;
|
|
8
|
+
const v4 = new UUIDV4Type();
|
|
9
|
+
const v7 = new UUIDV7Type();
|
|
10
|
+
export default class UUIDType extends PrimitiveType {
|
|
11
|
+
get name() {
|
|
12
|
+
return "string";
|
|
13
|
+
}
|
|
14
|
+
get datatype() {
|
|
15
|
+
return "uuid";
|
|
16
|
+
}
|
|
17
|
+
parse(x) {
|
|
18
|
+
if (!is.string(x) || !re.test(x)) {
|
|
19
|
+
throw E.invalid_format(x, `"${x}" is not a valid UUID`);
|
|
20
|
+
}
|
|
21
|
+
return x;
|
|
22
|
+
}
|
|
23
|
+
toJSON() {
|
|
24
|
+
return Storable.serialize(this);
|
|
25
|
+
}
|
|
26
|
+
v4() {
|
|
27
|
+
return v4;
|
|
28
|
+
}
|
|
29
|
+
v7() {
|
|
30
|
+
return v7;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=UUIDType.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import PrimitiveType from "#PrimitiveType";
|
|
3
|
+
import Storable from "#Storable";
|
|
4
|
+
export default class UUIDV4Type extends PrimitiveType<string, "UUIDV4Type"> implements Storable<"uuid_v4"> {
|
|
5
|
+
get name(): "string";
|
|
6
|
+
get datatype(): "uuid_v4";
|
|
7
|
+
parse(x: unknown): Infer<this>;
|
|
8
|
+
toJSON(): {
|
|
9
|
+
type: "string";
|
|
10
|
+
datatype: "uuid_v4";
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=UUIDV4Type.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import PrimitiveType from "#PrimitiveType";
|
|
2
|
+
import Storable from "#Storable";
|
|
3
|
+
import E from "#errors";
|
|
4
|
+
import is from "@rcompat/is";
|
|
5
|
+
const re = /^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i;
|
|
6
|
+
export default class UUIDV4Type extends PrimitiveType {
|
|
7
|
+
get name() {
|
|
8
|
+
return "string";
|
|
9
|
+
}
|
|
10
|
+
get datatype() {
|
|
11
|
+
return "uuid_v4";
|
|
12
|
+
}
|
|
13
|
+
parse(x) {
|
|
14
|
+
if (!is.string(x) || !re.test(x)) {
|
|
15
|
+
throw E.invalid_format(x, `"${x}" is not a valid UUID v4`);
|
|
16
|
+
}
|
|
17
|
+
return x;
|
|
18
|
+
}
|
|
19
|
+
toJSON() {
|
|
20
|
+
return Storable.serialize(this);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=UUIDV4Type.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import PrimitiveType from "#PrimitiveType";
|
|
3
|
+
import Storable from "#Storable";
|
|
4
|
+
export default class UUIDV7Type extends PrimitiveType<string, "UUIDV7Type"> implements Storable<"uuid_v7"> {
|
|
5
|
+
get name(): "string";
|
|
6
|
+
get datatype(): "uuid_v7";
|
|
7
|
+
parse(x: unknown): Infer<this>;
|
|
8
|
+
toJSON(): {
|
|
9
|
+
type: "string";
|
|
10
|
+
datatype: "uuid_v7";
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=UUIDV7Type.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import PrimitiveType from "#PrimitiveType";
|
|
2
|
+
import Storable from "#Storable";
|
|
3
|
+
import E from "#errors";
|
|
4
|
+
import is from "@rcompat/is";
|
|
5
|
+
const re = /^[\da-f]{8}-[\da-f]{4}-7[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i;
|
|
6
|
+
export default class UUIDV7Type extends PrimitiveType {
|
|
7
|
+
get name() {
|
|
8
|
+
return "string";
|
|
9
|
+
}
|
|
10
|
+
get datatype() {
|
|
11
|
+
return "uuid_v7";
|
|
12
|
+
}
|
|
13
|
+
parse(x) {
|
|
14
|
+
if (!is.string(x) || !re.test(x)) {
|
|
15
|
+
throw E.invalid_format(x, `"${x}" is not a valid UUID v7`);
|
|
16
|
+
}
|
|
17
|
+
return x;
|
|
18
|
+
}
|
|
19
|
+
toJSON() {
|
|
20
|
+
return Storable.serialize(this);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=UUIDV7Type.js.map
|
|
@@ -2,9 +2,9 @@ import coerce from "#coerce/int";
|
|
|
2
2
|
import CoerceKey from "#CoerceKey";
|
|
3
3
|
import NumericType from "#NumericType";
|
|
4
4
|
import type ParseOptions from "#ParseOptions";
|
|
5
|
-
import type
|
|
5
|
+
import type Storable from "#Storable";
|
|
6
6
|
import type UintDataType from "#UintDataType";
|
|
7
|
-
export default class UintType<T extends UintDataType> extends NumericType<T, number, "UintType"> implements
|
|
7
|
+
export default class UintType<T extends UintDataType> extends NumericType<T, number, "UintType"> implements Storable<T> {
|
|
8
8
|
[CoerceKey]: typeof coerce;
|
|
9
9
|
get name(): string;
|
|
10
10
|
/**
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
3
|
import type Infer from "#Infer";
|
|
4
|
-
import type
|
|
4
|
+
import type NormalizeSchema from "#NormalizeSchema";
|
|
5
5
|
import OptionalType from "#OptionalType";
|
|
6
6
|
import type Parsed from "#Parsed";
|
|
7
7
|
import type ParseOptions from "#ParseOptions";
|
|
8
8
|
import type Schema from "#Schema";
|
|
9
9
|
import type DefaultTrait from "#trait/Default";
|
|
10
10
|
import type OptionalTrait from "#trait/Optional";
|
|
11
|
-
import type TupleToUnion from "@rcompat/type
|
|
11
|
+
import type { TupleToUnion } from "@rcompat/type";
|
|
12
12
|
type InferUnion<T extends Schema[]> = TupleToUnion<{
|
|
13
|
-
[K in keyof T]: T[K] extends Schema ?
|
|
13
|
+
[K in keyof T]: T[K] extends Schema ? NormalizeSchema<T[K]>["infer"] : "union-never";
|
|
14
14
|
}>;
|
|
15
15
|
export default class UnionType<T extends Parsed<unknown>[]> extends GenericType<T, InferUnion<T>, "UnionType"> implements OptionalTrait, DefaultTrait<InferUnion<T>> {
|
|
16
16
|
#private;
|
package/lib/private/UnionType.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
|
-
import
|
|
2
|
+
import E from "#errors";
|
|
3
3
|
import GenericType from "#GenericType";
|
|
4
|
-
import schema from "#index";
|
|
5
4
|
import isParsedType from "#is-parsed-type";
|
|
6
5
|
import OptionalType from "#OptionalType";
|
|
7
6
|
import ParseError from "#ParseError";
|
|
7
|
+
import S from "#schema-errors";
|
|
8
8
|
import assert from "@rcompat/assert";
|
|
9
9
|
const print = (type) => {
|
|
10
10
|
const parsed = isParsedType(type);
|
|
@@ -29,7 +29,7 @@ const union_error = (types) => `\`${types.map(t => isParsedType(t) ? t.name : pr
|
|
|
29
29
|
export default class UnionType extends GenericType {
|
|
30
30
|
#of;
|
|
31
31
|
constructor(of) {
|
|
32
|
-
assert(of.length > 1,
|
|
32
|
+
assert.true(of.length > 1, S.union_at_least_two_members());
|
|
33
33
|
super();
|
|
34
34
|
this.#of = of;
|
|
35
35
|
}
|
|
@@ -46,21 +46,19 @@ export default class UnionType extends GenericType {
|
|
|
46
46
|
return new DefaultType(this, value);
|
|
47
47
|
}
|
|
48
48
|
parse(x, options = {}) {
|
|
49
|
-
|
|
50
|
-
const parsed = this.#of.map(type => {
|
|
51
|
-
const validator = isParsedType(type) ? type : schema(type);
|
|
49
|
+
for (const type of this.#of) {
|
|
52
50
|
try {
|
|
53
|
-
|
|
54
|
-
return
|
|
51
|
+
type.parse(x, options);
|
|
52
|
+
return x;
|
|
55
53
|
}
|
|
56
54
|
catch (e) {
|
|
57
|
-
|
|
55
|
+
if (!ParseError.is(e))
|
|
56
|
+
throw e;
|
|
57
|
+
// continue to next
|
|
58
58
|
}
|
|
59
|
-
});
|
|
60
|
-
if (!parsed.some(r => r === true)) {
|
|
61
|
-
throw new ParseError(error(union_error(this.#of), x, options));
|
|
62
59
|
}
|
|
63
|
-
|
|
60
|
+
// all types failed
|
|
61
|
+
throw E.invalid_type(x, union_error(this.#of), options);
|
|
64
62
|
}
|
|
65
63
|
toJSON() {
|
|
66
64
|
return { type: this.name, of: this.#of.map(t => t.toJSON()) };
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import GenericType from "#GenericType";
|
|
2
|
-
const
|
|
2
|
+
const storable = (x) => !!x && typeof x === "object" && "datatype" in x;
|
|
3
3
|
export default class VirtualType extends GenericType {
|
|
4
4
|
get datatype() {
|
|
5
|
-
if (
|
|
5
|
+
if (storable(this.schema))
|
|
6
6
|
return this.schema.datatype;
|
|
7
|
-
}
|
|
8
7
|
throw new Error("cannot be used in a store");
|
|
9
8
|
}
|
|
10
9
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export default function
|
|
1
|
+
export default function coerce_bigint(x: unknown): unknown;
|
|
2
2
|
//# sourceMappingURL=bigint.d.ts.map
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export default function coerceBigInt(x) {
|
|
1
|
+
import is from "@rcompat/is";
|
|
2
|
+
export default function coerce_bigint(x) {
|
|
4
3
|
// normalize from string to number; other types unaffected
|
|
5
|
-
const n = numeric(x) ? Number(x) : x;
|
|
4
|
+
const n = is.numeric(x) ? Number(x) : x;
|
|
6
5
|
// normalize from number to bigint; other types unaffected
|
|
7
|
-
if (
|
|
6
|
+
if (is.int(n))
|
|
8
7
|
return BigInt(n);
|
|
9
|
-
}
|
|
10
8
|
// bigint or invalid
|
|
11
9
|
return x;
|
|
12
10
|
}
|