@nmtjs/type 0.4.8 → 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/dist/compiler.js +84 -38
- package/dist/compiler.js.map +1 -1
- package/dist/index.js +53 -22
- package/dist/index.js.map +1 -1
- package/dist/schemas/discriminated-union.js +9 -0
- package/dist/schemas/discriminated-union.js.map +1 -0
- package/dist/schemas/nullable.js +1 -6
- package/dist/schemas/nullable.js.map +1 -1
- package/dist/temporal.js +7 -7
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +3 -43
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +17 -63
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +78 -41
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +3 -43
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +8 -48
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +8 -0
- package/dist/types/date.js.map +1 -0
- package/dist/types/enum.js +10 -94
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +3 -43
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +3 -26
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +52 -186
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +10 -131
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +25 -65
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +23 -328
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +16 -90
- package/dist/types/union.js.map +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/compiler.ts +124 -41
- package/src/index.ts +145 -63
- package/src/schemas/discriminated-union.ts +49 -0
- package/src/schemas/nullable.ts +7 -13
- package/src/temporal.ts +8 -7
- package/src/types/any.ts +6 -46
- package/src/types/array.ts +38 -86
- package/src/types/base.ts +205 -81
- package/src/types/boolean.ts +13 -47
- package/src/types/custom.ts +21 -79
- package/src/types/date.ts +10 -0
- package/src/types/enum.ts +18 -107
- package/src/types/literal.ts +7 -63
- package/src/types/never.ts +6 -36
- package/src/types/number.ts +52 -188
- package/src/types/object.ts +61 -202
- package/src/types/string.ts +25 -61
- package/src/types/temporal.ts +53 -410
- package/src/types/union.ts +98 -138
- package/src/utils.ts +8 -0
- package/dist/constants.js +0 -2
- package/dist/constants.js.map +0 -1
- package/dist/types/datetime.js +0 -53
- package/dist/types/datetime.js.map +0 -1
- package/src/constants.ts +0 -5
- package/src/types/datetime.ts +0 -65
package/dist/compiler.js
CHANGED
|
@@ -1,72 +1,118 @@
|
|
|
1
1
|
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
2
|
-
import { Value } from '@sinclair/typebox/value';
|
|
3
|
-
import {
|
|
2
|
+
import { TransformDecode, TransformEncode, Value } from '@sinclair/typebox/value';
|
|
3
|
+
import { IsDiscriminatedUnion } from "./schemas/discriminated-union.js";
|
|
4
4
|
function _parse(schema, value) {
|
|
5
5
|
return Value.Convert(schema, Value.Default(schema, Value.Clean(schema, Value.Clone(value))));
|
|
6
6
|
}
|
|
7
|
+
function _errors(errors) {
|
|
8
|
+
const result = [];
|
|
9
|
+
for (const error of errors){
|
|
10
|
+
if (IsDiscriminatedUnion(error.schema)) {
|
|
11
|
+
const discriminator = error.schema.discriminator;
|
|
12
|
+
const discriminatorValue = error.value?.[discriminator];
|
|
13
|
+
if (discriminatorValue !== undefined) {
|
|
14
|
+
const variantSchema = error.schema.anyOf.find((schema)=>schema.properties[discriminator].const === discriminatorValue);
|
|
15
|
+
if (variantSchema) {
|
|
16
|
+
const propertiesSchemas = [];
|
|
17
|
+
for(const element in variantSchema.properties){
|
|
18
|
+
const propertySchema = variantSchema.properties[element];
|
|
19
|
+
if (propertySchema !== variantSchema.properties[discriminator]) {
|
|
20
|
+
propertiesSchemas.push(propertySchema);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
for (const iter of error.errors){
|
|
24
|
+
for (const err of iter){
|
|
25
|
+
if (!propertiesSchemas.includes(err.schema)) continue;
|
|
26
|
+
result.push({
|
|
27
|
+
path: err.path,
|
|
28
|
+
message: err.message,
|
|
29
|
+
value: err.value
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
result.push({
|
|
38
|
+
path: error.path,
|
|
39
|
+
message: error.message,
|
|
40
|
+
value: error.value
|
|
41
|
+
});
|
|
42
|
+
for (const nestedError of error.errors){
|
|
43
|
+
result.push(..._errors(nestedError));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
7
48
|
function compileType(type) {
|
|
8
|
-
const schema =
|
|
49
|
+
const { final: schema } = type;
|
|
9
50
|
const compiled = TypeCompiler.Compile(schema);
|
|
51
|
+
const errors = (value)=>{
|
|
52
|
+
return _errors(compiled.Errors(value));
|
|
53
|
+
};
|
|
10
54
|
return {
|
|
11
55
|
check: compiled.Check.bind(compiled),
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
56
|
+
parse: _parse.bind(null, schema),
|
|
57
|
+
errors,
|
|
58
|
+
decode: TransformDecode.bind(null, schema, compiled.References()),
|
|
59
|
+
encode: TransformEncode.bind(null, schema, compiled.References())
|
|
16
60
|
};
|
|
17
61
|
}
|
|
18
62
|
export function compile(schema) {
|
|
19
63
|
const compiled = compileType(schema);
|
|
64
|
+
function decodeSafe(val) {
|
|
65
|
+
try {
|
|
66
|
+
return {
|
|
67
|
+
success: true,
|
|
68
|
+
value: compiled.decode(val)
|
|
69
|
+
};
|
|
70
|
+
} catch (error) {
|
|
71
|
+
return {
|
|
72
|
+
success: false,
|
|
73
|
+
error
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function encodeSafe(val) {
|
|
78
|
+
try {
|
|
79
|
+
return {
|
|
80
|
+
success: true,
|
|
81
|
+
value: compiled.encode(val)
|
|
82
|
+
};
|
|
83
|
+
} catch (error) {
|
|
84
|
+
return {
|
|
85
|
+
success: false,
|
|
86
|
+
error
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
20
90
|
return {
|
|
21
91
|
...compiled,
|
|
22
|
-
decodeSafe
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
success: true,
|
|
26
|
-
value: compiled.decode(val)
|
|
27
|
-
};
|
|
28
|
-
} catch (error) {
|
|
29
|
-
return {
|
|
30
|
-
success: false,
|
|
31
|
-
error
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
encodeSafe: (val)=>{
|
|
36
|
-
try {
|
|
37
|
-
return {
|
|
38
|
-
success: true,
|
|
39
|
-
value: compiled.encode(val)
|
|
40
|
-
};
|
|
41
|
-
} catch (error) {
|
|
42
|
-
return {
|
|
43
|
-
success: false,
|
|
44
|
-
error
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
}
|
|
92
|
+
decodeSafe,
|
|
93
|
+
encodeSafe
|
|
48
94
|
};
|
|
49
95
|
}
|
|
50
96
|
export var runtime;
|
|
51
97
|
(function(runtime) {
|
|
52
98
|
function parse(type, value) {
|
|
53
|
-
return _parse(
|
|
99
|
+
return _parse(type.schema, value);
|
|
54
100
|
}
|
|
55
101
|
runtime.parse = parse;
|
|
56
102
|
function errors(type, value) {
|
|
57
|
-
return Value.Errors(
|
|
103
|
+
return _errors(Value.Errors(type.schema, value));
|
|
58
104
|
}
|
|
59
105
|
runtime.errors = errors;
|
|
60
106
|
function check(type, value) {
|
|
61
|
-
return Value.Check(
|
|
107
|
+
return Value.Check(type.schema, value);
|
|
62
108
|
}
|
|
63
109
|
runtime.check = check;
|
|
64
110
|
function decode(type, value) {
|
|
65
|
-
return
|
|
111
|
+
return TransformDecode(type.schema, [], value);
|
|
66
112
|
}
|
|
67
113
|
runtime.decode = decode;
|
|
68
114
|
function encode(type, value) {
|
|
69
|
-
return
|
|
115
|
+
return TransformEncode(type.schema, [], value);
|
|
70
116
|
}
|
|
71
117
|
runtime.encode = encode;
|
|
72
118
|
})(runtime || (runtime = {}));
|
package/dist/compiler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import type { TSchema } from '@sinclair/typebox'\nimport {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import type { TSchema } from '@sinclair/typebox'\nimport {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport {\n TransformDecode,\n TransformEncode,\n Value,\n} from '@sinclair/typebox/value'\nimport type { t } from './index.ts'\nimport { IsDiscriminatedUnion } from './schemas/discriminated-union.ts'\nimport type { BaseType } from './types/base.ts'\n\ntype ValidationError = {\n path: string\n message: string\n value: unknown\n}\n\nexport type Compiled<T extends BaseType = BaseType> = {\n check: (val: unknown) => boolean\n errors: (val: unknown) => ValidationError[]\n parse: (val: unknown) => unknown\n /**\n * Requires to `check` before calling\n */\n decode: (val: unknown) => t.infer.decoded<T>\n /**\n * Requires to `check` before calling\n */\n encode: (val: unknown) => t.infer.encoded<T>\n /**\n * Requires to `check` before calling\n */\n decodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: t.infer.decoded<T> }\n | { success: false; error: any }\n /**\n * Requires to `check` before calling\n */\n encodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: t.infer.encoded<T> }\n | { success: false; error: any }\n}\n\n// FIXME: this one is very slow\nfunction _parse(schema: TSchema, value: any) {\n // Clone -> Clean -> Default -> Convert\n return Value.Convert(\n schema,\n Value.Default(schema, Value.Clean(schema, Value.Clone(value))),\n )\n}\n\nfunction _errors(errors: ValueErrorIterator) {\n const result: ValidationError[] = []\n\n for (const error of errors) {\n if (IsDiscriminatedUnion(error.schema)) {\n const discriminator = error.schema.discriminator\n const discriminatorValue = error.value?.[discriminator]\n if (discriminatorValue !== undefined) {\n const variantSchema = error.schema.anyOf.find(\n (schema) =>\n schema.properties[discriminator].const === discriminatorValue,\n )\n if (variantSchema) {\n const propertiesSchemas: TSchema[] = []\n for (const element in variantSchema.properties) {\n const propertySchema = variantSchema.properties[element]\n if (propertySchema !== variantSchema.properties[discriminator]) {\n propertiesSchemas.push(propertySchema)\n }\n }\n\n for (const iter of error.errors) {\n for (const err of iter) {\n if (!propertiesSchemas.includes(err.schema)) continue\n result.push({\n path: err.path,\n message: err.message,\n value: err.value,\n })\n }\n }\n\n continue\n }\n }\n }\n\n result.push({\n path: error.path,\n message: error.message,\n value: error.value,\n })\n\n for (const nestedError of error.errors) {\n result.push(..._errors(nestedError))\n }\n }\n\n return result\n}\n\nfunction compileType(type: BaseType) {\n const { final: schema } = type\n const compiled = TypeCompiler.Compile(schema)\n const errors = (value) => {\n return _errors(compiled.Errors(value))\n }\n\n return {\n check: compiled.Check.bind(compiled),\n parse: _parse.bind(null, schema),\n errors,\n decode: TransformDecode.bind(null, schema, compiled.References()),\n encode: TransformEncode.bind(null, schema, compiled.References()),\n }\n}\n\nexport function compile<T extends BaseType>(schema: T): Compiled<T> {\n const compiled = compileType(schema)\n\n function decodeSafe(val: unknown) {\n try {\n return {\n success: true as const,\n value: compiled.decode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n }\n\n function encodeSafe(val: unknown) {\n try {\n return {\n success: true as const,\n value: compiled.encode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n }\n\n return {\n ...compiled,\n decodeSafe,\n encodeSafe,\n } as any\n}\n\nexport namespace runtime {\n export function parse(type: BaseType, value: any) {\n return _parse(type.schema, value)\n }\n\n export function errors(type: BaseType, value: any): ValidationError[] {\n return _errors(Value.Errors(type.schema, value))\n }\n\n export function check(type: BaseType, value: any): boolean {\n return Value.Check(type.schema, value)\n }\n\n export function decode<T extends BaseType>(\n type: T,\n value: any,\n ): t.infer.decoded<T> {\n return TransformDecode(type.schema, [], value)\n }\n\n export function encode<T extends BaseType>(\n type: T,\n value: any,\n ): t.infer.encoded<T> {\n return TransformEncode(type.schema, [], value)\n }\n}\n"],"names":["TypeCompiler","TransformDecode","TransformEncode","Value","IsDiscriminatedUnion","_parse","schema","value","Convert","Default","Clean","Clone","_errors","errors","result","error","discriminator","discriminatorValue","undefined","variantSchema","anyOf","find","properties","const","propertiesSchemas","element","propertySchema","push","iter","err","includes","path","message","nestedError","compileType","type","final","compiled","Compile","Errors","check","Check","bind","parse","decode","References","encode","compile","decodeSafe","val","success","encodeSafe","runtime"],"mappings":"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"}
|
package/dist/index.js
CHANGED
|
@@ -1,37 +1,68 @@
|
|
|
1
|
+
import { AnyType } from "./types/any.js";
|
|
1
2
|
import { ArrayType } from "./types/array.js";
|
|
2
3
|
import { BooleanType } from "./types/boolean.js";
|
|
3
4
|
import { CustomType } from "./types/custom.js";
|
|
4
|
-
import { DateType } from "./types/
|
|
5
|
+
import { DateType } from "./types/date.js";
|
|
5
6
|
import { EnumType, ObjectEnumType } from "./types/enum.js";
|
|
6
7
|
import { LiteralType } from "./types/literal.js";
|
|
8
|
+
import { NeverType } from "./types/never.js";
|
|
7
9
|
import { BigIntType, IntegerType, NumberType } from "./types/number.js";
|
|
8
10
|
import { ObjectType, RecordType } from "./types/object.js";
|
|
9
11
|
import { StringType } from "./types/string.js";
|
|
10
|
-
import { IntersactionType, UnionType } from "./types/union.js";
|
|
11
|
-
import { AnyType } from "./types/any.js";
|
|
12
|
-
import { NeverType } from "./types/never.js";
|
|
12
|
+
import { DiscriminatedUnionType, IntersactionType, UnionType } from "./types/union.js";
|
|
13
13
|
import { register } from "./formats.js";
|
|
14
14
|
register();
|
|
15
15
|
export * from "./schemas/nullable.js";
|
|
16
|
-
export { BaseType
|
|
16
|
+
export { BaseType } from "./types/base.js";
|
|
17
17
|
export { ArrayType, BooleanType, CustomType, DateType, EnumType, LiteralType, IntegerType, NumberType, ObjectType, StringType, IntersactionType, UnionType, AnyType, NeverType, };
|
|
18
18
|
export var t;
|
|
19
19
|
(function(t) {
|
|
20
|
-
t.never =
|
|
21
|
-
t.boolean =
|
|
22
|
-
t.string =
|
|
23
|
-
t.number =
|
|
24
|
-
t.integer =
|
|
25
|
-
t.bitint =
|
|
26
|
-
t.literal =
|
|
27
|
-
t.objectEnum =
|
|
28
|
-
t.arrayEnum =
|
|
29
|
-
t.date =
|
|
30
|
-
t.array =
|
|
31
|
-
t.object =
|
|
32
|
-
t.record =
|
|
33
|
-
t.any =
|
|
34
|
-
t.or =
|
|
35
|
-
t.and =
|
|
36
|
-
t.
|
|
20
|
+
t.never = NeverType.factory;
|
|
21
|
+
t.boolean = BooleanType.factory;
|
|
22
|
+
t.string = StringType.factory;
|
|
23
|
+
t.number = NumberType.factory;
|
|
24
|
+
t.integer = IntegerType.factory;
|
|
25
|
+
t.bitint = BigIntType.factory;
|
|
26
|
+
t.literal = LiteralType.factory;
|
|
27
|
+
t.objectEnum = ObjectEnumType.factory;
|
|
28
|
+
t.arrayEnum = EnumType.factory;
|
|
29
|
+
t.date = DateType.factory;
|
|
30
|
+
t.array = ArrayType.factory;
|
|
31
|
+
t.object = ObjectType.factory;
|
|
32
|
+
t.record = RecordType.factory;
|
|
33
|
+
t.any = AnyType.factory;
|
|
34
|
+
t.or = UnionType.factory;
|
|
35
|
+
t.and = IntersactionType.factory;
|
|
36
|
+
t.discriminatedUnion = DiscriminatedUnionType.factory;
|
|
37
|
+
t.custom = CustomType.factory;
|
|
38
|
+
t.keyof = (type)=>{
|
|
39
|
+
return t.arrayEnum(Object.keys(type.props.properties));
|
|
40
|
+
};
|
|
41
|
+
t.pick = (source, pick)=>{
|
|
42
|
+
const properties = Object.fromEntries(Object.entries(source.props.properties).filter(([key])=>pick[key]));
|
|
43
|
+
return ObjectType.factory(properties);
|
|
44
|
+
};
|
|
45
|
+
t.omit = (source, omit)=>{
|
|
46
|
+
const properties = Object.fromEntries(Object.entries(source.props.properties).filter(([key])=>!omit[key]));
|
|
47
|
+
return ObjectType.factory(properties);
|
|
48
|
+
};
|
|
49
|
+
t.extend = (object1, properties)=>{
|
|
50
|
+
return ObjectType.factory({
|
|
51
|
+
...object1.props.properties,
|
|
52
|
+
...properties
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
t.merge = (object1, object2)=>{
|
|
56
|
+
return ObjectType.factory({
|
|
57
|
+
...object1.props.properties,
|
|
58
|
+
...object2.props.properties
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
t.partial = (object)=>{
|
|
62
|
+
const properties = {};
|
|
63
|
+
for (const [key, value] of Object.entries(object.props.properties)){
|
|
64
|
+
properties[key] = value.optional();
|
|
65
|
+
}
|
|
66
|
+
return ObjectType.factory(properties, {});
|
|
67
|
+
};
|
|
37
68
|
})(t || (t = {}));
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type {\n StaticDecode,\n StaticEncode,\n TLiteralValue,\n} from '@sinclair/typebox/type'\nimport { AnyType } from './types/any.ts'\nimport { ArrayType } from './types/array.ts'\nimport type {\n BaseType,\n BaseTypeAny,\n OptionalType,\n Static,\n} from './types/base.ts'\nimport { BooleanType } from './types/boolean.ts'\nimport { CustomType } from './types/custom.ts'\nimport { DateType } from './types/date.ts'\nimport { EnumType, ObjectEnumType } from './types/enum.ts'\nimport { LiteralType } from './types/literal.ts'\nimport { NeverType } from './types/never.ts'\nimport { BigIntType, IntegerType, NumberType } from './types/number.ts'\nimport { ObjectType, type ObjectTypeProps, RecordType } from './types/object.ts'\nimport { StringType } from './types/string.ts'\nimport {\n DiscriminatedUnionType,\n IntersactionType,\n UnionType,\n} from './types/union.ts'\nimport type { UnionToTupleString } from './utils.ts'\n\n// register ajv formats\nimport { register } from './formats.ts'\nregister()\n\nexport * from './schemas/nullable.ts'\nexport { BaseType, type BaseTypeAny } from './types/base.ts'\nexport { type TSchema } from '@sinclair/typebox'\nexport {\n ArrayType,\n BooleanType,\n CustomType,\n DateType,\n EnumType,\n LiteralType,\n IntegerType,\n NumberType,\n ObjectType,\n StringType,\n IntersactionType,\n UnionType,\n AnyType,\n NeverType,\n}\n\nexport namespace t {\n export namespace infer {\n export type decoded<T extends BaseTypeAny> = StaticDecode<\n T['_']['decoded']['output']\n >\n export type encoded<T extends BaseTypeAny> = StaticEncode<\n T['_']['encoded']['output']\n >\n export namespace input {\n export type decoded<T extends BaseTypeAny> = StaticDecode<\n T['_']['decoded']['input']\n >\n export type encoded<T extends BaseTypeAny> = StaticEncode<\n T['_']['encoded']['input']\n >\n }\n }\n\n export const never = NeverType.factory\n export const boolean = BooleanType.factory\n export const string = StringType.factory\n export const number = NumberType.factory\n export const integer = IntegerType.factory\n export const bitint = BigIntType.factory\n export const literal = LiteralType.factory\n export const objectEnum = ObjectEnumType.factory\n export const arrayEnum = EnumType.factory\n export const date = DateType.factory\n export const array = ArrayType.factory\n export const object = ObjectType.factory\n export const record = RecordType.factory\n export const any = AnyType.factory\n export const or = UnionType.factory\n export const and = IntersactionType.factory\n export const discriminatedUnion = DiscriminatedUnionType.factory\n export const custom = CustomType.factory\n\n export const keyof = <T extends ObjectType>(\n type: T,\n ): EnumType<\n UnionToTupleString<T extends ObjectType<infer Props> ? keyof Props : never>\n > => {\n return arrayEnum(Object.keys(type.props.properties) as any)\n }\n\n export const pick = <\n T extends ObjectType,\n P extends { [K in keyof T['props']['properties']]?: true },\n >(\n source: T,\n pick: P,\n ): ObjectType<{\n [K in keyof T['props']['properties'] as K extends keyof P\n ? K\n : never]: T['props']['properties'][K]\n }> => {\n const properties = Object.fromEntries(\n Object.entries(source.props.properties).filter(([key]) => pick[key]),\n )\n return ObjectType.factory(properties) as any\n }\n\n export const omit = <\n T extends ObjectType,\n P extends { [K in keyof T]?: true },\n >(\n source: T,\n omit: P,\n ): ObjectType<{\n [K in keyof T['props']['properties'] as K extends keyof P\n ? never\n : K]: T['props']['properties'][K]\n }> => {\n const properties = Object.fromEntries(\n Object.entries(source.props.properties).filter(([key]) => !omit[key]),\n )\n return ObjectType.factory(properties) as any\n }\n\n export const extend = <T extends ObjectType, P extends ObjectTypeProps>(\n object1: T,\n properties: P,\n ): ObjectType<{\n [K in keyof T['props']['properties'] | keyof P]: K extends keyof P\n ? P[K]\n : K extends keyof T['props']['properties']\n ? T['props']['properties'][K]\n : never\n }> => {\n return ObjectType.factory({\n ...object1.props.properties,\n ...properties,\n }) as any\n }\n\n export const merge = <T1 extends ObjectType, T2 extends ObjectType>(\n object1: T1,\n object2: T2,\n ): ObjectType<{\n [K in\n | keyof T1['props']['properties']\n | keyof T2['props']['properties']]: K extends keyof T2['props']['properties']\n ? T2['props']['properties'][K]\n : K extends keyof T1['props']['properties']\n ? T1['props']['properties'][K]\n : never\n }> => {\n return ObjectType.factory({\n ...object1.props.properties,\n ...object2.props.properties,\n }) as any\n }\n\n export const partial = <T extends ObjectType>(\n object: T,\n ): ObjectType<{\n [K in keyof T['props']['properties']]: OptionalType<\n T['props']['properties'][K]\n >\n }> => {\n const properties = {} as any\n\n for (const [key, value] of Object.entries(object.props.properties)) {\n properties[key] = value.optional()\n }\n\n return ObjectType.factory(properties, {}) as any\n }\n}\n"],"names":["AnyType","ArrayType","BooleanType","CustomType","DateType","EnumType","ObjectEnumType","LiteralType","NeverType","BigIntType","IntegerType","NumberType","ObjectType","RecordType","StringType","DiscriminatedUnionType","IntersactionType","UnionType","register","BaseType","t","never","factory","boolean","string","number","integer","bitint","literal","objectEnum","arrayEnum","date","array","object","record","any","or","and","discriminatedUnion","custom","keyof","type","Object","keys","props","properties","pick","source","fromEntries","entries","filter","key","omit","extend","object1","merge","object2","partial","value","optional"],"mappings":"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"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { KindGuard, Type, TypeGuard, ValueGuard } from '@sinclair/typebox';
|
|
2
|
+
export function IsDiscriminatedUnion(schema) {
|
|
3
|
+
return TypeGuard.IsUnion(schema) && 'discriminator' in schema && ValueGuard.IsString(schema.discriminator) && schema.anyOf.every((variant)=>KindGuard.IsObject(variant) && KindGuard.IsLiteralString(variant.properties[schema.discriminator]));
|
|
4
|
+
}
|
|
5
|
+
export function DiscriminatedUnion(key, types) {
|
|
6
|
+
return Type.Union(types, {
|
|
7
|
+
discriminator: key
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/schemas/discriminated-union.ts"],"sourcesContent":["import {\n KindGuard,\n type TLiteral,\n type TObject,\n type TPropertyKey,\n type TSchema,\n type TUnion,\n Type,\n TypeGuard,\n ValueGuard,\n} from '@sinclair/typebox'\n\nexport function IsDiscriminatedUnion(\n schema: TSchema,\n): schema is TDiscriminatedUnion {\n return (\n TypeGuard.IsUnion(schema) &&\n 'discriminator' in schema &&\n ValueGuard.IsString(schema.discriminator) &&\n schema.anyOf.every(\n (variant) =>\n KindGuard.IsObject(variant) &&\n KindGuard.IsLiteralString(variant.properties[schema.discriminator]),\n )\n )\n}\n\ntype DiscriminatedUnionProperties<K extends string = string> = {\n [OK in K]: TLiteral<any>\n} & {\n [OK in TPropertyKey]: any\n}\n\nexport interface TDiscriminatedUnion<\n K extends string = string,\n T extends TObject<DiscriminatedUnionProperties<K>>[] = TObject<\n DiscriminatedUnionProperties<K>\n >[],\n> extends TUnion<T> {\n discriminator: K\n anyOf: T\n}\n\nexport function DiscriminatedUnion<\n K extends string,\n T extends TObject<DiscriminatedUnionProperties<K>>[],\n>(key: K, types: T): TDiscriminatedUnion<K, T> {\n return Type.Union(types, { discriminator: key }) as any\n}\n"],"names":["KindGuard","Type","TypeGuard","ValueGuard","IsDiscriminatedUnion","schema","IsUnion","IsString","discriminator","anyOf","every","variant","IsObject","IsLiteralString","properties","DiscriminatedUnion","key","types","Union"],"mappings":"AAAA,SACEA,SAAS,EAMTC,IAAI,EACJC,SAAS,EACTC,UAAU,QACL,oBAAmB;AAE1B,OAAO,SAASC,qBACdC,MAAe;IAEf,OACEH,UAAUI,OAAO,CAACD,WAClB,mBAAmBA,UACnBF,WAAWI,QAAQ,CAACF,OAAOG,aAAa,KACxCH,OAAOI,KAAK,CAACC,KAAK,CAChB,CAACC,UACCX,UAAUY,QAAQ,CAACD,YACnBX,UAAUa,eAAe,CAACF,QAAQG,UAAU,CAACT,OAAOG,aAAa,CAAC;AAG1E;AAkBA,OAAO,SAASO,mBAGdC,GAAM,EAAEC,KAAQ;IAChB,OAAOhB,KAAKiB,KAAK,CAACD,OAAO;QAAET,eAAeQ;IAAI;AAChD"}
|
package/dist/schemas/nullable.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox/type';
|
|
2
2
|
export const Nullable = (schema, options = {})=>{
|
|
3
|
-
const { default: _default
|
|
3
|
+
const { default: _default } = schema;
|
|
4
4
|
return Type.Union([
|
|
5
5
|
schema,
|
|
6
6
|
Type.Null()
|
|
7
7
|
], {
|
|
8
8
|
default: _default,
|
|
9
|
-
description,
|
|
10
|
-
examples,
|
|
11
|
-
readOnly,
|
|
12
|
-
title,
|
|
13
|
-
writeOnly,
|
|
14
9
|
...options
|
|
15
10
|
});
|
|
16
11
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/nullable.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TNull,\n type TSchema,\n type TUnion,\n Type,\n} from '@sinclair/typebox/type'\n\nexport type TNullable<T extends TSchema> = TUnion<[T, TNull]>\nexport const Nullable = <T extends TSchema>(\n schema: T,\n options: SchemaOptions = {},\n) => {\n const {
|
|
1
|
+
{"version":3,"sources":["../../../src/schemas/nullable.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TNull,\n type TOptional,\n type TSchema,\n type TUndefined,\n type TUnion,\n Type,\n} from '@sinclair/typebox/type'\n\nexport type TNullable<T extends TSchema> = TUnion<[T, TNull]>\nexport const Nullable = <T extends TSchema>(\n schema: T,\n options: SchemaOptions = {},\n) => {\n const { default: _default } = schema\n\n return Type.Union([schema, Type.Null()], {\n default: _default,\n ...options,\n })\n}\n\nexport type TOptionalUndefined<T extends TSchema> = TOptional<\n TUnion<[T, TUndefined]>\n>\n"],"names":["Type","Nullable","schema","options","default","_default","Union","Null"],"mappings":"AAAA,SAOEA,IAAI,QACC,yBAAwB;AAG/B,OAAO,MAAMC,WAAW,CACtBC,QACAC,UAAyB,CAAC,CAAC;IAE3B,MAAM,EAAEC,SAASC,QAAQ,EAAE,GAAGH;IAE9B,OAAOF,KAAKM,KAAK,CAAC;QAACJ;QAAQF,KAAKO,IAAI;KAAG,EAAE;QACvCH,SAASC;QACT,GAAGF,OAAO;IACZ;AACF,EAAC"}
|
package/dist/temporal.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { DurationType, PlainDateTimeType, PlainDateType, PlainMonthDayType, PlainTimeType, PlainYearMonthType, ZonedDateTimeType } from "./types/temporal.js";
|
|
2
|
-
export const plainDate =
|
|
3
|
-
export const plainDatetime =
|
|
4
|
-
export const plainTime =
|
|
5
|
-
export const zonedDatetime =
|
|
6
|
-
export const duration =
|
|
7
|
-
export const plainYearMonth =
|
|
8
|
-
export const plainMonthDay =
|
|
2
|
+
export const plainDate = PlainDateType.factory;
|
|
3
|
+
export const plainDatetime = PlainDateTimeType.factory;
|
|
4
|
+
export const plainTime = PlainTimeType.factory;
|
|
5
|
+
export const zonedDatetime = ZonedDateTimeType.factory;
|
|
6
|
+
export const duration = DurationType.factory;
|
|
7
|
+
export const plainYearMonth = PlainYearMonthType.factory;
|
|
8
|
+
export const plainMonthDay = PlainMonthDayType.factory;
|
|
9
9
|
export { DurationType, PlainDateTimeType, PlainDateType, PlainMonthDayType, PlainTimeType, PlainYearMonthType, ZonedDateTimeType, };
|
package/dist/temporal.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/temporal.ts"],"sourcesContent":["import {\n DurationType,\n PlainDateTimeType,\n PlainDateType,\n PlainMonthDayType,\n PlainTimeType,\n PlainYearMonthType,\n ZonedDateTimeType,\n} from './types/temporal.ts'\n\nexport const plainDate =
|
|
1
|
+
{"version":3,"sources":["../../src/temporal.ts"],"sourcesContent":["import { Temporal } from 'temporal-polyfill'\nimport {\n DurationType,\n PlainDateTimeType,\n PlainDateType,\n PlainMonthDayType,\n PlainTimeType,\n PlainYearMonthType,\n ZonedDateTimeType,\n} from './types/temporal.ts'\n\nexport const plainDate = PlainDateType.factory\nexport const plainDatetime = PlainDateTimeType.factory\nexport const plainTime = PlainTimeType.factory\nexport const zonedDatetime = ZonedDateTimeType.factory\nexport const duration = DurationType.factory\nexport const plainYearMonth = PlainYearMonthType.factory\nexport const plainMonthDay = PlainMonthDayType.factory\n\nexport {\n DurationType,\n PlainDateTimeType,\n PlainDateType,\n PlainMonthDayType,\n PlainTimeType,\n PlainYearMonthType,\n ZonedDateTimeType,\n}\n"],"names":["DurationType","PlainDateTimeType","PlainDateType","PlainMonthDayType","PlainTimeType","PlainYearMonthType","ZonedDateTimeType","plainDate","factory","plainDatetime","plainTime","zonedDatetime","duration","plainYearMonth","plainMonthDay"],"mappings":"AACA,SACEA,YAAY,EACZC,iBAAiB,EACjBC,aAAa,EACbC,iBAAiB,EACjBC,aAAa,EACbC,kBAAkB,EAClBC,iBAAiB,QACZ,sBAAqB;AAE5B,OAAO,MAAMC,YAAYL,cAAcM,OAAO,CAAA;AAC9C,OAAO,MAAMC,gBAAgBR,kBAAkBO,OAAO,CAAA;AACtD,OAAO,MAAME,YAAYN,cAAcI,OAAO,CAAA;AAC9C,OAAO,MAAMG,gBAAgBL,kBAAkBE,OAAO,CAAA;AACtD,OAAO,MAAMI,WAAWZ,aAAaQ,OAAO,CAAA;AAC5C,OAAO,MAAMK,iBAAiBR,mBAAmBG,OAAO,CAAA;AACxD,OAAO,MAAMM,gBAAgBX,kBAAkBK,OAAO,CAAA;AAEtD,SACER,YAAY,EACZC,iBAAiB,EACjBC,aAAa,EACbC,iBAAiB,EACjBC,aAAa,EACbC,kBAAkB,EAClBC,iBAAiB,KAClB"}
|
package/dist/types/any.js
CHANGED
|
@@ -1,48 +1,8 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
3
|
export class AnyType extends BaseType {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
_constructSchema(options) {
|
|
8
|
-
return Type.Any(options);
|
|
9
|
-
}
|
|
10
|
-
nullable() {
|
|
11
|
-
return new AnyType(...this._with({
|
|
12
|
-
isNullable: true
|
|
13
|
-
}));
|
|
14
|
-
}
|
|
15
|
-
optional() {
|
|
16
|
-
return new AnyType(...this._with({
|
|
17
|
-
isOptional: true
|
|
18
|
-
}));
|
|
19
|
-
}
|
|
20
|
-
nullish() {
|
|
21
|
-
return new AnyType(...this._with({
|
|
22
|
-
isNullable: true,
|
|
23
|
-
isOptional: true
|
|
24
|
-
}));
|
|
25
|
-
}
|
|
26
|
-
default(value) {
|
|
27
|
-
return new AnyType(...this._with({
|
|
28
|
-
options: {
|
|
29
|
-
default: value
|
|
30
|
-
},
|
|
31
|
-
hasDefault: true
|
|
32
|
-
}));
|
|
33
|
-
}
|
|
34
|
-
description(description) {
|
|
35
|
-
return new AnyType(...this._with({
|
|
36
|
-
options: {
|
|
37
|
-
description
|
|
38
|
-
}
|
|
39
|
-
}));
|
|
40
|
-
}
|
|
41
|
-
examples(...examples) {
|
|
42
|
-
return new AnyType(...this._with({
|
|
43
|
-
options: {
|
|
44
|
-
examples
|
|
45
|
-
}
|
|
46
|
-
}));
|
|
4
|
+
_;
|
|
5
|
+
static factory() {
|
|
6
|
+
return new AnyType(Type.Any());
|
|
47
7
|
}
|
|
48
8
|
}
|
package/dist/types/any.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/any.ts"],"sourcesContent":["import { type
|
|
1
|
+
{"version":3,"sources":["../../../src/types/any.ts"],"sourcesContent":["import { type TAny, Type } from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport class AnyType extends BaseType<TAny> {\n _!: ConstantType<this['schema']>\n\n static factory() {\n return new AnyType(Type.Any())\n }\n}\n"],"names":["Type","BaseType","AnyType","_","factory","Any"],"mappings":"AAAA,SAAoBA,IAAI,QAAQ,oBAAmB;AACnD,SAASC,QAAQ,QAA2B,YAAW;AAEvD,OAAO,MAAMC,gBAAgBD;IAC3BE,EAAgC;IAEhC,OAAOC,UAAU;QACf,OAAO,IAAIF,QAAQF,KAAKK,GAAG;IAC7B;AACF"}
|
package/dist/types/array.js
CHANGED
|
@@ -1,73 +1,27 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
|
-
import { BaseType
|
|
2
|
+
import { BaseType } from "./base.js";
|
|
3
3
|
export class ArrayType extends BaseType {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.element = element;
|
|
8
|
-
}
|
|
9
|
-
_constructSchema(options, element) {
|
|
10
|
-
return Type.Array(getTypeSchema(element), options);
|
|
11
|
-
}
|
|
12
|
-
nullable() {
|
|
13
|
-
return new ArrayType(this.element, ...this._with({
|
|
14
|
-
isNullable: true
|
|
15
|
-
}));
|
|
16
|
-
}
|
|
17
|
-
optional() {
|
|
18
|
-
return new ArrayType(this.element, ...this._with({
|
|
19
|
-
isOptional: true
|
|
20
|
-
}));
|
|
21
|
-
}
|
|
22
|
-
nullish() {
|
|
23
|
-
return new ArrayType(this.element, ...this._with({
|
|
24
|
-
isNullable: true,
|
|
25
|
-
isOptional: true
|
|
26
|
-
}));
|
|
27
|
-
}
|
|
28
|
-
default(value) {
|
|
29
|
-
return new ArrayType(this.element, ...this._with({
|
|
30
|
-
options: {
|
|
31
|
-
default: value
|
|
32
|
-
},
|
|
33
|
-
hasDefault: true
|
|
34
|
-
}));
|
|
35
|
-
}
|
|
36
|
-
description(description) {
|
|
37
|
-
return new ArrayType(this.element, ...this._with({
|
|
38
|
-
options: {
|
|
39
|
-
description
|
|
40
|
-
}
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
examples(...examples) {
|
|
44
|
-
return new ArrayType(this.element, ...this._with({
|
|
45
|
-
options: {
|
|
46
|
-
example: examples[0],
|
|
47
|
-
examples
|
|
48
|
-
}
|
|
49
|
-
}));
|
|
4
|
+
_;
|
|
5
|
+
static factory(element, options = {}) {
|
|
6
|
+
return new ArrayType(Type.Array(element.schema, options));
|
|
50
7
|
}
|
|
51
8
|
min(value) {
|
|
52
|
-
return
|
|
53
|
-
options
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}));
|
|
9
|
+
return ArrayType.factory(this.props.element, {
|
|
10
|
+
...this.props.options,
|
|
11
|
+
minItems: value
|
|
12
|
+
});
|
|
57
13
|
}
|
|
58
14
|
max(value) {
|
|
59
|
-
return
|
|
60
|
-
options
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}));
|
|
15
|
+
return ArrayType.factory(this.props.element, {
|
|
16
|
+
...this.props.options,
|
|
17
|
+
maxItems: value
|
|
18
|
+
});
|
|
64
19
|
}
|
|
65
20
|
length(value) {
|
|
66
|
-
return
|
|
67
|
-
options
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}));
|
|
21
|
+
return ArrayType.factory(this.props.element, {
|
|
22
|
+
...this.props.options,
|
|
23
|
+
minItems: value,
|
|
24
|
+
maxItems: value
|
|
25
|
+
});
|
|
72
26
|
}
|
|
73
27
|
}
|
package/dist/types/array.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/array.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/types/array.ts"],"sourcesContent":["import {\n type ArrayOptions,\n type StaticDecode,\n type TArray,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class ArrayType<T extends BaseType = BaseType> extends BaseType<\n TArray<T['schema']>,\n { element: T; options: ArrayOptions }\n> {\n _!: {\n encoded: {\n input: TArray<T['_']['encoded']['input']>\n output: TArray<T['_']['encoded']['output']>\n }\n decoded: {\n input: TArray<T['_']['decoded']['input']>\n output: TArray<T['_']['decoded']['output']>\n }\n }\n\n static factory<T extends BaseType>(element: T, options: ArrayOptions = {}) {\n return new ArrayType<T>(Type.Array(element.schema, options))\n }\n\n min(value: number) {\n return ArrayType.factory(this.props.element, {\n ...this.props.options,\n minItems: value,\n })\n }\n\n max(value: number) {\n return ArrayType.factory(this.props.element, {\n ...this.props.options,\n maxItems: value,\n })\n }\n\n length(value: number) {\n return ArrayType.factory(this.props.element, {\n ...this.props.options,\n minItems: value,\n maxItems: value,\n })\n }\n}\n"],"names":["Type","BaseType","ArrayType","_","factory","element","options","Array","schema","min","value","props","minItems","max","maxItems","length"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAAQ,YAAW;AAEpC,OAAO,MAAMC,kBAAiDD;IAI5DE,EASC;IAED,OAAOC,QAA4BC,OAAU,EAAEC,UAAwB,CAAC,CAAC,EAAE;QACzE,OAAO,IAAIJ,UAAaF,KAAKO,KAAK,CAACF,QAAQG,MAAM,EAAEF;IACrD;IAEAG,IAAIC,KAAa,EAAE;QACjB,OAAOR,UAAUE,OAAO,CAAC,IAAI,CAACO,KAAK,CAACN,OAAO,EAAE;YAC3C,GAAG,IAAI,CAACM,KAAK,CAACL,OAAO;YACrBM,UAAUF;QACZ;IACF;IAEAG,IAAIH,KAAa,EAAE;QACjB,OAAOR,UAAUE,OAAO,CAAC,IAAI,CAACO,KAAK,CAACN,OAAO,EAAE;YAC3C,GAAG,IAAI,CAACM,KAAK,CAACL,OAAO;YACrBQ,UAAUJ;QACZ;IACF;IAEAK,OAAOL,KAAa,EAAE;QACpB,OAAOR,UAAUE,OAAO,CAAC,IAAI,CAACO,KAAK,CAACN,OAAO,EAAE;YAC3C,GAAG,IAAI,CAACM,KAAK,CAACL,OAAO;YACrBM,UAAUF;YACVI,UAAUJ;QACZ;IACF;AACF"}
|