pema 0.3.0 → 0.5.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 +1 -1
- package/lib/private/ArrayType.js +14 -22
- 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 -1
- package/lib/private/BuiltinType.js +3 -5
- package/lib/private/ConstructorType.d.ts +1 -1
- package/lib/private/ConstructorType.js +3 -5
- package/lib/private/DataType.d.ts +0 -1
- package/lib/private/DateType.d.ts +2 -2
- package/lib/private/DateType.js +2 -2
- package/lib/private/FileType.d.ts +2 -2
- package/lib/private/FileType.js +2 -2
- 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/Issue.d.ts +1 -1
- package/lib/private/LiteralType.d.ts +1 -2
- package/lib/private/LiteralType.js +3 -5
- package/lib/private/NormalizeSchema.d.ts +4 -6
- package/lib/private/NullType.js +3 -5
- package/lib/private/NumericType.d.ts +2 -2
- package/lib/private/ObjectType.d.ts +1 -1
- package/lib/private/ObjectType.js +9 -9
- package/lib/private/OmitType.d.ts +20 -0
- package/lib/private/OmitType.js +43 -0
- package/lib/private/ParseError.d.ts +1 -1
- package/lib/private/ParseError.js +2 -2
- package/lib/private/ParseOptions.d.ts +1 -1
- package/lib/private/Parsed.d.ts +1 -1
- package/lib/private/PartialType.js +6 -9
- package/lib/private/Partialable.d.ts +1 -1
- package/lib/private/PrimitiveType.js +18 -9
- package/lib/private/PureType.d.ts +1 -1
- package/lib/private/RecordType.js +8 -14
- package/lib/private/Schema.d.ts +1 -1
- package/lib/private/Serialized.d.ts +4 -4
- package/lib/private/{Storeable.d.ts → Storable.d.ts} +2 -2
- package/lib/private/{Storeable.js → Storable.js} +2 -2
- package/lib/private/StoreSchema.d.ts +3 -6
- package/lib/private/StoreType.d.ts +4 -12
- package/lib/private/StoreType.js +23 -34
- package/lib/private/StringType.d.ts +2 -2
- package/lib/private/StringType.js +2 -2
- package/lib/private/TupleType.d.ts +2 -2
- package/lib/private/TupleType.js +17 -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/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.js +3 -5
- package/lib/private/coerce/int.js +2 -3
- 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/fail.d.ts +4 -0
- package/lib/private/fail.js +6 -0
- package/lib/private/index.d.ts +6 -2
- package/lib/private/index.js +10 -2
- package/lib/private/json/JSONPayload.d.ts +1 -1
- package/lib/private/normalize.js +2 -3
- package/lib/private/object.d.ts +2 -3
- package/lib/private/omit.d.ts +6 -0
- package/lib/private/omit.js +5 -0
- 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/test/paths-of.d.ts +1 -1
- package/lib/private/validator/integer.js +2 -2
- package/lib/private/validator/length.js +2 -2
- package/lib/private/validator/max.js +2 -2
- package/lib/private/validator/min.js +2 -2
- package/lib/private/validator/range.js +2 -2
- package/lib/private/validator/values.d.ts +1 -1
- package/lib/private/validator/values.js +3 -2
- package/lib/public/index.d.ts +22 -0
- package/lib/public/index.js +10 -0
- package/package.json +13 -11
- 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/primary.d.ts +0 -4
- package/lib/private/primary.js +0 -3
- 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/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,11 +1,11 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
|
-
import
|
|
2
|
+
import fail from "#fail";
|
|
3
3
|
import ParsedKey from "#ParsedKey";
|
|
4
4
|
import ParseError from "#ParseError";
|
|
5
5
|
import join from "#path/join";
|
|
6
6
|
import next from "#path/next";
|
|
7
7
|
import VirtualType from "#VirtualType";
|
|
8
|
-
import
|
|
8
|
+
import is from "@rcompat/is";
|
|
9
9
|
export default class PartialType extends VirtualType {
|
|
10
10
|
#spec;
|
|
11
11
|
constructor(spec) {
|
|
@@ -22,9 +22,8 @@ export default class PartialType extends VirtualType {
|
|
|
22
22
|
return new DefaultType(this, value);
|
|
23
23
|
}
|
|
24
24
|
parse(x, options = {}) {
|
|
25
|
-
if (!
|
|
26
|
-
throw
|
|
27
|
-
}
|
|
25
|
+
if (!is.dict(x))
|
|
26
|
+
throw fail("object", x, options);
|
|
28
27
|
const input = x;
|
|
29
28
|
const out = {};
|
|
30
29
|
const issues = [];
|
|
@@ -40,9 +39,8 @@ export default class PartialType extends VirtualType {
|
|
|
40
39
|
catch (e) {
|
|
41
40
|
if (e instanceof ParseError) {
|
|
42
41
|
// child already rebased to /<key> via nextOptions -> just collect
|
|
43
|
-
if (e.issues && e.issues.length)
|
|
42
|
+
if (e.issues && e.issues.length)
|
|
44
43
|
issues.push(...e.issues);
|
|
45
|
-
}
|
|
46
44
|
}
|
|
47
45
|
else {
|
|
48
46
|
// wrap non-ParseError into a properly-pathed issue at /<key>
|
|
@@ -57,9 +55,8 @@ export default class PartialType extends VirtualType {
|
|
|
57
55
|
}
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
|
-
if (issues.length > 0)
|
|
58
|
+
if (issues.length > 0)
|
|
61
59
|
throw new ParseError(issues);
|
|
62
|
-
}
|
|
63
60
|
return out;
|
|
64
61
|
}
|
|
65
62
|
toJSON() {
|
|
@@ -25,29 +25,38 @@ export default class PrimitiveType extends Type {
|
|
|
25
25
|
return this.derive({ options: { coerce: true } });
|
|
26
26
|
}
|
|
27
27
|
parse(x, options = {}) {
|
|
28
|
-
|
|
29
|
-
const
|
|
28
|
+
// hotpath: avoid object spread when possible
|
|
29
|
+
const has_instance_options = this.#options.coerce !== undefined
|
|
30
|
+
|| this.#options[ParsedKey] !== undefined;
|
|
31
|
+
const $options = has_instance_options
|
|
32
|
+
? { ...this.#options, ...options }
|
|
33
|
+
: options;
|
|
34
|
+
// hotpath: avoid array spread when no option validators
|
|
35
|
+
const option_validators = $options.validators;
|
|
36
|
+
const validators = option_validators && option_validators.length > 0
|
|
37
|
+
? option_validators.concat(this.#validators)
|
|
38
|
+
: this.#validators;
|
|
30
39
|
const $x = $options.coerce === true ? this[CoerceKey](x) : x;
|
|
31
40
|
if (typeof $x !== this.name) {
|
|
32
41
|
throw new ParseError(error(this.name, $x, $options));
|
|
33
42
|
}
|
|
34
43
|
const base = $options[ParsedKey] ?? "";
|
|
35
|
-
for (
|
|
44
|
+
for (let i = 0; i < validators.length; i++) {
|
|
36
45
|
try {
|
|
37
|
-
|
|
46
|
+
validators[i]($x);
|
|
38
47
|
}
|
|
39
48
|
catch (e) {
|
|
40
49
|
if (e instanceof ParseError) {
|
|
41
50
|
// rebase each issue path under `base`
|
|
42
|
-
const rebased = (e.issues ?? []).map(
|
|
43
|
-
...
|
|
44
|
-
path:
|
|
51
|
+
const rebased = (e.issues ?? []).map(issue => ({
|
|
52
|
+
...issue,
|
|
53
|
+
path: issue.path === ""
|
|
45
54
|
? base
|
|
46
|
-
: (base === "" ?
|
|
55
|
+
: (base === "" ? issue.path : (base + issue.path)),
|
|
47
56
|
}));
|
|
48
57
|
throw new ParseError(rebased);
|
|
49
58
|
}
|
|
50
|
-
//
|
|
59
|
+
// not a ParseError - wrap with proper path
|
|
51
60
|
const message = e && typeof e.message === "string"
|
|
52
61
|
? e.message
|
|
53
62
|
: String(e);
|
|
@@ -2,7 +2,7 @@ import type Infer from "#Infer";
|
|
|
2
2
|
import OptionalType from "#OptionalType";
|
|
3
3
|
import Parsed from "#Parsed";
|
|
4
4
|
import type OptionalTrait from "#trait/Optional";
|
|
5
|
-
import type Printable from "@rcompat/type
|
|
5
|
+
import type { Printable } from "@rcompat/type";
|
|
6
6
|
export default class PureType<Type, Name extends string = "PureType"> extends Parsed<Type> implements Printable, OptionalTrait {
|
|
7
7
|
get name(): string;
|
|
8
8
|
get Name(): Name;
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import error from "#error";
|
|
2
1
|
import expected from "#expected";
|
|
2
|
+
import fail from "#fail";
|
|
3
3
|
import GenericType from "#GenericType";
|
|
4
4
|
import OptionalType from "#OptionalType";
|
|
5
5
|
import ParsedKey from "#ParsedKey";
|
|
6
6
|
import ParseError from "#ParseError";
|
|
7
7
|
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);
|
|
8
|
+
import next from "#path/next";
|
|
9
|
+
import is from "@rcompat/is";
|
|
15
10
|
export default class RecordType extends GenericType {
|
|
16
11
|
#key;
|
|
17
12
|
#value;
|
|
@@ -27,9 +22,8 @@ export default class RecordType extends GenericType {
|
|
|
27
22
|
return "record";
|
|
28
23
|
}
|
|
29
24
|
parse(x, options = {}) {
|
|
30
|
-
if (
|
|
31
|
-
throw
|
|
32
|
-
}
|
|
25
|
+
if (!is.dict(x))
|
|
26
|
+
throw fail("object", x, options);
|
|
33
27
|
const key_name = this.#key.name;
|
|
34
28
|
const keys = Object.keys(x);
|
|
35
29
|
const symbols = Object.getOwnPropertySymbols(x);
|
|
@@ -44,21 +38,21 @@ export default class RecordType extends GenericType {
|
|
|
44
38
|
}]);
|
|
45
39
|
}
|
|
46
40
|
keys.forEach(k => {
|
|
47
|
-
if (key_name === "string" &&
|
|
41
|
+
if (key_name === "string" && is.numeric(k)) {
|
|
48
42
|
throw new ParseError([{
|
|
49
43
|
input: x,
|
|
50
44
|
message: expected("string key", +k),
|
|
51
45
|
path: join(base, k),
|
|
52
46
|
}]);
|
|
53
47
|
}
|
|
54
|
-
if (key_name === "number" && !
|
|
48
|
+
if (key_name === "number" && !is.numeric(k)) {
|
|
55
49
|
throw new ParseError([{
|
|
56
50
|
input: x,
|
|
57
51
|
message: expected("number key", k),
|
|
58
52
|
path: join(base, k),
|
|
59
53
|
}]);
|
|
60
54
|
}
|
|
61
|
-
this.#value.parse(x[k],
|
|
55
|
+
this.#value.parse(x[k], next(k, options));
|
|
62
56
|
});
|
|
63
57
|
}
|
|
64
58
|
if (key_name === "symbol") {
|
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
|
};
|
|
@@ -63,9 +62,10 @@ type StructuralSerialized = {
|
|
|
63
62
|
type: "schema";
|
|
64
63
|
of: Serialized;
|
|
65
64
|
} | {
|
|
66
|
-
type: "
|
|
65
|
+
type: "omit";
|
|
66
|
+
properties: Dict<Serialized>;
|
|
67
67
|
} | {
|
|
68
|
-
type: "
|
|
68
|
+
type: "pure";
|
|
69
69
|
};
|
|
70
70
|
type Serialized = PrimitiveSerialized | NumberSerialized | BigIntSerialized | StructuralSerialized;
|
|
71
71
|
export type { Serialized as default };
|
|
@@ -1,7 +1,7 @@
|
|
|
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
|
|
4
|
+
export default abstract class Storable<T extends K = K> extends Parsed<unknown> {
|
|
5
5
|
abstract get datatype(): T;
|
|
6
6
|
abstract toJSON(): Serialized;
|
|
7
7
|
static serialize<N extends string, T extends K>(s: {
|
|
@@ -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,21 +1,13 @@
|
|
|
1
|
-
import GenericType from "#GenericType";
|
|
2
1
|
import type Infer from "#Infer";
|
|
3
|
-
import
|
|
2
|
+
import ObjectType from "#ObjectType";
|
|
4
3
|
import type ParseOptions from "#ParseOptions";
|
|
5
4
|
import PartialType from "#PartialType";
|
|
6
|
-
import type Serialized from "#Serialized";
|
|
7
5
|
import type StoreSchema from "#StoreSchema";
|
|
8
|
-
|
|
9
|
-
export default class StoreType<T extends StoreSchema> extends GenericType<T, InferStore<T>, "StoreType"> {
|
|
6
|
+
export default class StoreType<S extends StoreSchema> extends ObjectType<S> {
|
|
10
7
|
#private;
|
|
11
|
-
constructor(
|
|
8
|
+
constructor(properties: S, pk?: string | null);
|
|
12
9
|
get name(): string;
|
|
13
|
-
get schema(): T;
|
|
14
|
-
partial(): PartialType<T>;
|
|
15
10
|
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
16
|
-
|
|
17
|
-
type: "object";
|
|
18
|
-
properties: Dict<Serialized>;
|
|
19
|
-
};
|
|
11
|
+
partial(): PartialType<any>;
|
|
20
12
|
}
|
|
21
13
|
//# sourceMappingURL=StoreType.d.ts.map
|
package/lib/private/StoreType.js
CHANGED
|
@@ -1,45 +1,34 @@
|
|
|
1
|
-
import
|
|
2
|
-
import ParsedKey from "#ParsedKey";
|
|
1
|
+
import ObjectType from "#ObjectType";
|
|
3
2
|
import PartialType from "#PartialType";
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
import fail from "#fail";
|
|
4
|
+
import next from "#path/next";
|
|
5
|
+
import is from "@rcompat/is";
|
|
6
|
+
export default class StoreType extends ObjectType {
|
|
7
|
+
#pk;
|
|
8
|
+
constructor(properties, pk = null) {
|
|
9
|
+
super(properties);
|
|
10
|
+
this.#pk = pk;
|
|
10
11
|
}
|
|
11
12
|
get name() {
|
|
12
13
|
return "store";
|
|
13
14
|
}
|
|
14
|
-
get schema() {
|
|
15
|
-
return this.#properties;
|
|
16
|
-
}
|
|
17
|
-
partial() {
|
|
18
|
-
return new PartialType(this.#properties);
|
|
19
|
-
}
|
|
20
15
|
parse(x, options = {}) {
|
|
21
|
-
const
|
|
22
|
-
if (
|
|
23
|
-
throw
|
|
16
|
+
const $options = { ...options };
|
|
17
|
+
if (x !== undefined && !is.dict(x))
|
|
18
|
+
throw fail("object", x, $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;
|
|
24
27
|
}
|
|
25
|
-
|
|
26
|
-
for (const k in spec) {
|
|
27
|
-
const r = spec[k].parse(x[k], {
|
|
28
|
-
...options, [ParsedKey]: join(options[ParsedKey] ?? "", String(k)),
|
|
29
|
-
});
|
|
30
|
-
// exclude undefined (optionals)
|
|
31
|
-
if (r !== undefined) {
|
|
32
|
-
result[k] = r;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return x;
|
|
28
|
+
return out;
|
|
36
29
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
for (const [k, v] of Object.entries(this.#properties)) {
|
|
40
|
-
properties[k] = v.toJSON();
|
|
41
|
-
}
|
|
42
|
-
return { type: "object", properties };
|
|
30
|
+
partial() {
|
|
31
|
+
return new PartialType(this.properties);
|
|
43
32
|
}
|
|
44
33
|
}
|
|
45
34
|
//# sourceMappingURL=StoreType.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
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;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import schemafail from "#error/schemafail";
|
|
2
2
|
import PrimitiveType from "#PrimitiveType";
|
|
3
|
-
import
|
|
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";
|
|
@@ -51,7 +51,7 @@ export default class StringType extends PrimitiveType {
|
|
|
51
51
|
return this.derive({ validators: [length(from, to)] });
|
|
52
52
|
}
|
|
53
53
|
toJSON() {
|
|
54
|
-
return
|
|
54
|
+
return Storable.serialize(this);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
//# 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 fail from "#fail";
|
|
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,23 @@ export default class TupleType extends GenericType {
|
|
|
16
15
|
return new OptionalType(this);
|
|
17
16
|
}
|
|
18
17
|
parse(x, options = {}) {
|
|
19
|
-
if (!
|
|
20
|
-
throw
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
});
|
|
18
|
+
if (!Array.isArray(x))
|
|
19
|
+
throw fail("array", x, options);
|
|
20
|
+
const items = this.#items;
|
|
21
|
+
const len = items.length;
|
|
22
|
+
// validate each expected item
|
|
23
|
+
for (let i = 0; i < len; i++)
|
|
24
|
+
items[i].parse(x[i], next(i, options));
|
|
25
|
+
// reject extra items
|
|
26
|
+
if (x.length > len)
|
|
27
|
+
throw fail("undefined", x[len], next(len, options));
|
|
30
28
|
return x;
|
|
31
29
|
}
|
|
32
|
-
toJSON() {
|
|
30
|
+
toJSON() {
|
|
31
|
+
return {
|
|
32
|
+
type: this.name,
|
|
33
|
+
of: this.#items.map(i => i.toJSON()),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
33
36
|
}
|
|
34
37
|
//# 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
|
|
@@ -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,7 +1,6 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
|
-
import
|
|
2
|
+
import fail from "#fail";
|
|
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";
|
|
@@ -29,7 +28,7 @@ const union_error = (types) => `\`${types.map(t => isParsedType(t) ? t.name : pr
|
|
|
29
28
|
export default class UnionType extends GenericType {
|
|
30
29
|
#of;
|
|
31
30
|
constructor(of) {
|
|
32
|
-
assert(of.length > 1, "union type must have at least two members");
|
|
31
|
+
assert.true(of.length > 1, "union type must have at least two members");
|
|
33
32
|
super();
|
|
34
33
|
this.#of = of;
|
|
35
34
|
}
|
|
@@ -46,21 +45,20 @@ export default class UnionType extends GenericType {
|
|
|
46
45
|
return new DefaultType(this, value);
|
|
47
46
|
}
|
|
48
47
|
parse(x, options = {}) {
|
|
49
|
-
|
|
50
|
-
const parsed = this.#of.map(type => {
|
|
51
|
-
const validator = isParsedType(type) ? type : schema(type);
|
|
48
|
+
for (const type of this.#of) {
|
|
52
49
|
try {
|
|
53
|
-
|
|
54
|
-
return
|
|
50
|
+
type.parse(x, options);
|
|
51
|
+
return x;
|
|
55
52
|
}
|
|
56
53
|
catch (e) {
|
|
57
|
-
|
|
54
|
+
if (!(e instanceof ParseError)) {
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
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 fail(union_error(this.#of), x, 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,12 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import numeric from "@rcompat/is/numeric";
|
|
1
|
+
import is from "@rcompat/is";
|
|
3
2
|
export default function coerceBigInt(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
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type Parsed from "#Parsed";
|
|
2
|
+
import type RecordType from "#RecordType";
|
|
3
|
+
import type StringType from "#StringType";
|
|
4
|
+
/**
|
|
5
|
+
* Value is a dictionary (record with string keys) of the given type.
|
|
6
|
+
*
|
|
7
|
+
* @param of - The value type (defaults to p.string)
|
|
8
|
+
*/
|
|
9
|
+
export default function dict(): RecordType<StringType, StringType>;
|
|
10
|
+
export default function dict<const Value extends Parsed<unknown>>(of: Value): RecordType<StringType, Value>;
|
|
11
|
+
//# sourceMappingURL=dict.d.ts.map
|