pema 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/lib/private/ArrayType.d.ts +31 -5
- package/lib/private/ArrayType.js +86 -22
- package/lib/private/BigIntDataType.d.ts +3 -0
- package/lib/private/BigIntDataType.js +2 -0
- package/lib/private/BigIntType.d.ts +8 -3
- package/lib/private/BigIntType.js +7 -4
- package/lib/private/BigUintDataType.d.ts +3 -0
- package/lib/private/BigUintDataType.js +2 -0
- package/lib/private/BigUintType.d.ts +10 -0
- package/lib/private/BigUintType.js +10 -0
- package/lib/private/BlobType.d.ts +12 -2
- package/lib/private/BlobType.js +12 -2
- package/lib/private/BooleanType.d.ts +10 -2
- package/lib/private/BooleanType.js +14 -2
- package/lib/private/BuiltinType.d.ts +7 -5
- package/lib/private/BuiltinType.js +18 -17
- package/lib/private/CoerceKey.d.ts +4 -0
- package/lib/private/CoerceKey.js +3 -0
- package/lib/private/ConstructorType.d.ts +10 -4
- package/lib/private/ConstructorType.js +13 -11
- package/lib/private/DataKey.d.ts +4 -0
- package/lib/private/DataKey.js +2 -0
- package/lib/private/DataType.d.ts +23 -0
- package/lib/private/DataType.js +2 -0
- package/lib/private/DateType.d.ts +12 -2
- package/lib/private/DateType.js +15 -2
- package/lib/private/DecrementDepth.d.ts +3 -0
- package/lib/private/DecrementDepth.js +2 -0
- package/lib/private/DefaultType.d.ts +13 -6
- package/lib/private/DefaultType.js +19 -11
- package/lib/private/FileType.d.ts +12 -2
- package/lib/private/FileType.js +12 -2
- package/lib/private/FloatDataType.d.ts +3 -0
- package/lib/private/FloatDataType.js +2 -0
- package/lib/private/GenericType.d.ts +2 -2
- package/lib/private/GenericType.js +2 -2
- package/lib/private/Id.d.ts +3 -0
- package/lib/private/Id.js +2 -0
- package/lib/private/Infer.d.ts +2 -2
- package/lib/private/InferInputSchema.d.ts +26 -0
- package/lib/private/InferInputSchema.js +2 -0
- package/lib/private/InferSchema.d.ts +5 -5
- package/lib/private/InferStore.d.ts +9 -0
- package/lib/private/InferStore.js +2 -0
- package/lib/private/InferStoreOut.d.ts +8 -0
- package/lib/private/InferStoreOut.js +2 -0
- package/lib/private/IntDataType.d.ts +3 -0
- package/lib/private/IntDataType.js +2 -0
- package/lib/private/IntType.d.ts +10 -5
- package/lib/private/IntType.js +13 -12
- package/lib/private/Issue.d.ts +6 -0
- package/lib/private/Issue.js +2 -0
- package/lib/private/LiteralType.d.ts +9 -2
- package/lib/private/LiteralType.js +17 -9
- package/lib/private/NormalizeSchema.d.ts +12 -7
- package/lib/private/NullType.d.ts +6 -2
- package/lib/private/NullType.js +11 -11
- package/lib/private/NumberType.d.ts +7 -3
- package/lib/private/NumberType.js +7 -4
- package/lib/private/NumericType.d.ts +25 -0
- package/lib/private/NumericType.js +38 -0
- package/lib/private/ObjectType.d.ts +23 -0
- package/lib/private/ObjectType.js +53 -0
- package/lib/private/OptionalType.d.ts +12 -5
- package/lib/private/OptionalType.js +16 -4
- package/lib/private/ParseError.d.ts +10 -0
- package/lib/private/ParseError.js +51 -0
- package/lib/private/ParseIssue.d.ts +5 -0
- package/lib/private/ParseIssue.js +2 -0
- package/lib/private/ParseOptions.d.ts +10 -0
- package/lib/private/ParseOptions.js +3 -0
- package/lib/private/Parsed.d.ts +24 -0
- package/lib/private/Parsed.js +17 -0
- package/lib/private/ParsedKey.d.ts +4 -0
- package/lib/private/ParsedKey.js +3 -0
- package/lib/private/PartialType.d.ts +25 -0
- package/lib/private/PartialType.js +69 -0
- package/lib/private/Partialable.d.ts +5 -0
- package/lib/private/Partialable.js +2 -0
- package/lib/private/PrimaryType.d.ts +14 -0
- package/lib/private/PrimaryType.js +26 -0
- package/lib/private/PrimitiveType.d.ts +12 -4
- package/lib/private/PrimitiveType.js +50 -17
- package/lib/private/PureType.d.ts +15 -0
- package/lib/private/PureType.js +21 -0
- package/lib/private/RecordType.d.ts +19 -4
- package/lib/private/RecordType.js +43 -14
- package/lib/private/RecordTypeKey.d.ts +1 -1
- package/lib/private/Schema.d.ts +3 -3
- package/lib/private/SchemaError.d.ts +4 -0
- package/lib/private/SchemaError.js +8 -0
- package/lib/private/Serialized.d.ts +72 -0
- package/lib/private/Serialized.js +2 -0
- package/lib/private/StoreId.d.ts +6 -0
- package/lib/private/StoreId.js +2 -0
- package/lib/private/StoreSchema.d.ts +8 -0
- package/lib/private/StoreSchema.js +2 -0
- package/lib/private/StoreType.d.ts +21 -0
- package/lib/private/StoreType.js +45 -0
- package/lib/private/Storeable.d.ts +15 -0
- package/lib/private/Storeable.js +7 -0
- package/lib/private/StringType.d.ts +17 -5
- package/lib/private/StringType.js +47 -13
- package/lib/private/SymbolType.d.ts +4 -1
- package/lib/private/SymbolType.js +5 -2
- package/lib/private/TupleType.d.ts +11 -4
- package/lib/private/TupleType.js +16 -22
- package/lib/private/Type.d.ts +5 -3
- package/lib/private/Type.js +2 -2
- package/lib/private/URLType.d.ts +16 -2
- package/lib/private/URLType.js +12 -2
- package/lib/private/UintDataType.d.ts +3 -0
- package/lib/private/UintDataType.js +2 -0
- package/lib/private/UintType.d.ts +14 -7
- package/lib/private/UintType.js +20 -12
- package/lib/private/UndefinedType.d.ts +4 -1
- package/lib/private/UndefinedType.js +7 -2
- package/lib/private/UnionType.d.ts +15 -4
- package/lib/private/UnionType.js +32 -26
- package/lib/private/UnknownType.d.ts +5 -0
- package/lib/private/UnknownType.js +7 -0
- package/lib/private/Validator.d.ts +1 -1
- package/lib/private/VirtualType.d.ts +6 -0
- package/lib/private/VirtualType.js +11 -0
- package/lib/private/array.d.ts +6 -3
- package/lib/private/array.js +7 -1
- package/lib/private/bigint.d.ts +1 -3
- package/lib/private/bigint.js +1 -2
- package/lib/private/biguint.d.ts +2 -0
- package/lib/private/biguint.js +2 -0
- package/lib/private/boolean.d.ts +3 -0
- package/lib/private/boolean.js +3 -0
- package/lib/private/coerce/bigint.d.ts +2 -0
- package/lib/private/coerce/bigint.js +13 -0
- package/lib/private/coerce/date.d.ts +2 -0
- package/lib/private/coerce/date.js +56 -0
- package/lib/private/coerce/int.d.ts +2 -0
- package/lib/private/coerce/int.js +8 -0
- package/lib/private/constructor.d.ts +5 -2
- package/lib/private/constructor.js +3 -0
- package/lib/private/error/fail.d.ts +3 -0
- package/lib/private/error/fail.js +5 -0
- package/lib/private/error/schemafail.d.ts +3 -0
- package/lib/private/error/schemafail.js +5 -0
- package/lib/private/error.d.ts +3 -2
- package/lib/private/error.js +10 -3
- package/lib/private/expect.d.ts +12 -11
- package/lib/private/expect.js +22 -11
- package/lib/private/expected.js +10 -1
- package/lib/private/f32.d.ts +4 -0
- package/lib/private/f32.js +4 -0
- package/lib/private/f64.d.ts +4 -0
- package/lib/private/f64.js +3 -0
- package/lib/private/i128.d.ts +4 -0
- package/lib/private/i128.js +6 -0
- package/lib/private/i16.d.ts +4 -0
- package/lib/private/i16.js +6 -0
- package/lib/private/i32.d.ts +4 -0
- package/lib/private/i32.js +6 -0
- package/lib/private/i64.d.ts +4 -0
- package/lib/private/i64.js +6 -0
- package/lib/private/i8.d.ts +4 -0
- package/lib/private/i8.js +6 -0
- package/lib/private/index.d.ts +4 -2
- package/lib/private/index.js +5 -40
- package/lib/private/int.d.ts +1 -3
- package/lib/private/int.js +1 -2
- package/lib/private/is-parsed-type.d.ts +3 -0
- package/lib/private/is-parsed-type.js +6 -0
- package/lib/private/json/JSONIssue.d.ts +6 -0
- package/lib/private/json/JSONIssue.js +2 -0
- package/lib/private/json/JSONPayload.d.ts +5 -0
- package/lib/private/json/JSONPayload.js +2 -0
- package/lib/private/literal.d.ts +1 -2
- package/lib/private/literal.js +1 -1
- package/lib/private/normalize.d.ts +4 -0
- package/lib/private/normalize.js +42 -0
- package/lib/private/number.d.ts +1 -3
- package/lib/private/number.js +1 -2
- package/lib/private/object.d.ts +11 -0
- package/lib/private/object.js +10 -0
- package/lib/private/optional.d.ts +2 -2
- package/lib/private/partial.d.ts +9 -0
- package/lib/private/partial.js +8 -0
- package/lib/private/path/join.d.ts +3 -0
- package/lib/private/path/join.js +13 -0
- package/lib/private/path/next.d.ts +9 -0
- package/lib/private/path/next.js +9 -0
- package/lib/private/path/rebase.d.ts +3 -0
- package/lib/private/path/rebase.js +9 -0
- package/lib/private/primary.d.ts +4 -0
- package/lib/private/primary.js +3 -0
- package/lib/private/pure.d.ts +3 -0
- package/lib/private/pure.js +5 -0
- package/lib/private/record.d.ts +5 -2
- package/lib/private/record.js +3 -0
- package/lib/private/spec/bigint.d.ts +5 -0
- package/lib/private/spec/bigint.js +73 -0
- package/lib/private/spec/biguint.d.ts +5 -0
- package/lib/private/spec/biguint.js +73 -0
- package/lib/private/spec/int.d.ts +5 -0
- package/lib/private/spec/int.js +70 -0
- package/lib/private/spec/uint.d.ts +5 -0
- package/lib/private/spec/uint.js +73 -0
- package/lib/private/string.d.ts +3 -0
- package/lib/private/string.js +3 -0
- package/lib/private/test/messages-of.d.ts +3 -0
- package/lib/private/test/messages-of.js +4 -0
- package/lib/private/test/paths-of.d.ts +3 -0
- package/lib/private/test/paths-of.js +4 -0
- package/lib/private/test/throws-issues.d.ts +4 -0
- package/lib/private/test/throws-issues.js +13 -0
- package/lib/private/trait/Default.d.ts +9 -0
- package/lib/private/trait/Default.js +4 -0
- package/lib/private/trait/Optional.d.ts +11 -0
- package/lib/private/trait/Optional.js +4 -0
- package/lib/private/tuple.d.ts +6 -2
- package/lib/private/tuple.js +6 -1
- package/lib/private/u128.d.ts +4 -0
- package/lib/private/u128.js +6 -0
- package/lib/private/u16.d.ts +4 -0
- package/lib/private/u16.js +6 -0
- package/lib/private/u32.d.ts +7 -0
- package/lib/private/u32.js +9 -0
- package/lib/private/u64.d.ts +4 -0
- package/lib/private/u64.js +6 -0
- package/lib/private/u8.d.ts +4 -0
- package/lib/private/u8.js +6 -0
- package/lib/private/uint.d.ts +1 -3
- package/lib/private/uint.js +1 -2
- package/lib/private/union.d.ts +6 -5
- package/lib/private/union.js +4 -1
- package/lib/private/unknown.d.ts +7 -0
- package/lib/private/unknown.js +6 -0
- package/lib/private/validator/email.d.ts +3 -0
- package/lib/private/validator/email.js +4 -0
- package/lib/private/validator/ends-with.d.ts +4 -0
- package/lib/private/validator/ends-with.js +11 -0
- package/lib/private/validator/f32.d.ts +2 -0
- package/lib/private/validator/f32.js +11 -0
- package/lib/private/validator/integer.d.ts +3 -0
- package/lib/private/validator/integer.js +12 -0
- package/lib/private/validator/isotime.d.ts +2 -0
- package/lib/private/validator/isotime.js +30 -0
- package/lib/private/validator/length.d.ts +5 -0
- package/lib/private/validator/length.js +29 -0
- package/lib/private/validator/max.d.ts +6 -0
- package/lib/private/validator/max.js +37 -0
- package/lib/private/validator/min.d.ts +6 -0
- package/lib/private/validator/min.js +37 -0
- package/lib/private/validator/port.d.ts +3 -0
- package/lib/private/validator/port.js +15 -0
- package/lib/private/validator/range.d.ts +3 -0
- package/lib/private/validator/range.js +22 -0
- package/lib/private/validator/regex.d.ts +5 -0
- package/lib/private/validator/regex.js +15 -0
- package/lib/private/validator/starts-with.d.ts +4 -0
- package/lib/private/validator/starts-with.js +11 -0
- package/lib/private/validator/unique-by.d.ts +2 -0
- package/lib/private/validator/unique-by.js +23 -0
- package/lib/private/validator/unique-with.d.ts +2 -0
- package/lib/private/validator/unique-with.js +18 -0
- package/lib/private/validator/unique.d.ts +2 -0
- package/lib/private/validator/unique.js +19 -0
- package/lib/private/validator/uuid.d.ts +3 -0
- package/lib/private/validator/uuid.js +4 -0
- package/lib/private/validator/values.d.ts +4 -0
- package/lib/private/validator/values.js +14 -0
- package/lib/public/DataType.d.ts +2 -0
- package/lib/public/DataType.js +2 -0
- package/lib/public/Id.d.ts +2 -0
- package/lib/public/Id.js +2 -0
- package/lib/public/InferStore.d.ts +2 -0
- package/lib/public/InferStore.js +2 -0
- package/lib/public/InferStoreOut.d.ts +2 -0
- package/lib/public/InferStoreOut.js +2 -0
- package/lib/public/Issue.d.ts +2 -0
- package/lib/public/Issue.js +2 -0
- package/lib/public/JSONPayload.d.ts +2 -0
- package/lib/public/JSONPayload.js +2 -0
- package/lib/public/ParseError.d.ts +2 -0
- package/lib/public/ParseError.js +2 -0
- package/lib/public/Serialized.d.ts +2 -0
- package/lib/public/Serialized.js +2 -0
- package/lib/public/StoreId.d.ts +2 -0
- package/lib/public/StoreId.js +2 -0
- package/lib/public/StoreSchema.d.ts +2 -0
- package/lib/public/StoreSchema.js +2 -0
- package/lib/public/StoreType.d.ts +3 -0
- package/lib/public/StoreType.js +2 -0
- package/lib/public/array.d.ts +3 -0
- package/lib/public/array.js +3 -0
- package/lib/public/bigint.d.ts +2 -0
- package/lib/public/bigint.js +2 -0
- package/lib/public/biguint.d.ts +2 -0
- package/lib/public/biguint.js +2 -0
- package/lib/public/boolean.d.ts +1 -0
- package/lib/public/boolean.js +1 -0
- package/lib/public/constructor.d.ts +1 -0
- package/lib/public/constructor.js +1 -0
- package/lib/public/f32.d.ts +2 -0
- package/lib/public/f32.js +2 -0
- package/lib/public/f64.d.ts +2 -0
- package/lib/public/f64.js +2 -0
- package/lib/public/i128.d.ts +2 -0
- package/lib/public/i128.js +2 -0
- package/lib/public/i16.d.ts +2 -0
- package/lib/public/i16.js +2 -0
- package/lib/public/i32.d.ts +2 -0
- package/lib/public/i32.js +2 -0
- package/lib/public/i64.d.ts +2 -0
- package/lib/public/i64.js +2 -0
- package/lib/public/i8.d.ts +2 -0
- package/lib/public/i8.js +2 -0
- package/lib/public/index.d.ts +6 -0
- package/lib/public/index.js +6 -0
- package/lib/public/number.d.ts +1 -0
- package/lib/public/number.js +1 -0
- package/lib/public/primary.d.ts +2 -0
- package/lib/public/primary.js +2 -0
- package/lib/public/pure.d.ts +2 -0
- package/lib/public/pure.js +2 -0
- package/lib/public/record.d.ts +1 -0
- package/lib/public/record.js +1 -0
- package/lib/public/string.d.ts +1 -0
- package/lib/public/string.js +1 -0
- package/lib/public/u128.d.ts +2 -0
- package/lib/public/u128.js +2 -0
- package/lib/public/u16.d.ts +2 -0
- package/lib/public/u16.js +2 -0
- package/lib/public/u32.d.ts +2 -0
- package/lib/public/u32.js +2 -0
- package/lib/public/u64.d.ts +2 -0
- package/lib/public/u64.js +2 -0
- package/lib/public/u8.d.ts +2 -0
- package/lib/public/u8.js +2 -0
- package/lib/public/uint.d.ts +3 -0
- package/lib/public/uint.js +3 -0
- package/lib/public/union.d.ts +1 -0
- package/lib/public/union.js +1 -0
- package/lib/public/unknown.d.ts +3 -0
- package/lib/public/unknown.js +2 -0
- package/package.json +14 -11
- package/lib/private/AbstractConstructor.d.ts +0 -3
- package/lib/private/AbstractConstructor.js +0 -2
- package/lib/private/MaybeInt.d.ts +0 -3
- package/lib/private/MaybeInt.js +0 -2
- package/lib/private/SchemaType.d.ts +0 -13
- package/lib/private/SchemaType.js +0 -63
- package/lib/private/Validated.d.ts +0 -8
- package/lib/private/Validated.js +0 -10
- package/lib/private/ValidatedKey.d.ts +0 -4
- package/lib/private/ValidatedKey.js +0 -3
- package/lib/private/is-int.d.ts +0 -3
- package/lib/private/is-int.js +0 -5
- package/lib/private/is_validated_type.d.ts +0 -4
- package/lib/private/is_validated_type.js +0 -5
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
function humanize(path) {
|
|
2
|
+
return path === ""
|
|
3
|
+
? ""
|
|
4
|
+
: path
|
|
5
|
+
.slice(1)
|
|
6
|
+
.split("/")
|
|
7
|
+
.map(seg => seg.replace(/~1/g, "/").replace(/~0/g, "~"))
|
|
8
|
+
.map(seg => `.${seg}`)
|
|
9
|
+
.join("");
|
|
10
|
+
}
|
|
11
|
+
function stringify(issue) {
|
|
12
|
+
// For root (scalar) errors, keep just the message;
|
|
13
|
+
// otherwise prefix with humanized path
|
|
14
|
+
return issue.path === ""
|
|
15
|
+
? issue.message
|
|
16
|
+
: `${humanize(issue.path)}: ${issue.message}`;
|
|
17
|
+
}
|
|
18
|
+
export default class ParseError extends Error {
|
|
19
|
+
#issues;
|
|
20
|
+
constructor(issues) {
|
|
21
|
+
super(stringify(issues[0]));
|
|
22
|
+
this.name = "ParseError";
|
|
23
|
+
this.#issues = issues;
|
|
24
|
+
}
|
|
25
|
+
get issues() {
|
|
26
|
+
return this.#issues;
|
|
27
|
+
}
|
|
28
|
+
toJSON() {
|
|
29
|
+
const issues = this.#issues ?? [];
|
|
30
|
+
if (issues.length === 0) {
|
|
31
|
+
return { message: "Parsing failed", messages: ["Parsing failed"] };
|
|
32
|
+
}
|
|
33
|
+
const isForm = issues.some(i => i.path !== "");
|
|
34
|
+
if (!isForm) {
|
|
35
|
+
const messages = issues.map(i => i.message);
|
|
36
|
+
return { message: messages[0], messages };
|
|
37
|
+
}
|
|
38
|
+
const dict = {};
|
|
39
|
+
for (const i of issues) {
|
|
40
|
+
const key = i.path;
|
|
41
|
+
if (!dict[key])
|
|
42
|
+
dict[key] = { message: i.message, messages: [] };
|
|
43
|
+
dict[key].messages.push(i.message);
|
|
44
|
+
// Keep the first as the short message
|
|
45
|
+
if (dict[key].messages.length === 1)
|
|
46
|
+
dict[key].message = i.message;
|
|
47
|
+
}
|
|
48
|
+
return dict;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=ParseError.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type ParsedKey from "#ParsedKey";
|
|
2
|
+
import type Validator from "#Validator";
|
|
3
|
+
import type JSONPointer from "@rcompat/type/JSONPointer";
|
|
4
|
+
export default interface ParseOptions<T = unknown> {
|
|
5
|
+
coerce?: boolean;
|
|
6
|
+
[ParsedKey]?: JSONPointer;
|
|
7
|
+
partial?: boolean;
|
|
8
|
+
validators?: Validator<T>[];
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=ParseOptions.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import CoerceKey from "#CoerceKey";
|
|
2
|
+
import ParsedKey from "#ParsedKey";
|
|
3
|
+
import type ParseOptions from "#ParseOptions";
|
|
4
|
+
import type Serialized from "#Serialized";
|
|
5
|
+
import type Serializable from "@rcompat/type/Serializable";
|
|
6
|
+
export default abstract class Parsed<StaticType> implements Serializable {
|
|
7
|
+
get [ParsedKey](): "ParsedKey";
|
|
8
|
+
get infer(): StaticType;
|
|
9
|
+
get nullable(): boolean;
|
|
10
|
+
[CoerceKey](x: unknown): unknown;
|
|
11
|
+
abstract get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Parse the given value.
|
|
14
|
+
*
|
|
15
|
+
* @param x Value to parse.
|
|
16
|
+
*
|
|
17
|
+
* @throws `ParseError` if the value could not be parsed.
|
|
18
|
+
*
|
|
19
|
+
* @returns The parsed value, if successfully parsed.
|
|
20
|
+
*/
|
|
21
|
+
abstract parse(x: unknown, options?: ParseOptions): StaticType;
|
|
22
|
+
abstract toJSON(): Serialized;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=Parsed.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import CoerceKey from "#CoerceKey";
|
|
2
|
+
import ParsedKey from "#ParsedKey";
|
|
3
|
+
export default class Parsed {
|
|
4
|
+
get [ParsedKey]() {
|
|
5
|
+
return "ParsedKey";
|
|
6
|
+
}
|
|
7
|
+
get infer() {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
get nullable() {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
[CoerceKey](x) {
|
|
14
|
+
return x;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=Parsed.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import type Infer from "#Infer";
|
|
3
|
+
import type ParseOptions from "#ParseOptions";
|
|
4
|
+
import type Partialable from "#Partialable";
|
|
5
|
+
import type DefaultTrait from "#trait/Default";
|
|
6
|
+
import VirtualType from "#VirtualType";
|
|
7
|
+
type InferPartial<D extends Partialable> = {
|
|
8
|
+
-readonly [K in keyof D]?: NonNullable<Infer<D[K]>>;
|
|
9
|
+
};
|
|
10
|
+
export default class PartialType<D extends Partialable> extends VirtualType<D, InferPartial<D>, "PartialType"> implements DefaultTrait<InferPartial<D>> {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(spec: D);
|
|
13
|
+
get name(): "partial";
|
|
14
|
+
get schema(): D;
|
|
15
|
+
default(value: (() => InferPartial<D>) | InferPartial<D>): DefaultType<this, InferPartial<D>>;
|
|
16
|
+
parse(x: unknown, options?: ParseOptions): InferPartial<D>;
|
|
17
|
+
toJSON(): {
|
|
18
|
+
type: "partial";
|
|
19
|
+
of: {
|
|
20
|
+
type: "string";
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=PartialType.d.ts.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import DefaultType from "#DefaultType";
|
|
2
|
+
import error from "#error";
|
|
3
|
+
import ParsedKey from "#ParsedKey";
|
|
4
|
+
import ParseError from "#ParseError";
|
|
5
|
+
import join from "#path/join";
|
|
6
|
+
import next from "#path/next";
|
|
7
|
+
import VirtualType from "#VirtualType";
|
|
8
|
+
import isDict from "@rcompat/is/dict";
|
|
9
|
+
export default class PartialType extends VirtualType {
|
|
10
|
+
#spec;
|
|
11
|
+
constructor(spec) {
|
|
12
|
+
super();
|
|
13
|
+
this.#spec = spec;
|
|
14
|
+
}
|
|
15
|
+
get name() {
|
|
16
|
+
return "partial";
|
|
17
|
+
}
|
|
18
|
+
get schema() {
|
|
19
|
+
return this.#spec;
|
|
20
|
+
}
|
|
21
|
+
default(value) {
|
|
22
|
+
return new DefaultType(this, value);
|
|
23
|
+
}
|
|
24
|
+
parse(x, options = {}) {
|
|
25
|
+
if (!isDict(x)) {
|
|
26
|
+
throw new ParseError(error("object", x, options));
|
|
27
|
+
}
|
|
28
|
+
const input = x;
|
|
29
|
+
const out = {};
|
|
30
|
+
const issues = [];
|
|
31
|
+
for (const key of Object.keys(this.#spec)) {
|
|
32
|
+
// skip missing/undefined keys (partial semantics)
|
|
33
|
+
if (!(key in input) || input[key] === undefined)
|
|
34
|
+
continue;
|
|
35
|
+
try {
|
|
36
|
+
const parsed = this.#spec[key].parse(input[key], next(key, options));
|
|
37
|
+
if (parsed !== undefined)
|
|
38
|
+
out[key] = parsed;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
if (e instanceof ParseError) {
|
|
42
|
+
// child already rebased to /<key> via nextOptions -> just collect
|
|
43
|
+
if (e.issues && e.issues.length) {
|
|
44
|
+
issues.push(...e.issues);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// wrap non-ParseError into a properly-pathed issue at /<key>
|
|
49
|
+
const message = e && typeof e.message === "string"
|
|
50
|
+
? e.message
|
|
51
|
+
: String(e);
|
|
52
|
+
issues.push({
|
|
53
|
+
input: input[key],
|
|
54
|
+
message: message,
|
|
55
|
+
path: join(options[ParsedKey] ?? "", key),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (issues.length > 0) {
|
|
61
|
+
throw new ParseError(issues);
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
toJSON() {
|
|
66
|
+
return { type: this.name, of: { type: "string" } };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=PartialType.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import type ParseOptions from "#ParseOptions";
|
|
3
|
+
import PrimitiveType from "#PrimitiveType";
|
|
4
|
+
import Storeable from "#Storeable";
|
|
5
|
+
export default class PrimaryType extends PrimitiveType<string | undefined, "PrimaryType"> implements Storeable<"primary"> {
|
|
6
|
+
get name(): "primary";
|
|
7
|
+
get datatype(): "primary";
|
|
8
|
+
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
9
|
+
toJSON(): {
|
|
10
|
+
type: "primary";
|
|
11
|
+
datatype: "primary";
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=PrimaryType.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import error from "#error";
|
|
2
|
+
import ParseError from "#ParseError";
|
|
3
|
+
import PrimitiveType from "#PrimitiveType";
|
|
4
|
+
import Storeable from "#Storeable";
|
|
5
|
+
export default class PrimaryType extends PrimitiveType {
|
|
6
|
+
get name() {
|
|
7
|
+
return "primary";
|
|
8
|
+
}
|
|
9
|
+
get datatype() {
|
|
10
|
+
return "primary";
|
|
11
|
+
}
|
|
12
|
+
parse(x, options = {}) {
|
|
13
|
+
// the primary type is an optional ype
|
|
14
|
+
if (x === undefined) {
|
|
15
|
+
return x;
|
|
16
|
+
}
|
|
17
|
+
if (typeof x !== "string") {
|
|
18
|
+
throw new ParseError(error(this.name, x, options));
|
|
19
|
+
}
|
|
20
|
+
return x;
|
|
21
|
+
}
|
|
22
|
+
toJSON() {
|
|
23
|
+
return Storeable.serialize(this);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=PrimaryType.js.map
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import type Infer from "#Infer";
|
|
2
|
+
import type ParseOptions from "#ParseOptions";
|
|
2
3
|
import Type from "#Type";
|
|
3
4
|
import type Validator from "#Validator";
|
|
4
|
-
|
|
5
|
+
type Next<T> = {
|
|
6
|
+
options?: ParseOptions;
|
|
7
|
+
validators?: Validator<T>[];
|
|
8
|
+
};
|
|
9
|
+
export default abstract class PrimitiveType<StaticType, Name extends string> extends Type<StaticType, Name> {
|
|
5
10
|
#private;
|
|
6
|
-
constructor(
|
|
11
|
+
constructor(validators?: Validator<StaticType>[], options?: ParseOptions);
|
|
12
|
+
get options(): ParseOptions<unknown>;
|
|
7
13
|
get validators(): Validator<StaticType>[];
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
derive(next: Next<StaticType>): this;
|
|
15
|
+
get coerce(): this;
|
|
16
|
+
parse(x: unknown, options?: ParseOptions<StaticType>): Infer<this>;
|
|
10
17
|
}
|
|
18
|
+
export {};
|
|
11
19
|
//# sourceMappingURL=PrimitiveType.d.ts.map
|
|
@@ -1,31 +1,64 @@
|
|
|
1
|
-
import
|
|
1
|
+
import CoerceKey from "#CoerceKey";
|
|
2
|
+
import error from "#error";
|
|
3
|
+
import ParsedKey from "#ParsedKey";
|
|
4
|
+
import ParseError from "#ParseError";
|
|
2
5
|
import Type from "#Type";
|
|
3
|
-
const error_message = (name, x, key) => {
|
|
4
|
-
const base = expected(name, x);
|
|
5
|
-
return key === undefined
|
|
6
|
-
? base
|
|
7
|
-
: `${key}: ${base}`;
|
|
8
|
-
};
|
|
9
6
|
export default class PrimitiveType extends Type {
|
|
10
|
-
#name;
|
|
11
7
|
#validators;
|
|
12
|
-
|
|
8
|
+
#options;
|
|
9
|
+
constructor(validators = [], options = {}) {
|
|
13
10
|
super();
|
|
14
|
-
this.#name = name;
|
|
15
11
|
this.#validators = validators;
|
|
12
|
+
this.#options = options;
|
|
13
|
+
}
|
|
14
|
+
get options() {
|
|
15
|
+
return this.#options;
|
|
16
16
|
}
|
|
17
17
|
get validators() {
|
|
18
18
|
return this.#validators;
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
derive(next) {
|
|
21
|
+
const Constructor = this.constructor;
|
|
22
|
+
return new Constructor([...this.#validators, ...next.validators ?? []], { ...this.#options, ...next.options ?? {} });
|
|
23
|
+
}
|
|
24
|
+
get coerce() {
|
|
25
|
+
return this.derive({ options: { coerce: true } });
|
|
22
26
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
parse(x, options = {}) {
|
|
28
|
+
const $options = { ...this.#options, ...options };
|
|
29
|
+
const validators = [...$options.validators ?? [], ...this.#validators];
|
|
30
|
+
const $x = $options.coerce === true ? this[CoerceKey](x) : x;
|
|
31
|
+
if (typeof $x !== this.name) {
|
|
32
|
+
throw new ParseError(error(this.name, $x, $options));
|
|
33
|
+
}
|
|
34
|
+
const base = $options[ParsedKey] ?? "";
|
|
35
|
+
for (const v of validators) {
|
|
36
|
+
try {
|
|
37
|
+
v($x);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
if (e instanceof ParseError) {
|
|
41
|
+
// rebase each issue path under `base`
|
|
42
|
+
const rebased = (e.issues ?? []).map(i => ({
|
|
43
|
+
...i,
|
|
44
|
+
path: i.path === ""
|
|
45
|
+
? base
|
|
46
|
+
: (base === "" ? i.path : (base + i.path)),
|
|
47
|
+
}));
|
|
48
|
+
throw new ParseError(rebased);
|
|
49
|
+
}
|
|
50
|
+
// non a ParseError - wrap with proper path
|
|
51
|
+
const message = e && typeof e.message === "string"
|
|
52
|
+
? e.message
|
|
53
|
+
: String(e);
|
|
54
|
+
throw new ParseError([{
|
|
55
|
+
input: x,
|
|
56
|
+
message,
|
|
57
|
+
path: base,
|
|
58
|
+
}]);
|
|
59
|
+
}
|
|
26
60
|
}
|
|
27
|
-
|
|
28
|
-
return x;
|
|
61
|
+
return $x;
|
|
29
62
|
}
|
|
30
63
|
}
|
|
31
64
|
//# sourceMappingURL=PrimitiveType.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type Infer from "#Infer";
|
|
2
|
+
import OptionalType from "#OptionalType";
|
|
3
|
+
import Parsed from "#Parsed";
|
|
4
|
+
import type OptionalTrait from "#trait/Optional";
|
|
5
|
+
import type Printable from "@rcompat/type/Printable";
|
|
6
|
+
export default class PureType<Type, Name extends string = "PureType"> extends Parsed<Type> implements Printable, OptionalTrait {
|
|
7
|
+
get name(): string;
|
|
8
|
+
get Name(): Name;
|
|
9
|
+
optional(): OptionalType<this>;
|
|
10
|
+
parse(x: unknown): Infer<this>;
|
|
11
|
+
toJSON(): {
|
|
12
|
+
type: "pure";
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=PureType.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import OptionalType from "#OptionalType";
|
|
2
|
+
import Parsed from "#Parsed";
|
|
3
|
+
export default class PureType extends Parsed {
|
|
4
|
+
get name() {
|
|
5
|
+
return "pure";
|
|
6
|
+
}
|
|
7
|
+
get Name() {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
optional() {
|
|
11
|
+
return new OptionalType(this);
|
|
12
|
+
}
|
|
13
|
+
parse(x) {
|
|
14
|
+
// no parsing of static types
|
|
15
|
+
return x;
|
|
16
|
+
}
|
|
17
|
+
toJSON() {
|
|
18
|
+
return { type: "pure" };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=PureType.js.map
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import GenericType from "#GenericType";
|
|
2
2
|
import type Infer from "#Infer";
|
|
3
3
|
import OptionalType from "#OptionalType";
|
|
4
|
+
import type Parsed from "#Parsed";
|
|
5
|
+
import type ParseOptions from "#ParseOptions";
|
|
4
6
|
import type RecordTypeKey from "#RecordTypeKey";
|
|
5
|
-
import type
|
|
6
|
-
export default class RecordType<Key extends RecordTypeKey, Value extends
|
|
7
|
+
import type OptionalTrait from "#trait/Optional";
|
|
8
|
+
export default class RecordType<Key extends RecordTypeKey, Value extends Parsed<unknown>> extends GenericType<Value, Record<Infer<Key>, Infer<Value>>, "RecordType"> implements OptionalTrait {
|
|
7
9
|
#private;
|
|
8
10
|
constructor(k: Key, v: Value);
|
|
9
11
|
optional(): OptionalType<this>;
|
|
10
|
-
get name():
|
|
11
|
-
|
|
12
|
+
get name(): "record";
|
|
13
|
+
parse(x: unknown, options?: ParseOptions): Infer<this>;
|
|
14
|
+
toJSON(): {
|
|
15
|
+
type: "record";
|
|
16
|
+
key: {
|
|
17
|
+
type: "string";
|
|
18
|
+
datatype: "string";
|
|
19
|
+
} | {
|
|
20
|
+
type: "symbol";
|
|
21
|
+
} | {
|
|
22
|
+
type: "number" | "bigint";
|
|
23
|
+
datatype: "f64";
|
|
24
|
+
};
|
|
25
|
+
value: import("./Serialized.js").default;
|
|
26
|
+
};
|
|
12
27
|
}
|
|
13
28
|
//# sourceMappingURL=RecordType.d.ts.map
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import error from "#error";
|
|
2
|
+
import expected from "#expected";
|
|
1
3
|
import GenericType from "#GenericType";
|
|
2
4
|
import OptionalType from "#OptionalType";
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import ParsedKey from "#ParsedKey";
|
|
6
|
+
import ParseError from "#ParseError";
|
|
7
|
+
import join from "#path/join";
|
|
8
|
+
const nextOptions = (k, options) => {
|
|
9
|
+
const base = options?.[ParsedKey] ?? "";
|
|
10
|
+
return options === undefined
|
|
11
|
+
? { [ParsedKey]: join("", k) }
|
|
12
|
+
: { ...options, [ParsedKey]: join(base, k) };
|
|
8
13
|
};
|
|
9
14
|
const is_numeric = (string) => /^-?\d+(\.\d+)?$/.test(string);
|
|
10
15
|
export default class RecordType extends GenericType {
|
|
@@ -21,38 +26,62 @@ export default class RecordType extends GenericType {
|
|
|
21
26
|
get name() {
|
|
22
27
|
return "record";
|
|
23
28
|
}
|
|
24
|
-
|
|
29
|
+
parse(x, options = {}) {
|
|
25
30
|
if (typeof x !== "object" || x === null) {
|
|
26
|
-
throw new
|
|
31
|
+
throw new ParseError(error("object", x, options));
|
|
27
32
|
}
|
|
28
33
|
const key_name = this.#key.name;
|
|
29
34
|
const keys = Object.keys(x);
|
|
30
35
|
const symbols = Object.getOwnPropertySymbols(x);
|
|
36
|
+
const base = options[ParsedKey] ?? "";
|
|
31
37
|
if (key_name === "string" || key_name === "number") {
|
|
32
38
|
// no key may be a symbol
|
|
33
39
|
if (symbols.length > 0) {
|
|
34
|
-
throw new
|
|
40
|
+
throw new ParseError([{
|
|
41
|
+
input: x,
|
|
42
|
+
message: expected(`${key_name} key`, symbols[0]),
|
|
43
|
+
path: base,
|
|
44
|
+
}]);
|
|
35
45
|
}
|
|
36
46
|
keys.forEach(k => {
|
|
37
47
|
if (key_name === "string" && is_numeric(k)) {
|
|
38
|
-
throw new
|
|
48
|
+
throw new ParseError([{
|
|
49
|
+
input: x,
|
|
50
|
+
message: expected("string key", +k),
|
|
51
|
+
path: join(base, k),
|
|
52
|
+
}]);
|
|
39
53
|
}
|
|
40
54
|
if (key_name === "number" && !is_numeric(k)) {
|
|
41
|
-
throw new
|
|
55
|
+
throw new ParseError([{
|
|
56
|
+
input: x,
|
|
57
|
+
message: expected("number key", k),
|
|
58
|
+
path: join(base, k),
|
|
59
|
+
}]);
|
|
42
60
|
}
|
|
43
|
-
this.#value.
|
|
61
|
+
this.#value.parse(x[k], nextOptions(k, options));
|
|
44
62
|
});
|
|
45
63
|
}
|
|
46
64
|
if (key_name === "symbol") {
|
|
47
|
-
//
|
|
65
|
+
// disallow any non-symbol keys
|
|
48
66
|
if (keys.length > 0) {
|
|
49
|
-
throw new
|
|
67
|
+
throw new ParseError([{
|
|
68
|
+
input: x,
|
|
69
|
+
message: expected("symbol key", keys[0]),
|
|
70
|
+
path: join(base, keys[0]),
|
|
71
|
+
}]);
|
|
50
72
|
}
|
|
51
73
|
symbols.forEach(k => {
|
|
52
|
-
this.#value.
|
|
74
|
+
this.#value.parse(x[k], options);
|
|
53
75
|
});
|
|
54
76
|
}
|
|
55
77
|
return x;
|
|
56
78
|
}
|
|
79
|
+
toJSON() {
|
|
80
|
+
return {
|
|
81
|
+
type: this.name,
|
|
82
|
+
key: this.#key.toJSON(),
|
|
83
|
+
value: this.#value.toJSON(),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
57
86
|
}
|
|
58
87
|
//# sourceMappingURL=RecordType.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type NumberType from "#NumberType";
|
|
2
2
|
import type StringType from "#StringType";
|
|
3
3
|
import type SymbolType from "#SymbolType";
|
|
4
|
-
type KeyType =
|
|
4
|
+
type KeyType = NumberType | StringType | SymbolType;
|
|
5
5
|
export type { KeyType as default };
|
|
6
6
|
//# sourceMappingURL=RecordTypeKey.d.ts.map
|
package/lib/private/Schema.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
type Schema =
|
|
1
|
+
import type Parsed from "#Parsed";
|
|
2
|
+
import type AbstractNewable from "@rcompat/type/AbstractNewable";
|
|
3
|
+
type Schema = Parsed<unknown> | AbstractNewable | null | undefined | string | number | boolean | Schema[] | {
|
|
4
4
|
[k: string]: Schema;
|
|
5
5
|
};
|
|
6
6
|
export type { Schema as default };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type Dict from "@rcompat/type/Dict";
|
|
2
|
+
import type JSONValue from "@rcompat/type/JSONValue";
|
|
3
|
+
type DT<D extends string = string> = {
|
|
4
|
+
datatype?: D;
|
|
5
|
+
};
|
|
6
|
+
type PrimitiveSerialized = {
|
|
7
|
+
type: "string";
|
|
8
|
+
} & DT | {
|
|
9
|
+
type: "boolean";
|
|
10
|
+
} & DT | {
|
|
11
|
+
type: "symbol";
|
|
12
|
+
} & DT | {
|
|
13
|
+
type: "null";
|
|
14
|
+
} & DT | {
|
|
15
|
+
type: "undefined";
|
|
16
|
+
} & DT | {
|
|
17
|
+
type: "date";
|
|
18
|
+
} & DT | {
|
|
19
|
+
type: "blob";
|
|
20
|
+
} & DT | {
|
|
21
|
+
type: "file";
|
|
22
|
+
} & DT | {
|
|
23
|
+
type: "url";
|
|
24
|
+
} & DT;
|
|
25
|
+
type NumberSerialized = {
|
|
26
|
+
type: "number";
|
|
27
|
+
} & DT;
|
|
28
|
+
type BigIntSerialized = {
|
|
29
|
+
type: "bigint";
|
|
30
|
+
} & DT;
|
|
31
|
+
type StructuralSerialized = {
|
|
32
|
+
type: "newable";
|
|
33
|
+
of: string;
|
|
34
|
+
} | {
|
|
35
|
+
type: "literal";
|
|
36
|
+
value: JSONValue;
|
|
37
|
+
} | {
|
|
38
|
+
type: "array";
|
|
39
|
+
of: Serialized;
|
|
40
|
+
} | {
|
|
41
|
+
type: "tuple";
|
|
42
|
+
of: Serialized[];
|
|
43
|
+
} | {
|
|
44
|
+
type: "record";
|
|
45
|
+
key: Serialized;
|
|
46
|
+
value: Serialized;
|
|
47
|
+
} | {
|
|
48
|
+
type: "object";
|
|
49
|
+
properties: Dict<Serialized>;
|
|
50
|
+
} | {
|
|
51
|
+
type: "union";
|
|
52
|
+
of: Serialized[];
|
|
53
|
+
} | {
|
|
54
|
+
type: "optional";
|
|
55
|
+
of: Serialized;
|
|
56
|
+
} | {
|
|
57
|
+
type: "default";
|
|
58
|
+
of: Serialized;
|
|
59
|
+
} | {
|
|
60
|
+
type: "partial";
|
|
61
|
+
of: Serialized;
|
|
62
|
+
} | {
|
|
63
|
+
type: "schema";
|
|
64
|
+
of: Serialized;
|
|
65
|
+
} | {
|
|
66
|
+
type: "pure";
|
|
67
|
+
} | {
|
|
68
|
+
type: "primary";
|
|
69
|
+
};
|
|
70
|
+
type Serialized = PrimitiveSerialized | NumberSerialized | BigIntSerialized | StructuralSerialized;
|
|
71
|
+
export type { Serialized as default };
|
|
72
|
+
//# sourceMappingURL=Serialized.d.ts.map
|