pema 0.6.1 → 0.7.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 +11 -4
- package/lib/private/BuiltinType.d.ts +1 -1
- package/lib/private/BuiltinType.js +6 -2
- package/lib/private/ConstructorType.d.ts +1 -1
- package/lib/private/ConstructorType.js +3 -1
- package/lib/private/DefaultType.d.ts +1 -1
- package/lib/private/DefaultType.js +6 -5
- package/lib/private/EnumType.d.ts +1 -1
- package/lib/private/EnumType.js +5 -2
- package/lib/private/FunctionType.d.ts +1 -1
- package/lib/private/FunctionType.js +6 -3
- package/lib/private/IntType.d.ts +1 -1
- package/lib/private/IntType.js +2 -2
- package/lib/private/IsType.d.ts +1 -1
- package/lib/private/IsType.js +3 -1
- package/lib/private/JSONType.d.ts +1 -1
- package/lib/private/JSONType.js +3 -1
- package/lib/private/LiteralType.d.ts +1 -2
- package/lib/private/LiteralType.js +3 -4
- package/lib/private/Loose.d.ts +4 -0
- package/lib/private/Loose.js +3 -0
- package/lib/private/NormalizeSchema.d.ts +3 -3
- package/lib/private/NullType.d.ts +1 -1
- package/lib/private/NullType.js +5 -2
- package/lib/private/NumericType.d.ts +2 -1
- package/lib/private/ObjectType.d.ts +1 -1
- package/lib/private/ObjectType.js +18 -5
- package/lib/private/OmitType.d.ts +1 -1
- package/lib/private/OmitType.js +3 -1
- package/lib/private/OptionalType.d.ts +1 -1
- package/lib/private/OptionalType.js +6 -5
- package/lib/private/ParseError.d.ts +1 -1
- package/lib/private/ParseError.js +9 -8
- package/lib/private/ParseOptions.d.ts +2 -1
- package/lib/private/Parsed.d.ts +4 -3
- package/lib/private/Parsed.js +2 -3
- package/lib/private/PartialType.d.ts +1 -1
- package/lib/private/PartialType.js +3 -1
- package/lib/private/PrimitiveType.d.ts +1 -1
- package/lib/private/PrimitiveType.js +7 -4
- package/lib/private/PureType.d.ts +1 -1
- package/lib/private/PureType.js +2 -2
- package/lib/private/RecordType.d.ts +1 -1
- package/lib/private/RecordType.js +3 -1
- package/lib/private/Storable.d.ts +1 -0
- package/lib/private/Storable.js +4 -0
- package/lib/private/StoreType.d.ts +1 -1
- package/lib/private/StoreType.js +4 -2
- package/lib/private/TupleType.d.ts +1 -1
- package/lib/private/TupleType.js +11 -4
- package/lib/private/UUIDType.d.ts +1 -1
- package/lib/private/UUIDType.js +3 -1
- package/lib/private/UUIDV4Type.d.ts +1 -1
- package/lib/private/UUIDV4Type.js +3 -1
- package/lib/private/UUIDV7Type.d.ts +1 -1
- package/lib/private/UUIDV7Type.js +3 -1
- package/lib/private/UintType.d.ts +1 -1
- package/lib/private/UintType.js +2 -2
- package/lib/private/UnionType.d.ts +1 -1
- package/lib/private/UnionType.js +6 -4
- package/lib/private/VirtualType.js +2 -2
- package/lib/private/array.d.ts +6 -4
- package/lib/private/array.js +15 -2
- package/lib/private/blob.d.ts +6 -2
- package/lib/private/blob.js +8 -1
- package/lib/private/boolean.d.ts +6 -5
- package/lib/private/boolean.js +8 -4
- package/lib/private/coerce/date.js +3 -5
- package/lib/private/coerce/int.js +1 -1
- package/lib/private/constructor.d.ts +6 -5
- package/lib/private/constructor.js +16 -1
- package/lib/private/date.d.ts +6 -2
- package/lib/private/date.js +8 -1
- package/lib/private/dict.d.ts +12 -2
- package/lib/private/dict.js +10 -2
- package/lib/private/enum.d.ts +9 -1
- package/lib/private/enum.js +14 -1
- package/lib/private/errors.js +5 -6
- package/lib/private/f32.d.ts +6 -2
- package/lib/private/f32.js +8 -1
- package/lib/private/f64.d.ts +6 -2
- package/lib/private/f64.js +8 -1
- package/lib/private/file.d.ts +6 -2
- package/lib/private/file.js +8 -1
- package/lib/private/function.d.ts +6 -5
- package/lib/private/function.js +8 -1
- package/lib/private/i128.d.ts +6 -2
- package/lib/private/i128.js +8 -1
- package/lib/private/i16.d.ts +6 -2
- package/lib/private/i16.js +8 -1
- package/lib/private/i32.d.ts +6 -2
- package/lib/private/i32.js +8 -1
- package/lib/private/i64.d.ts +6 -2
- package/lib/private/i64.js +8 -1
- package/lib/private/i8.d.ts +6 -2
- package/lib/private/i8.js +8 -1
- package/lib/private/index.d.ts +33 -15
- package/lib/private/index.js +148 -41
- package/lib/private/is-parsed.d.ts +3 -0
- package/lib/private/is-parsed.js +7 -0
- package/lib/private/is.d.ts +6 -2
- package/lib/private/is.js +14 -1
- package/lib/private/json.d.ts +12 -2
- package/lib/private/json.js +14 -1
- package/lib/private/literal.d.ts +10 -2
- package/lib/private/literal.js +16 -1
- package/lib/private/normalize.js +4 -11
- package/lib/private/null.d.ts +6 -2
- package/lib/private/null.js +8 -1
- package/lib/private/object.d.ts +11 -4
- package/lib/private/object.js +22 -1
- package/lib/private/omit.d.ts +9 -1
- package/lib/private/omit.js +14 -1
- package/lib/private/partial.d.ts +9 -2
- package/lib/private/partial.js +16 -2
- package/lib/private/path/next.d.ts +1 -7
- package/lib/private/pure.d.ts +9 -1
- package/lib/private/pure.js +14 -1
- package/lib/private/record.d.ts +6 -5
- package/lib/private/record.js +14 -1
- package/lib/private/resolve.d.ts +2 -0
- package/lib/private/resolve.js +7 -0
- package/lib/private/spec/bigint.d.ts +4 -1
- package/lib/private/spec/bigint.js +27 -26
- package/lib/private/spec/biguint.d.ts +4 -1
- package/lib/private/spec/biguint.js +23 -22
- package/lib/private/spec/int.d.ts +4 -1
- package/lib/private/spec/int.js +24 -23
- package/lib/private/spec/uint.d.ts +4 -1
- package/lib/private/spec/uint.js +22 -21
- package/lib/private/string.d.ts +6 -5
- package/lib/private/string.js +8 -4
- package/lib/private/symbol.d.ts +6 -2
- package/lib/private/symbol.js +8 -1
- package/lib/private/test.d.ts +4 -21
- package/lib/private/test.js +0 -44
- package/lib/private/tuple.d.ts +9 -2
- package/lib/private/tuple.js +16 -1
- package/lib/private/u128.d.ts +6 -2
- package/lib/private/u128.js +8 -1
- package/lib/private/u16.d.ts +6 -2
- package/lib/private/u16.js +8 -1
- package/lib/private/u32.d.ts +6 -5
- package/lib/private/u32.js +8 -1
- package/lib/private/u64.d.ts +6 -2
- package/lib/private/u64.js +8 -1
- package/lib/private/u8.d.ts +6 -2
- package/lib/private/u8.js +8 -1
- package/lib/private/undefined.d.ts +6 -2
- package/lib/private/undefined.js +8 -1
- package/lib/private/union.d.ts +12 -3
- package/lib/private/union.js +14 -1
- package/lib/private/unknown.d.ts +6 -5
- package/lib/private/unknown.js +8 -1
- package/lib/private/url.d.ts +6 -2
- package/lib/private/url.js +8 -1
- package/lib/private/uuid.d.ts +6 -2
- package/lib/private/uuid.js +8 -1
- package/package.json +7 -6
- package/lib/private/is-parsed-type.d.ts +0 -3
- package/lib/private/is-parsed-type.js +0 -6
|
@@ -29,7 +29,7 @@ export default class ArrayType<T extends Parsed<unknown>> extends GenericType<T,
|
|
|
29
29
|
min(limit: number): this;
|
|
30
30
|
max(limit: number): this;
|
|
31
31
|
length(from: number, to: number): this;
|
|
32
|
-
parse(
|
|
32
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
33
33
|
toJSON(): {
|
|
34
34
|
type: "array";
|
|
35
35
|
of: import("./Serialized.js").default;
|
package/lib/private/ArrayType.js
CHANGED
|
@@ -2,6 +2,7 @@ import DefaultType from "#DefaultType";
|
|
|
2
2
|
import EnumType from "#EnumType";
|
|
3
3
|
import E from "#errors";
|
|
4
4
|
import GenericType from "#GenericType";
|
|
5
|
+
import Loose from "#Loose";
|
|
5
6
|
import OptionalType from "#OptionalType";
|
|
6
7
|
import ParsedKey from "#ParsedKey";
|
|
7
8
|
import ParseError from "#ParseError";
|
|
@@ -9,12 +10,14 @@ import join from "#path/join";
|
|
|
9
10
|
import next from "#path/next";
|
|
10
11
|
import rebase from "#path/rebase";
|
|
11
12
|
import PrimitiveType from "#PrimitiveType";
|
|
13
|
+
import resolve from "#resolve";
|
|
12
14
|
import S from "#schema-errors";
|
|
13
15
|
import length from "#validator/length";
|
|
14
16
|
import max from "#validator/max";
|
|
15
17
|
import min from "#validator/min";
|
|
16
18
|
import unique from "#validator/unique";
|
|
17
19
|
import uniqueBy from "#validator/unique-by";
|
|
20
|
+
import is from "@rcompat/is";
|
|
18
21
|
function isPrimitive(x) {
|
|
19
22
|
return x instanceof PrimitiveType || x instanceof EnumType;
|
|
20
23
|
}
|
|
@@ -67,9 +70,13 @@ export default class ArrayType extends GenericType {
|
|
|
67
70
|
length(from, to) {
|
|
68
71
|
return this.derive({ validators: [length(from, to)] });
|
|
69
72
|
}
|
|
70
|
-
parse(
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
+
parse(u, options = {}) {
|
|
74
|
+
const x = resolve(u);
|
|
75
|
+
const $options = this[Loose] !== undefined
|
|
76
|
+
? { ...options, [Loose]: this[Loose] }
|
|
77
|
+
: options;
|
|
78
|
+
const path = $options[ParsedKey] ?? "";
|
|
79
|
+
if (!is.array(x))
|
|
73
80
|
throw E.invalid_type(x, "array", path);
|
|
74
81
|
const item = this.#item;
|
|
75
82
|
const len = x.length;
|
|
@@ -77,7 +84,7 @@ export default class ArrayType extends GenericType {
|
|
|
77
84
|
for (let i = 0; i < len; i++) {
|
|
78
85
|
if (!(i in x))
|
|
79
86
|
throw E.invalid_type(undefined, item.name, join(path, i));
|
|
80
|
-
out[i] = item.parse(x[i], next(i, options));
|
|
87
|
+
out[i] = item.parse(x[i], next(i, $options));
|
|
81
88
|
}
|
|
82
89
|
const validators = this.#validators;
|
|
83
90
|
for (let i = 0; i < validators.length; i++) {
|
|
@@ -6,6 +6,6 @@ export default abstract class BuiltinType<StaticType, Name extends string> exten
|
|
|
6
6
|
#private;
|
|
7
7
|
abstract get Type(): AbstractNewable;
|
|
8
8
|
constructor(options?: ParseOptions);
|
|
9
|
-
parse(
|
|
9
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
10
10
|
}
|
|
11
11
|
//# sourceMappingURL=BuiltinType.d.ts.map
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import CoerceKey from "#CoerceKey";
|
|
2
2
|
import E from "#errors";
|
|
3
|
+
import Loose from "#Loose";
|
|
4
|
+
import resolve from "#resolve";
|
|
3
5
|
import Type from "#Type";
|
|
4
6
|
export default class BuiltinType extends Type {
|
|
5
7
|
#options;
|
|
@@ -11,9 +13,11 @@ export default class BuiltinType extends Type {
|
|
|
11
13
|
const Constructor = this.constructor;
|
|
12
14
|
return new Constructor({ ...this.#options, ...next });
|
|
13
15
|
}
|
|
14
|
-
parse(
|
|
16
|
+
parse(u, options = {}) {
|
|
17
|
+
const x = resolve(u);
|
|
15
18
|
const $options = { ...this.#options, ...options };
|
|
16
|
-
const
|
|
19
|
+
const loose = this[Loose] ?? $options[Loose] ?? false;
|
|
20
|
+
const $x = loose ? this[CoerceKey](x) : x;
|
|
17
21
|
if (!($x instanceof this.Type))
|
|
18
22
|
throw E.invalid_type($x, this.name, $options);
|
|
19
23
|
return $x;
|
|
@@ -12,7 +12,7 @@ export default class ConstructorType<C extends AbstractNewable> extends GenericT
|
|
|
12
12
|
get name(): string;
|
|
13
13
|
optional(): OptionalType<this>;
|
|
14
14
|
default(value: (() => InstanceType<C>) | InstanceType<C>): DefaultType<this, InstanceType<C>>;
|
|
15
|
-
parse(
|
|
15
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
16
16
|
toJSON(): {
|
|
17
17
|
type: "newable";
|
|
18
18
|
of: string;
|
|
@@ -2,6 +2,7 @@ import DefaultType from "#DefaultType";
|
|
|
2
2
|
import E from "#errors";
|
|
3
3
|
import GenericType from "#GenericType";
|
|
4
4
|
import OptionalType from "#OptionalType";
|
|
5
|
+
import resolve from "#resolve";
|
|
5
6
|
export default class ConstructorType extends GenericType {
|
|
6
7
|
#type;
|
|
7
8
|
constructor(t) {
|
|
@@ -17,7 +18,8 @@ export default class ConstructorType extends GenericType {
|
|
|
17
18
|
default(value) {
|
|
18
19
|
return new DefaultType(this, value);
|
|
19
20
|
}
|
|
20
|
-
parse(
|
|
21
|
+
parse(u, options = {}) {
|
|
22
|
+
const x = resolve(u);
|
|
21
23
|
if (!(x instanceof this.#type))
|
|
22
24
|
throw E.invalid_type(x, this.name, options);
|
|
23
25
|
return x;
|
|
@@ -8,7 +8,7 @@ export default class DefaultType<S extends Parsed<unknown>, D extends Infer<S>>
|
|
|
8
8
|
get name(): "default";
|
|
9
9
|
get schema(): S;
|
|
10
10
|
get input(): Infer<S> | undefined;
|
|
11
|
-
parse(
|
|
11
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
12
12
|
toJSON(): {
|
|
13
13
|
type: "default";
|
|
14
14
|
of: import("./Serialized.js").default;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import resolve from "#resolve";
|
|
1
2
|
import VirtualType from "#VirtualType";
|
|
2
3
|
import is from "@rcompat/is";
|
|
3
4
|
export default class DefaultType extends VirtualType {
|
|
@@ -17,13 +18,13 @@ export default class DefaultType extends VirtualType {
|
|
|
17
18
|
get input() {
|
|
18
19
|
return undefined;
|
|
19
20
|
}
|
|
20
|
-
parse(
|
|
21
|
-
let
|
|
21
|
+
parse(u, options = {}) {
|
|
22
|
+
let x = resolve(u);
|
|
22
23
|
// default fallback
|
|
23
|
-
if (is.undefined(
|
|
24
|
-
|
|
24
|
+
if (is.undefined(x)) {
|
|
25
|
+
x = is.function(this.#default) ? this.#default() : this.#default;
|
|
25
26
|
}
|
|
26
|
-
return this.#schema.parse(
|
|
27
|
+
return this.#schema.parse(x, options);
|
|
27
28
|
}
|
|
28
29
|
toJSON() {
|
|
29
30
|
return { type: this.name, of: this.#schema.toJSON() };
|
|
@@ -11,7 +11,7 @@ export default class EnumType<T extends readonly Literal[]> extends GenericType<
|
|
|
11
11
|
get name(): string;
|
|
12
12
|
optional(): OptionalType<this>;
|
|
13
13
|
default<D extends T[number]>(value: (() => D) | D): DefaultType<EnumType<T>, D>;
|
|
14
|
-
parse(
|
|
14
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
15
15
|
toJSON(): {
|
|
16
16
|
type: "enum";
|
|
17
17
|
values: T[number][];
|
package/lib/private/EnumType.js
CHANGED
|
@@ -2,6 +2,8 @@ import DefaultType from "#DefaultType";
|
|
|
2
2
|
import E from "#errors";
|
|
3
3
|
import GenericType from "#GenericType";
|
|
4
4
|
import OptionalType from "#OptionalType";
|
|
5
|
+
import resolve from "#resolve";
|
|
6
|
+
import is from "@rcompat/is";
|
|
5
7
|
export default class EnumType extends GenericType {
|
|
6
8
|
#values;
|
|
7
9
|
constructor(values) {
|
|
@@ -17,8 +19,9 @@ export default class EnumType extends GenericType {
|
|
|
17
19
|
default(value) {
|
|
18
20
|
return new DefaultType(this, value);
|
|
19
21
|
}
|
|
20
|
-
parse(
|
|
21
|
-
|
|
22
|
+
parse(u, options = {}) {
|
|
23
|
+
const x = resolve(u);
|
|
24
|
+
if (!is.string(x) || !this.#values.includes(x)) {
|
|
22
25
|
throw E.invalid_type(x, this.name, options);
|
|
23
26
|
}
|
|
24
27
|
return x;
|
|
@@ -7,7 +7,7 @@ import type { UnknownFunction } from "@rcompat/type";
|
|
|
7
7
|
export default class FunctionType extends GenericType<UnknownFunction, UnknownFunction, "FunctionType"> implements OptionalTrait {
|
|
8
8
|
get name(): "function";
|
|
9
9
|
optional(): OptionalType<this>;
|
|
10
|
-
parse(
|
|
10
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
11
11
|
toJSON(): {
|
|
12
12
|
type: "function";
|
|
13
13
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import E from "#errors";
|
|
1
2
|
import GenericType from "#GenericType";
|
|
2
3
|
import OptionalType from "#OptionalType";
|
|
3
|
-
import
|
|
4
|
+
import resolve from "#resolve";
|
|
5
|
+
import is from "@rcompat/is";
|
|
4
6
|
export default class FunctionType extends GenericType {
|
|
5
7
|
get name() {
|
|
6
8
|
return "function";
|
|
@@ -8,8 +10,9 @@ export default class FunctionType extends GenericType {
|
|
|
8
10
|
optional() {
|
|
9
11
|
return new OptionalType(this);
|
|
10
12
|
}
|
|
11
|
-
parse(
|
|
12
|
-
|
|
13
|
+
parse(u, options = {}) {
|
|
14
|
+
const x = resolve(u);
|
|
15
|
+
if (!is.function(x))
|
|
13
16
|
throw E.invalid_type(x, this.name, options);
|
|
14
17
|
return x;
|
|
15
18
|
}
|
package/lib/private/IntType.d.ts
CHANGED
|
@@ -7,6 +7,6 @@ import type Storable from "#Storable";
|
|
|
7
7
|
export default class IntType<T extends IntDataType> extends NumericType<T, number, "IntType"> implements Storable<T> {
|
|
8
8
|
[CoerceKey]: typeof coerce;
|
|
9
9
|
get name(): string;
|
|
10
|
-
parse(
|
|
10
|
+
parse(u: unknown, options?: ParseOptions<number>): import("./Infer.js").default<this>;
|
|
11
11
|
}
|
|
12
12
|
//# sourceMappingURL=IntType.d.ts.map
|
package/lib/private/IntType.js
CHANGED
package/lib/private/IsType.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export default class IsType<T> extends Parsed<T> implements OptionalTrait, Defau
|
|
|
11
11
|
get name(): "is";
|
|
12
12
|
optional(): OptionalType<this>;
|
|
13
13
|
default(value: (() => T) | T): DefaultType<this, T>;
|
|
14
|
-
parse(
|
|
14
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
15
15
|
toJSON(): {
|
|
16
16
|
type: "is";
|
|
17
17
|
};
|
package/lib/private/IsType.js
CHANGED
|
@@ -2,6 +2,7 @@ import DefaultType from "#DefaultType";
|
|
|
2
2
|
import E from "#errors";
|
|
3
3
|
import OptionalType from "#OptionalType";
|
|
4
4
|
import Parsed from "#Parsed";
|
|
5
|
+
import resolve from "#resolve";
|
|
5
6
|
export default class IsType extends Parsed {
|
|
6
7
|
#predicate;
|
|
7
8
|
constructor(predicate) {
|
|
@@ -17,7 +18,8 @@ export default class IsType extends Parsed {
|
|
|
17
18
|
default(value) {
|
|
18
19
|
return new DefaultType(this, value);
|
|
19
20
|
}
|
|
20
|
-
parse(
|
|
21
|
+
parse(u, options = {}) {
|
|
22
|
+
const x = resolve(u);
|
|
21
23
|
if (!this.#predicate(x))
|
|
22
24
|
throw E.invalid_type(x, this.name, options);
|
|
23
25
|
return x;
|
|
@@ -16,7 +16,7 @@ export default class JSONType<S extends JSONInput = undefined> extends Storable<
|
|
|
16
16
|
get datatype(): "json";
|
|
17
17
|
optional(): OptionalType<this>;
|
|
18
18
|
default(value: (() => Infer<this>) | Infer<this>): DefaultType<this, Infer<this>>;
|
|
19
|
-
parse(
|
|
19
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
20
20
|
toJSON(): {
|
|
21
21
|
type: "json";
|
|
22
22
|
datatype: "json";
|
package/lib/private/JSONType.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import DefaultType from "#DefaultType";
|
|
2
2
|
import E from "#errors";
|
|
3
3
|
import OptionalType from "#OptionalType";
|
|
4
|
+
import resolve from "#resolve";
|
|
4
5
|
import Storable from "#Storable";
|
|
5
6
|
import is from "@rcompat/is";
|
|
6
7
|
export default class JSONType extends Storable {
|
|
@@ -21,7 +22,8 @@ export default class JSONType extends Storable {
|
|
|
21
22
|
default(value) {
|
|
22
23
|
return new DefaultType(this, value);
|
|
23
24
|
}
|
|
24
|
-
parse(
|
|
25
|
+
parse(u, options = {}) {
|
|
26
|
+
const x = resolve(u);
|
|
25
27
|
if (this.#inner !== undefined) {
|
|
26
28
|
// delegate to inner schema for typed validation
|
|
27
29
|
return this.#inner.parse(x, options);
|
|
@@ -7,12 +7,11 @@ type Literal = string | boolean | number;
|
|
|
7
7
|
export default class LiteralType<T extends Literal> extends GenericType<T, T, "LiteralType"> {
|
|
8
8
|
#private;
|
|
9
9
|
constructor(literal: T);
|
|
10
|
-
static new<T extends Literal>(literal: T): LiteralType<T>;
|
|
11
10
|
static get Literal(): Literal;
|
|
12
11
|
get name(): string;
|
|
13
12
|
optional(): OptionalType<this>;
|
|
14
13
|
default(value: (() => T) | T): DefaultType<this, T>;
|
|
15
|
-
parse(
|
|
14
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
16
15
|
toJSON(): {
|
|
17
16
|
type: "literal";
|
|
18
17
|
value: T;
|
|
@@ -2,15 +2,13 @@ import DefaultType from "#DefaultType";
|
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
3
|
import OptionalType from "#OptionalType";
|
|
4
4
|
import E from "#errors";
|
|
5
|
+
import resolve from "#resolve";
|
|
5
6
|
export default class LiteralType extends GenericType {
|
|
6
7
|
#literal;
|
|
7
8
|
constructor(literal) {
|
|
8
9
|
super();
|
|
9
10
|
this.#literal = literal;
|
|
10
11
|
}
|
|
11
|
-
static new(literal) {
|
|
12
|
-
return new LiteralType(literal);
|
|
13
|
-
}
|
|
14
12
|
static get Literal() {
|
|
15
13
|
return undefined;
|
|
16
14
|
}
|
|
@@ -23,7 +21,8 @@ export default class LiteralType extends GenericType {
|
|
|
23
21
|
default(value) {
|
|
24
22
|
return new DefaultType(this, value);
|
|
25
23
|
}
|
|
26
|
-
parse(
|
|
24
|
+
parse(u, options = {}) {
|
|
25
|
+
const x = resolve(u);
|
|
27
26
|
if (x !== this.#literal)
|
|
28
27
|
throw E.invalid_type(x, this.name, options);
|
|
29
28
|
return x;
|
|
@@ -7,13 +7,13 @@ import type Parsed from "#Parsed";
|
|
|
7
7
|
import type Schema from "#Schema";
|
|
8
8
|
import type TupleType from "#TupleType";
|
|
9
9
|
import type UndefinedType from "#UndefinedType";
|
|
10
|
-
import type { AbstractNewable } from "@rcompat/type";
|
|
10
|
+
import type { AbstractNewable, Dict } from "@rcompat/type";
|
|
11
11
|
type NormalizeSchemaArray<T extends Schema[]> = {
|
|
12
12
|
[K in keyof T]: NormalizeSchema<T[K]>;
|
|
13
13
|
};
|
|
14
|
-
export type NormalizeSchemaObject<T extends
|
|
14
|
+
export type NormalizeSchemaObject<T extends Dict> = {
|
|
15
15
|
-readonly [K in keyof T]: NormalizeSchema<T[K]>;
|
|
16
16
|
};
|
|
17
|
-
type NormalizeSchema<S> = S extends Parsed<unknown> ? S : S extends null ? NullType : S extends undefined ? UndefinedType : S extends string | number | boolean ? LiteralType<S> : S extends AbstractNewable ? ConstructorType<S> : S extends [Schema] ? ArrayType<NormalizeSchema<S[0]>> : S extends Schema[] ? TupleType<NormalizeSchemaArray<S>> : S extends
|
|
17
|
+
type NormalizeSchema<S> = S extends Parsed<unknown> ? S : S extends null ? NullType : S extends undefined ? UndefinedType : S extends string | number | boolean ? LiteralType<S> : S extends AbstractNewable ? ConstructorType<S> : S extends [Schema] ? ArrayType<NormalizeSchema<S[0]>> : S extends Schema[] ? TupleType<NormalizeSchemaArray<S>> : S extends Dict ? ObjectType<NormalizeSchemaObject<S>> : never;
|
|
18
18
|
export type { NormalizeSchema as default };
|
|
19
19
|
//# sourceMappingURL=NormalizeSchema.d.ts.map
|
|
@@ -3,7 +3,7 @@ import type ParseOptions from "#ParseOptions";
|
|
|
3
3
|
import PrimitiveType from "#PrimitiveType";
|
|
4
4
|
export default class NullType extends PrimitiveType<null, "NullType"> {
|
|
5
5
|
get name(): "null";
|
|
6
|
-
parse(
|
|
6
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
7
7
|
toJSON(): {
|
|
8
8
|
type: "null";
|
|
9
9
|
};
|
package/lib/private/NullType.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import PrimitiveType from "#PrimitiveType";
|
|
2
2
|
import E from "#errors";
|
|
3
|
+
import resolve from "#resolve";
|
|
4
|
+
import is from "@rcompat/is";
|
|
3
5
|
export default class NullType extends PrimitiveType {
|
|
4
6
|
get name() {
|
|
5
7
|
return "null";
|
|
6
8
|
}
|
|
7
|
-
parse(
|
|
8
|
-
|
|
9
|
+
parse(u, options = {}) {
|
|
10
|
+
const x = resolve(u);
|
|
11
|
+
if (!is.null(x))
|
|
9
12
|
throw E.invalid_type(x, this.name, options);
|
|
10
13
|
return x;
|
|
11
14
|
}
|
|
@@ -3,6 +3,7 @@ import type ParseOptions from "#ParseOptions";
|
|
|
3
3
|
import PrimitiveType from "#PrimitiveType";
|
|
4
4
|
import type Storable from "#Storable";
|
|
5
5
|
import type Validator from "#Validator";
|
|
6
|
+
import type { Dict } from "@rcompat/type";
|
|
6
7
|
type Next<T> = {
|
|
7
8
|
options?: ParseOptions;
|
|
8
9
|
validators?: Validator<T>[];
|
|
@@ -11,7 +12,7 @@ export default abstract class NumericType<Key extends DataKey, T extends bigint
|
|
|
11
12
|
#private;
|
|
12
13
|
constructor(datatype: Key, validators?: Validator<T>[], options?: ParseOptions);
|
|
13
14
|
derive(next: Next<T>): this;
|
|
14
|
-
values(anyof:
|
|
15
|
+
values(anyof: Dict<T>): this;
|
|
15
16
|
range(from: T, to: T): this;
|
|
16
17
|
min(from: T): this;
|
|
17
18
|
max(to: T): this;
|
|
@@ -22,7 +22,7 @@ export default class ObjectType<P extends Dict<Parsed<unknown>>, I = ObjectInfer
|
|
|
22
22
|
extend<E extends {
|
|
23
23
|
[K in keyof E]: K extends keyof P ? never : Parsed<unknown>;
|
|
24
24
|
}>(extra: E | ObjectType<E>): ObjectType<Unpack<P & E>>;
|
|
25
|
-
parse(
|
|
25
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
26
26
|
toJSON(): {
|
|
27
27
|
type: "object";
|
|
28
28
|
properties: Dict<Serialized>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import E from "#errors";
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
|
+
import Loose from "#Loose";
|
|
3
4
|
import OptionalType from "#OptionalType";
|
|
4
5
|
import next from "#path/next";
|
|
6
|
+
import resolve from "#resolve";
|
|
5
7
|
import SE from "#schema-errors";
|
|
6
8
|
import is from "@rcompat/is";
|
|
7
9
|
export default class ObjectType extends GenericType {
|
|
@@ -26,13 +28,19 @@ export default class ObjectType extends GenericType {
|
|
|
26
28
|
}
|
|
27
29
|
#derive(options) {
|
|
28
30
|
const Constructor = this.constructor;
|
|
29
|
-
|
|
31
|
+
const instance = new Constructor(this.#properties, {
|
|
32
|
+
...this.#options, ...options,
|
|
33
|
+
});
|
|
34
|
+
instance[Loose] = this[Loose];
|
|
35
|
+
return instance;
|
|
30
36
|
}
|
|
31
37
|
optional() {
|
|
32
38
|
return new OptionalType(this);
|
|
33
39
|
}
|
|
34
40
|
shape() {
|
|
35
|
-
|
|
41
|
+
const i = new ObjectType(this.#properties, this.#options);
|
|
42
|
+
i[Loose] = this[Loose];
|
|
43
|
+
return i;
|
|
36
44
|
}
|
|
37
45
|
extend(extra) {
|
|
38
46
|
const properties = extra instanceof ObjectType ? extra.properties : extra;
|
|
@@ -40,10 +48,15 @@ export default class ObjectType extends GenericType {
|
|
|
40
48
|
if (key in this.#properties)
|
|
41
49
|
throw SE.extend_key_collision(key);
|
|
42
50
|
}
|
|
43
|
-
|
|
51
|
+
const instance = new ObjectType({ ...this.#properties, ...properties }, this.#options);
|
|
52
|
+
instance[Loose] = this[Loose];
|
|
53
|
+
return instance;
|
|
44
54
|
}
|
|
45
|
-
parse(
|
|
46
|
-
const
|
|
55
|
+
parse(u, options = {}) {
|
|
56
|
+
const x = resolve(u);
|
|
57
|
+
const $options = this[Loose] !== undefined
|
|
58
|
+
? { ...this.#options, ...options, [Loose]: this[Loose] }
|
|
59
|
+
: { ...this.#options, ...options };
|
|
47
60
|
if (is.defined(x) && !is.dict(x))
|
|
48
61
|
throw E.invalid_type(x, "object", $options);
|
|
49
62
|
const input = x ?? {};
|
|
@@ -11,7 +11,7 @@ export default class OmitType<P extends Dict<Parsed<unknown>>, K extends keyof P
|
|
|
11
11
|
#private;
|
|
12
12
|
constructor(type: ObjectType<P>, keys: K[]);
|
|
13
13
|
get name(): "omit";
|
|
14
|
-
parse(
|
|
14
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
15
15
|
toJSON(): {
|
|
16
16
|
type: "omit";
|
|
17
17
|
properties: Dict<Serialized>;
|
package/lib/private/OmitType.js
CHANGED
|
@@ -2,6 +2,7 @@ import E from "#errors";
|
|
|
2
2
|
import GenericType from "#GenericType";
|
|
3
3
|
import ParsedKey from "#ParsedKey";
|
|
4
4
|
import join from "#path/join";
|
|
5
|
+
import resolve from "#resolve";
|
|
5
6
|
import is from "@rcompat/is";
|
|
6
7
|
export default class OmitType extends GenericType {
|
|
7
8
|
#properties;
|
|
@@ -16,7 +17,8 @@ export default class OmitType extends GenericType {
|
|
|
16
17
|
get name() {
|
|
17
18
|
return "omit";
|
|
18
19
|
}
|
|
19
|
-
parse(
|
|
20
|
+
parse(u, options = {}) {
|
|
21
|
+
const x = resolve(u);
|
|
20
22
|
if (!is.dict(x))
|
|
21
23
|
throw E.invalid_type(x, "object", options);
|
|
22
24
|
const out = {};
|
|
@@ -8,7 +8,7 @@ export default class OptionalType<S extends Parsed<unknown>> extends VirtualType
|
|
|
8
8
|
get name(): "optional";
|
|
9
9
|
get schema(): S;
|
|
10
10
|
get nullable(): boolean;
|
|
11
|
-
parse(
|
|
11
|
+
parse(u: unknown, options?: ParseOptions): Infer<this>;
|
|
12
12
|
toJSON(): {
|
|
13
13
|
type: "optional";
|
|
14
14
|
of: import("./Serialized.js").default;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import resolve from "#resolve";
|
|
1
2
|
import VirtualType from "#VirtualType";
|
|
3
|
+
import is from "@rcompat/is";
|
|
2
4
|
export default class OptionalType extends VirtualType {
|
|
3
5
|
#schema;
|
|
4
6
|
constructor(s) {
|
|
@@ -14,13 +16,12 @@ export default class OptionalType extends VirtualType {
|
|
|
14
16
|
get nullable() {
|
|
15
17
|
return true;
|
|
16
18
|
}
|
|
17
|
-
parse(
|
|
18
|
-
const
|
|
19
|
+
parse(u, options = {}) {
|
|
20
|
+
const x = resolve(u);
|
|
19
21
|
// optional
|
|
20
|
-
if (x
|
|
22
|
+
if (is.undefined(x))
|
|
21
23
|
return undefined;
|
|
22
|
-
|
|
23
|
-
return s.parse(x, options);
|
|
24
|
+
return this.#schema.parse(x, options);
|
|
24
25
|
}
|
|
25
26
|
toJSON() {
|
|
26
27
|
return {
|
|
@@ -5,7 +5,7 @@ declare const brand: unique symbol;
|
|
|
5
5
|
export default class ParseError extends Error implements Serializable {
|
|
6
6
|
#private;
|
|
7
7
|
[brand]: boolean;
|
|
8
|
-
static is(
|
|
8
|
+
static is(x: unknown): x is ParseError;
|
|
9
9
|
constructor(issues: ParseIssue[]);
|
|
10
10
|
get issues(): ParseIssue[];
|
|
11
11
|
toJSON(): JSONPayload;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import assert from "@rcompat/assert";
|
|
2
|
+
import is from "@rcompat/is";
|
|
2
3
|
function humanize(path) {
|
|
3
4
|
return path === ""
|
|
4
5
|
? ""
|
|
@@ -16,12 +17,12 @@ function stringify(issue) {
|
|
|
16
17
|
? issue.message
|
|
17
18
|
: `${humanize(issue.path)}: ${issue.message}`;
|
|
18
19
|
}
|
|
19
|
-
const brand = Symbol.for("pema/
|
|
20
|
+
const brand = Symbol.for("pema/ParseError/v0");
|
|
20
21
|
export default class ParseError extends Error {
|
|
21
22
|
[brand] = true;
|
|
22
23
|
#issues = [];
|
|
23
|
-
static is(
|
|
24
|
-
return
|
|
24
|
+
static is(x) {
|
|
25
|
+
return is.branded(x, brand);
|
|
25
26
|
}
|
|
26
27
|
constructor(issues) {
|
|
27
28
|
super(stringify(issues[0]));
|
|
@@ -43,19 +44,19 @@ export default class ParseError extends Error {
|
|
|
43
44
|
messages,
|
|
44
45
|
};
|
|
45
46
|
}
|
|
46
|
-
const
|
|
47
|
+
const payload = {};
|
|
47
48
|
for (const i of issues) {
|
|
48
49
|
const key = i.path;
|
|
49
|
-
if (!(key in
|
|
50
|
-
|
|
50
|
+
if (!(key in payload))
|
|
51
|
+
payload[key] = {
|
|
51
52
|
type: i.type,
|
|
52
53
|
message: i.message,
|
|
53
54
|
messages: [],
|
|
54
55
|
};
|
|
55
|
-
const entry =
|
|
56
|
+
const entry = payload[key];
|
|
56
57
|
entry.messages.push(i.message);
|
|
57
58
|
}
|
|
58
|
-
return
|
|
59
|
+
return payload;
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
//# sourceMappingURL=ParseError.js.map
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type Loose from "#Loose";
|
|
1
2
|
import type ParsedKey from "#ParsedKey";
|
|
2
3
|
import type Validator from "#Validator";
|
|
3
4
|
import type { JSONPointer } from "@rcompat/type";
|
|
4
5
|
export default interface ParseOptions<T = unknown> {
|
|
5
|
-
coerce?: boolean;
|
|
6
6
|
[ParsedKey]?: JSONPointer;
|
|
7
|
+
[Loose]?: boolean;
|
|
7
8
|
partial?: boolean;
|
|
8
9
|
validators?: Validator<T>[];
|
|
9
10
|
}
|
package/lib/private/Parsed.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import CoerceKey from "#CoerceKey";
|
|
2
|
+
import Loose from "#Loose";
|
|
2
3
|
import ParsedKey from "#ParsedKey";
|
|
3
4
|
import type ParseOptions from "#ParseOptions";
|
|
4
5
|
import type Serialized from "#Serialized";
|
|
@@ -7,19 +8,19 @@ export default abstract class Parsed<StaticType> implements Serializable {
|
|
|
7
8
|
get [ParsedKey](): "ParsedKey";
|
|
8
9
|
get infer(): StaticType;
|
|
9
10
|
get nullable(): boolean;
|
|
11
|
+
[Loose]: boolean | undefined;
|
|
10
12
|
[CoerceKey](x: unknown): unknown;
|
|
11
13
|
abstract get name(): string;
|
|
12
14
|
/**
|
|
13
15
|
* Parse the given value.
|
|
14
16
|
*
|
|
15
|
-
* @param
|
|
17
|
+
* @param u Value to parse.
|
|
16
18
|
*
|
|
17
19
|
* @throws `ParseError` if the value could not be parsed.
|
|
18
20
|
*
|
|
19
21
|
* @returns The parsed value, if successfully parsed.
|
|
20
22
|
*/
|
|
21
|
-
abstract parse(
|
|
22
|
-
coerce(x: unknown, options?: ParseOptions): StaticType;
|
|
23
|
+
abstract parse(u: unknown, options?: ParseOptions): StaticType;
|
|
23
24
|
abstract toJSON(): Serialized;
|
|
24
25
|
}
|
|
25
26
|
//# sourceMappingURL=Parsed.d.ts.map
|