@nmtjs/type 0.6.4 → 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/dist/index.js +31 -31
- package/dist/index.js.map +1 -1
- package/dist/temporal.js +7 -8
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +4 -4
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +23 -23
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +65 -66
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +4 -4
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +10 -9
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +6 -13
- package/dist/types/date.js.map +1 -1
- package/dist/types/enum.js +7 -16
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +7 -6
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +4 -4
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +26 -93
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +42 -31
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +31 -47
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +40 -41
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +25 -18
- package/dist/types/union.js.map +1 -1
- package/dist/utils.js +0 -1
- package/dist/utils.js.map +1 -1
- package/package.json +7 -19
- package/src/index.ts +24 -25
- package/src/temporal.ts +8 -8
- package/src/types/any.ts +5 -3
- package/src/types/array.ts +24 -22
- package/src/types/base.ts +148 -81
- package/src/types/boolean.ts +5 -3
- package/src/types/custom.ts +43 -24
- package/src/types/date.ts +17 -16
- package/src/types/enum.ts +12 -22
- package/src/types/literal.ts +9 -6
- package/src/types/never.ts +5 -3
- package/src/types/number.ts +31 -93
- package/src/types/object.ts +44 -37
- package/src/types/string.ts +41 -39
- package/src/types/temporal.ts +72 -32
- package/src/types/union.ts +59 -50
- package/src/utils.ts +22 -22
- package/dist/compiler.js +0 -55
- package/dist/compiler.js.map +0 -1
- package/dist/formats.js +0 -127
- package/dist/formats.js.map +0 -1
- package/dist/inference.js +0 -1
- package/dist/inference.js.map +0 -1
- package/dist/parse.js +0 -145
- package/dist/parse.js.map +0 -1
- package/dist/runtime.js +0 -73
- package/dist/runtime.js.map +0 -1
- package/dist/schemas/default.js +0 -6
- package/dist/schemas/default.js.map +0 -1
- package/dist/schemas/discriminated-union.js +0 -9
- package/dist/schemas/discriminated-union.js.map +0 -1
- package/dist/schemas/nullable.js +0 -11
- package/dist/schemas/nullable.js.map +0 -1
- package/src/compiler.ts +0 -100
- package/src/formats.ts +0 -182
- package/src/inference.ts +0 -128
- package/src/parse.ts +0 -217
- package/src/runtime.ts +0 -137
- package/src/schemas/default.ts +0 -12
- package/src/schemas/discriminated-union.ts +0 -49
- package/src/schemas/nullable.ts +0 -20
package/dist/runtime.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { TransformDecode, TransformEncode, Value } from '@sinclair/typebox/value';
|
|
2
|
-
import { register } from "./formats.js";
|
|
3
|
-
import { Clone } from "./parse.js";
|
|
4
|
-
import { IsDiscriminatedUnion } from "./schemas/discriminated-union.js";
|
|
5
|
-
register();
|
|
6
|
-
export function _applyDefaults(schema, value) {
|
|
7
|
-
return Value.Default(schema, value);
|
|
8
|
-
}
|
|
9
|
-
export function _parse(schema, value, cloneOptions) {
|
|
10
|
-
if (cloneOptions?.clone !== false) {
|
|
11
|
-
value = Clone(value, cloneOptions?.exclude);
|
|
12
|
-
}
|
|
13
|
-
return Value.Clean(schema, Value.Convert(schema, value));
|
|
14
|
-
}
|
|
15
|
-
export function _traversErrors(errors) {
|
|
16
|
-
const result = [];
|
|
17
|
-
for (const error of errors){
|
|
18
|
-
if (IsDiscriminatedUnion(error.schema)) {
|
|
19
|
-
const discriminator = error.schema.discriminator;
|
|
20
|
-
const discriminatorValue = error.value?.[discriminator];
|
|
21
|
-
if (discriminatorValue !== undefined) {
|
|
22
|
-
const variantSchema = error.schema.anyOf.find((schema)=>schema.properties[discriminator].const === discriminatorValue);
|
|
23
|
-
if (variantSchema) {
|
|
24
|
-
const propertiesSchemas = [];
|
|
25
|
-
for(const element in variantSchema.properties){
|
|
26
|
-
const propertySchema = variantSchema.properties[element];
|
|
27
|
-
if (propertySchema !== variantSchema.properties[discriminator]) {
|
|
28
|
-
propertiesSchemas.push(propertySchema);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
for (const iter of error.errors){
|
|
32
|
-
for (const err of iter){
|
|
33
|
-
if (!propertiesSchemas.includes(err.schema)) continue;
|
|
34
|
-
result.push({
|
|
35
|
-
path: err.path,
|
|
36
|
-
message: err.message,
|
|
37
|
-
value: err.value
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
result.push({
|
|
46
|
-
path: error.path,
|
|
47
|
-
message: error.message,
|
|
48
|
-
value: error.value
|
|
49
|
-
});
|
|
50
|
-
for (const nestedError of error.errors){
|
|
51
|
-
result.push(..._traversErrors(nestedError));
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
export function applyDefaults(type, value) {
|
|
57
|
-
return _applyDefaults(type.schema, value);
|
|
58
|
-
}
|
|
59
|
-
export function parse(type, value, cloneOptions) {
|
|
60
|
-
return _parse(type.schema, value, cloneOptions);
|
|
61
|
-
}
|
|
62
|
-
export function errors(type, value) {
|
|
63
|
-
return _traversErrors(Value.Errors(type.schema, value));
|
|
64
|
-
}
|
|
65
|
-
export function check(type, value) {
|
|
66
|
-
return Value.Check(type.schema, value);
|
|
67
|
-
}
|
|
68
|
-
export function decode(type, value) {
|
|
69
|
-
return TransformDecode(type.schema, [], value);
|
|
70
|
-
}
|
|
71
|
-
export function encode(type, value) {
|
|
72
|
-
return TransformEncode(type.schema, [], value);
|
|
73
|
-
}
|
package/dist/runtime.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/runtime.ts"],"sourcesContent":["import type { ClassConstructor } from '@nmtjs/common'\nimport type { TSchema } from '@sinclair/typebox'\nimport type { ValueErrorIterator } from '@sinclair/typebox/compiler'\nimport {\n TransformDecode,\n TransformEncode,\n Value,\n} from '@sinclair/typebox/value'\nimport { register } from './formats.ts'\nimport type {\n StaticInputEncode,\n StaticOutputDecode,\n StaticOutputEncode,\n} from './inference.ts'\nimport { Clone } from './parse.ts'\nimport { IsDiscriminatedUnion } from './schemas/discriminated-union.ts'\nimport type { BaseType } from './types/base.ts'\n\n// register ajv formats\nregister()\n\nexport type CloneOptions = {\n clone?: boolean\n exclude?: Set<any>\n}\n\nexport type ValidationError = {\n path: string\n message: string\n value: unknown\n}\n\n// TODO: this one is very slow\nexport function _applyDefaults(schema: TSchema, value: any) {\n return Value.Default(schema, value)\n}\n\n// TODO: this one is very slow\n// Clone -> Clean -> Convert\nexport function _parse(\n schema: TSchema,\n value: any,\n cloneOptions?: CloneOptions,\n) {\n if (cloneOptions?.clone !== false) {\n value = Clone(value, cloneOptions?.exclude)\n }\n return Value.Clean(schema, Value.Convert(schema, value))\n}\n\nexport function _traversErrors(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(..._traversErrors(nestedError))\n }\n }\n\n return result\n}\n\nexport function applyDefaults(type: BaseType, value: unknown) {\n return _applyDefaults(type.schema, value)\n}\n\nexport function parse(\n type: BaseType,\n value: unknown,\n cloneOptions?: CloneOptions,\n) {\n return _parse(type.schema, value, cloneOptions)\n}\n\nexport function errors(type: BaseType, value: unknown): ValidationError[] {\n return _traversErrors(Value.Errors(type.schema, value))\n}\n\nexport function check<T extends BaseType>(\n type: T,\n value: unknown,\n): value is StaticInputEncode<T['schema']> {\n return Value.Check(type.schema, value)\n}\n\nexport function decode<T extends BaseType>(\n type: T,\n value: unknown,\n): StaticOutputDecode<T['schema']> {\n return TransformDecode(type.schema, [], value)\n}\n\nexport function encode<T extends BaseType>(\n type: T,\n value: unknown,\n): StaticOutputEncode<T['schema']> {\n return TransformEncode(type.schema, [], value)\n}\n"],"names":["TransformDecode","TransformEncode","Value","register","Clone","IsDiscriminatedUnion","_applyDefaults","schema","value","Default","_parse","cloneOptions","clone","exclude","Clean","Convert","_traversErrors","errors","result","error","discriminator","discriminatorValue","undefined","variantSchema","anyOf","find","properties","const","propertiesSchemas","element","propertySchema","push","iter","err","includes","path","message","nestedError","applyDefaults","type","parse","Errors","check","Check","decode","encode"],"mappings":"AAGA,SACEA,eAAe,EACfC,eAAe,EACfC,KAAK,QACA,0BAAyB;AAChC,SAASC,QAAQ,QAAQ,eAAc;AAMvC,SAASC,KAAK,QAAQ,aAAY;AAClC,SAASC,oBAAoB,QAAQ,mCAAkC;AAIvEF;AAcA,OAAO,SAASG,eAAeC,MAAe,EAAEC,KAAU;IACxD,OAAON,MAAMO,OAAO,CAACF,QAAQC;AAC/B;AAIA,OAAO,SAASE,OACdH,MAAe,EACfC,KAAU,EACVG,YAA2B;IAE3B,IAAIA,cAAcC,UAAU,OAAO;QACjCJ,QAAQJ,MAAMI,OAAOG,cAAcE;IACrC;IACA,OAAOX,MAAMY,KAAK,CAACP,QAAQL,MAAMa,OAAO,CAACR,QAAQC;AACnD;AAEA,OAAO,SAASQ,eAAeC,MAA0B;IACvD,MAAMC,SAA4B,EAAE;IAEpC,KAAK,MAAMC,SAASF,OAAQ;QAC1B,IAAIZ,qBAAqBc,MAAMZ,MAAM,GAAG;YACtC,MAAMa,gBAAgBD,MAAMZ,MAAM,CAACa,aAAa;YAChD,MAAMC,qBAAqBF,MAAMX,KAAK,EAAE,CAACY,cAAc;YACvD,IAAIC,uBAAuBC,WAAW;gBACpC,MAAMC,gBAAgBJ,MAAMZ,MAAM,CAACiB,KAAK,CAACC,IAAI,CAC3C,CAAClB,SACCA,OAAOmB,UAAU,CAACN,cAAc,CAACO,KAAK,KAAKN;gBAE/C,IAAIE,eAAe;oBACjB,MAAMK,oBAA+B,EAAE;oBACvC,IAAK,MAAMC,WAAWN,cAAcG,UAAU,CAAE;wBAC9C,MAAMI,iBAAiBP,cAAcG,UAAU,CAACG,QAAQ;wBACxD,IAAIC,mBAAmBP,cAAcG,UAAU,CAACN,cAAc,EAAE;4BAC9DQ,kBAAkBG,IAAI,CAACD;wBACzB;oBACF;oBAEA,KAAK,MAAME,QAAQb,MAAMF,MAAM,CAAE;wBAC/B,KAAK,MAAMgB,OAAOD,KAAM;4BACtB,IAAI,CAACJ,kBAAkBM,QAAQ,CAACD,IAAI1B,MAAM,GAAG;4BAC7CW,OAAOa,IAAI,CAAC;gCACVI,MAAMF,IAAIE,IAAI;gCACdC,SAASH,IAAIG,OAAO;gCACpB5B,OAAOyB,IAAIzB,KAAK;4BAClB;wBACF;oBACF;oBAEA;gBACF;YACF;QACF;QAEAU,OAAOa,IAAI,CAAC;YACVI,MAAMhB,MAAMgB,IAAI;YAChBC,SAASjB,MAAMiB,OAAO;YACtB5B,OAAOW,MAAMX,KAAK;QACpB;QAEA,KAAK,MAAM6B,eAAelB,MAAMF,MAAM,CAAE;YACtCC,OAAOa,IAAI,IAAIf,eAAeqB;QAChC;IACF;IAEA,OAAOnB;AACT;AAEA,OAAO,SAASoB,cAAcC,IAAc,EAAE/B,KAAc;IAC1D,OAAOF,eAAeiC,KAAKhC,MAAM,EAAEC;AACrC;AAEA,OAAO,SAASgC,MACdD,IAAc,EACd/B,KAAc,EACdG,YAA2B;IAE3B,OAAOD,OAAO6B,KAAKhC,MAAM,EAAEC,OAAOG;AACpC;AAEA,OAAO,SAASM,OAAOsB,IAAc,EAAE/B,KAAc;IACnD,OAAOQ,eAAed,MAAMuC,MAAM,CAACF,KAAKhC,MAAM,EAAEC;AAClD;AAEA,OAAO,SAASkC,MACdH,IAAO,EACP/B,KAAc;IAEd,OAAON,MAAMyC,KAAK,CAACJ,KAAKhC,MAAM,EAAEC;AAClC;AAEA,OAAO,SAASoC,OACdL,IAAO,EACP/B,KAAc;IAEd,OAAOR,gBAAgBuC,KAAKhC,MAAM,EAAE,EAAE,EAAEC;AAC1C;AAEA,OAAO,SAASqC,OACdN,IAAO,EACP/B,KAAc;IAEd,OAAOP,gBAAgBsC,KAAKhC,MAAM,EAAE,EAAE,EAAEC;AAC1C"}
|
package/dist/schemas/default.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/default.ts"],"sourcesContent":["import type { TSchema } from '@sinclair/typebox'\n\nexport type TDefault<Type extends TSchema, Default = unknown> = Type & {\n default: Default\n}\n\nexport function Default<Type extends TSchema, const Default>(\n type: Type,\n default_: Default,\n): TDefault<Type, Default> {\n return { ...type, default: default_ } as never\n}\n"],"names":["Default","type","default_","default"],"mappings":"AAMA,OAAO,SAASA,QACdC,IAAU,EACVC,QAAiB;IAEjB,OAAO;QAAE,GAAGD,IAAI;QAAEE,SAASD;IAAS;AACtC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
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\nexport type 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
DELETED
|
@@ -1 +0,0 @@
|
|
|
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 { default: _default } = schema\n\n return Type.Union([schema, Type.Null()], {\n default: _default,\n ...options,\n })\n}\n"],"names":["Type","Nullable","schema","options","default","_default","Union","Null"],"mappings":"AAAA,SAKEA,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/src/compiler.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { TypeCompiler } from '@sinclair/typebox/compiler'
|
|
2
|
-
import { TransformDecode, TransformEncode } from '@sinclair/typebox/value'
|
|
3
|
-
import type {
|
|
4
|
-
StaticInputEncode,
|
|
5
|
-
StaticOutputDecode,
|
|
6
|
-
StaticOutputEncode,
|
|
7
|
-
} from './inference.ts'
|
|
8
|
-
import {
|
|
9
|
-
_applyDefaults,
|
|
10
|
-
_parse,
|
|
11
|
-
_traversErrors,
|
|
12
|
-
type CloneOptions,
|
|
13
|
-
type ValidationError,
|
|
14
|
-
} from './runtime.ts'
|
|
15
|
-
import type { BaseType } from './types/base.ts'
|
|
16
|
-
|
|
17
|
-
export type Compiled<T extends BaseType = BaseType> = {
|
|
18
|
-
check: (val: unknown) => val is StaticInputEncode<T['schema']>
|
|
19
|
-
errors: (val: unknown) => ValidationError[]
|
|
20
|
-
parse: (val: unknown, cloneOptions?: CloneOptions) => unknown
|
|
21
|
-
applyDefaults: (val: unknown) => unknown
|
|
22
|
-
/**
|
|
23
|
-
* Requires to `check` before calling
|
|
24
|
-
*/
|
|
25
|
-
decode: (val: unknown) => StaticOutputDecode<T['schema']>
|
|
26
|
-
/**
|
|
27
|
-
* Requires to `check` before calling
|
|
28
|
-
*/
|
|
29
|
-
encode: (val: unknown) => StaticOutputEncode<T['schema']>
|
|
30
|
-
/**
|
|
31
|
-
* Requires to `check` before calling
|
|
32
|
-
*/
|
|
33
|
-
decodeSafe: (
|
|
34
|
-
val: unknown,
|
|
35
|
-
) =>
|
|
36
|
-
| { success: true; value: StaticOutputDecode<T['schema']> }
|
|
37
|
-
| { success: false; error: any }
|
|
38
|
-
/**
|
|
39
|
-
* Requires to `check` before calling
|
|
40
|
-
*/
|
|
41
|
-
encodeSafe: (
|
|
42
|
-
val: unknown,
|
|
43
|
-
) =>
|
|
44
|
-
| { success: true; value: StaticOutputEncode<T['schema']> }
|
|
45
|
-
| { success: false; error: any }
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function compileType(type: BaseType) {
|
|
49
|
-
const { schema } = type
|
|
50
|
-
const compiled = TypeCompiler.Compile(schema)
|
|
51
|
-
|
|
52
|
-
const check = (value: unknown) => compiled.Check(value)
|
|
53
|
-
const applyDefaults = (value: unknown) => _applyDefaults(schema, value)
|
|
54
|
-
const parse = (value: unknown, cloneOptions?: CloneOptions) =>
|
|
55
|
-
_parse(schema, value, cloneOptions)
|
|
56
|
-
const errors = (value: unknown) => _traversErrors(compiled.Errors(value))
|
|
57
|
-
const decode = TransformDecode.bind(null, schema, compiled.References())
|
|
58
|
-
const encode = TransformEncode.bind(null, schema, compiled.References())
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
check,
|
|
62
|
-
parse,
|
|
63
|
-
errors,
|
|
64
|
-
applyDefaults,
|
|
65
|
-
decode,
|
|
66
|
-
encode,
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function compile<T extends BaseType>(type: T): Compiled<T> {
|
|
71
|
-
const compiled = compileType(type)
|
|
72
|
-
|
|
73
|
-
function decodeSafe(val: unknown) {
|
|
74
|
-
try {
|
|
75
|
-
return {
|
|
76
|
-
success: true as const,
|
|
77
|
-
value: compiled.decode(val),
|
|
78
|
-
}
|
|
79
|
-
} catch (error) {
|
|
80
|
-
return { success: false as const, error }
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function encodeSafe(val: unknown) {
|
|
85
|
-
try {
|
|
86
|
-
return {
|
|
87
|
-
success: true as const,
|
|
88
|
-
value: compiled.encode(val),
|
|
89
|
-
}
|
|
90
|
-
} catch (error) {
|
|
91
|
-
return { success: false as const, error }
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
...compiled,
|
|
97
|
-
decodeSafe,
|
|
98
|
-
encodeSafe,
|
|
99
|
-
} as any
|
|
100
|
-
}
|
package/src/formats.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
// Source: https://gist.github.com/ChuckJonas/74d9cfb6ba46244ef4eaa5818c06987b
|
|
2
|
-
// TODO: review all of this
|
|
3
|
-
|
|
4
|
-
import { FormatRegistry } from '@sinclair/typebox/type'
|
|
5
|
-
|
|
6
|
-
export const register = () => {
|
|
7
|
-
for (const [name, format] of Object.entries(fullFormats)) {
|
|
8
|
-
if (format === true) {
|
|
9
|
-
FormatRegistry.Set(name, () => true)
|
|
10
|
-
continue
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (typeof format === 'function') {
|
|
14
|
-
FormatRegistry.Set(name, format)
|
|
15
|
-
continue
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
FormatRegistry.Set(name, (value) => format.test(value))
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const fullFormats: Record<
|
|
23
|
-
string,
|
|
24
|
-
RegExp | ((v: any) => boolean) | true
|
|
25
|
-
> = {
|
|
26
|
-
// date: http://tools.ietf.org/html/rfc3339#section-5.6
|
|
27
|
-
date,
|
|
28
|
-
// date-time: http://tools.ietf.org/html/rfc3339#section-5.6
|
|
29
|
-
time: getTime(true),
|
|
30
|
-
'date-time': getDateTime(true),
|
|
31
|
-
'iso-time': getTime(),
|
|
32
|
-
'iso-date-time': getDateTime(),
|
|
33
|
-
// duration: https://tools.ietf.org/html/rfc3339#appendix-A
|
|
34
|
-
duration:
|
|
35
|
-
/^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?((\d+|\d+\.\d+)S)?)?|(\d+W)?)$/,
|
|
36
|
-
uri,
|
|
37
|
-
'uri-reference':
|
|
38
|
-
/^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'"()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?(?:\?(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i,
|
|
39
|
-
// uri-template: https://tools.ietf.org/html/rfc6570
|
|
40
|
-
'uri-template':
|
|
41
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex:
|
|
42
|
-
/^(?:(?:[^\x00-\x20"'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*$/i,
|
|
43
|
-
// For the source: https://gist.github.com/dperini/729294
|
|
44
|
-
// For test cases: https://mathiasbynens.be/demo/url-regex
|
|
45
|
-
url: /^(?:https?|ftp):\/\/(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)(?:\.(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)*(?:\.(?:[a-z\u{00a1}-\u{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/iu,
|
|
46
|
-
email:
|
|
47
|
-
/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
|
|
48
|
-
hostname:
|
|
49
|
-
/^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i,
|
|
50
|
-
// optimized https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html
|
|
51
|
-
ipv4: /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/,
|
|
52
|
-
ipv6: /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i,
|
|
53
|
-
regex,
|
|
54
|
-
// uuid: http://tools.ietf.org/html/rfc4122
|
|
55
|
-
uuid: /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
|
|
56
|
-
// JSON-pointer: https://tools.ietf.org/html/rfc6901
|
|
57
|
-
// uri fragment: https://tools.ietf.org/html/rfc3986#appendix-A
|
|
58
|
-
'json-pointer': /^(?:\/(?:[^~/]|~0|~1)*)*$/,
|
|
59
|
-
'json-pointer-uri-fragment':
|
|
60
|
-
/^#(?:\/(?:[a-z0-9_\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i,
|
|
61
|
-
// relative JSON-pointer: http://tools.ietf.org/html/draft-luff-relative-json-pointer-00
|
|
62
|
-
'relative-json-pointer': /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/,
|
|
63
|
-
// the following formats are used by the openapi specification: https://spec.openapis.org/oas/v3.0.0#data-types
|
|
64
|
-
// byte: https://github.com/miguelmota/is-base64
|
|
65
|
-
byte,
|
|
66
|
-
// signed 32 bit integer
|
|
67
|
-
int32: validateInt32,
|
|
68
|
-
// signed 64 bit integer
|
|
69
|
-
int64: validateInt64,
|
|
70
|
-
// C-type float
|
|
71
|
-
float: validateNumber,
|
|
72
|
-
// C-type double
|
|
73
|
-
double: validateNumber,
|
|
74
|
-
// hint to the UI to hide input strings
|
|
75
|
-
password: true,
|
|
76
|
-
// unchecked string payload
|
|
77
|
-
binary: true,
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function isLeapYear(year: number): boolean {
|
|
81
|
-
// https://tools.ietf.org/html/rfc3339#appendix-C
|
|
82
|
-
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
|
|
86
|
-
const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
87
|
-
|
|
88
|
-
function date(str: string): boolean {
|
|
89
|
-
// full-date from http://tools.ietf.org/html/rfc3339#section-5.6
|
|
90
|
-
const matches: string[] | null = DATE.exec(str)
|
|
91
|
-
if (!matches) return false
|
|
92
|
-
const year: number = +matches[1]
|
|
93
|
-
const month: number = +matches[2]
|
|
94
|
-
const day: number = +matches[3]
|
|
95
|
-
return (
|
|
96
|
-
month >= 1 &&
|
|
97
|
-
month <= 12 &&
|
|
98
|
-
day >= 1 &&
|
|
99
|
-
day <= (month === 2 && isLeapYear(year) ? 29 : DAYS[month])
|
|
100
|
-
)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const TIME = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(z|([+-])(\d\d)(?::?(\d\d))?)?$/i
|
|
104
|
-
|
|
105
|
-
function getTime(strictTimeZone?: boolean): (str: string) => boolean {
|
|
106
|
-
return function time(str: string): boolean {
|
|
107
|
-
const matches: string[] | null = TIME.exec(str)
|
|
108
|
-
if (!matches) return false
|
|
109
|
-
const hr: number = +matches[1]
|
|
110
|
-
const min: number = +matches[2]
|
|
111
|
-
const sec: number = +matches[3]
|
|
112
|
-
const tz: string | undefined = matches[4]
|
|
113
|
-
const tzSign: number = matches[5] === '-' ? -1 : 1
|
|
114
|
-
const tzH: number = +(matches[6] || 0)
|
|
115
|
-
const tzM: number = +(matches[7] || 0)
|
|
116
|
-
if (tzH > 23 || tzM > 59 || (strictTimeZone && !tz)) return false
|
|
117
|
-
if (hr <= 23 && min <= 59 && sec < 60) return true
|
|
118
|
-
// leap second
|
|
119
|
-
const utcMin = min - tzM * tzSign
|
|
120
|
-
const utcHr = hr - tzH * tzSign - (utcMin < 0 ? 1 : 0)
|
|
121
|
-
return (
|
|
122
|
-
(utcHr === 23 || utcHr === -1) &&
|
|
123
|
-
(utcMin === 59 || utcMin === -1) &&
|
|
124
|
-
sec < 61
|
|
125
|
-
)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const DATE_TIME_SEPARATOR = /t|\s/i
|
|
130
|
-
function getDateTime(strictTimeZone?: boolean): (str: string) => boolean {
|
|
131
|
-
const time = getTime(strictTimeZone)
|
|
132
|
-
|
|
133
|
-
return function date_time(str: string): boolean {
|
|
134
|
-
// http://tools.ietf.org/html/rfc3339#section-5.6
|
|
135
|
-
const dateTime: string[] = str.split(DATE_TIME_SEPARATOR)
|
|
136
|
-
return dateTime.length === 2 && date(dateTime[0]) && time(dateTime[1])
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const NOT_URI_FRAGMENT = /\/|:/
|
|
141
|
-
const URI =
|
|
142
|
-
/^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i
|
|
143
|
-
|
|
144
|
-
function uri(str: string): boolean {
|
|
145
|
-
// http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required "."
|
|
146
|
-
return NOT_URI_FRAGMENT.test(str) && URI.test(str)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const BYTE =
|
|
150
|
-
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm
|
|
151
|
-
|
|
152
|
-
function byte(str: string): boolean {
|
|
153
|
-
BYTE.lastIndex = 0
|
|
154
|
-
return BYTE.test(str)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const MIN_INT32 = -(2 ** 31)
|
|
158
|
-
const MAX_INT32 = 2 ** 31 - 1
|
|
159
|
-
|
|
160
|
-
function validateInt32(value: number): boolean {
|
|
161
|
-
return Number.isInteger(value) && value <= MAX_INT32 && value >= MIN_INT32
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function validateInt64(value: number): boolean {
|
|
165
|
-
// JSON and javascript max Int is 2**53, so any int that passes isInteger is valid for Int64
|
|
166
|
-
return Number.isInteger(value)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function validateNumber(): boolean {
|
|
170
|
-
return true
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const Z_ANCHOR = /[^\\]\\Z/
|
|
174
|
-
function regex(str: string): boolean {
|
|
175
|
-
if (Z_ANCHOR.test(str)) return false
|
|
176
|
-
try {
|
|
177
|
-
new RegExp(str)
|
|
178
|
-
return true
|
|
179
|
-
} catch (e) {
|
|
180
|
-
return false
|
|
181
|
-
}
|
|
182
|
-
}
|
package/src/inference.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import type * as Types from '@sinclair/typebox'
|
|
2
|
-
import type { TProperties, TSchema } from '@sinclair/typebox'
|
|
3
|
-
import type { TDefault } from './schemas/default.ts'
|
|
4
|
-
|
|
5
|
-
export type StaticInputEncode<Type extends TSchema> =
|
|
6
|
-
Type extends Types.TOptional<TSchema>
|
|
7
|
-
? Types.StaticEncode<Type> | undefined
|
|
8
|
-
: Types.StaticEncode<Type>
|
|
9
|
-
|
|
10
|
-
export type StaticInputDecode<Type extends TSchema> =
|
|
11
|
-
Type extends Types.TOptional<TSchema>
|
|
12
|
-
? Types.StaticDecode<Type> | undefined
|
|
13
|
-
: Types.StaticDecode<Type>
|
|
14
|
-
|
|
15
|
-
export type StaticOutputEncode<Type extends TSchema> = Types.StaticEncode<
|
|
16
|
-
TMap<Type, StaticOutputMapping>
|
|
17
|
-
>
|
|
18
|
-
|
|
19
|
-
export type StaticOutputDecode<Type extends TSchema> = Types.StaticDecode<
|
|
20
|
-
TMap<Type, StaticOutputMapping>
|
|
21
|
-
>
|
|
22
|
-
|
|
23
|
-
interface StaticOutputMapping extends TMapping {
|
|
24
|
-
output: this['input']
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
interface TMapping {
|
|
28
|
-
input: unknown
|
|
29
|
-
output: unknown
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
type TApply<
|
|
33
|
-
Type extends TSchema,
|
|
34
|
-
Mapping extends TMapping,
|
|
35
|
-
Mapped = (Mapping & { input: Type })['output'],
|
|
36
|
-
Result = Mapped extends TSchema ? Mapped : never,
|
|
37
|
-
> = Result
|
|
38
|
-
|
|
39
|
-
type TFromProperties<
|
|
40
|
-
Properties extends TProperties,
|
|
41
|
-
Mapping extends TMapping,
|
|
42
|
-
Result extends TProperties = {
|
|
43
|
-
[Key in keyof Properties]: TMap<Properties[Key], Mapping>
|
|
44
|
-
},
|
|
45
|
-
> = Result
|
|
46
|
-
|
|
47
|
-
type TFromRest<
|
|
48
|
-
Types extends TSchema[],
|
|
49
|
-
Mapping extends TMapping,
|
|
50
|
-
Result extends TSchema[] = [],
|
|
51
|
-
> = Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]]
|
|
52
|
-
? TFromRest<Right, Mapping, [...Result, TMap<Left, Mapping>]>
|
|
53
|
-
: Result
|
|
54
|
-
|
|
55
|
-
type TFromType<
|
|
56
|
-
Type extends TSchema,
|
|
57
|
-
Mapping extends TMapping,
|
|
58
|
-
Result extends TSchema = TApply<Type, Mapping>,
|
|
59
|
-
> = Result
|
|
60
|
-
|
|
61
|
-
type UnwrapDefault<T extends TSchema> = T extends TDefault<infer U>
|
|
62
|
-
? U extends Types.TOptional<infer V>
|
|
63
|
-
? V
|
|
64
|
-
: U
|
|
65
|
-
: T
|
|
66
|
-
|
|
67
|
-
type TMap<
|
|
68
|
-
Type extends TSchema,
|
|
69
|
-
Mapping extends TMapping,
|
|
70
|
-
// Maps the Exterior Type
|
|
71
|
-
Exterior extends TSchema = TFromType<Type, Mapping>,
|
|
72
|
-
// Maps the Interior Parameterized Types
|
|
73
|
-
Interior extends TSchema = Exterior extends Types.TConstructor<
|
|
74
|
-
infer Parameters extends TSchema[],
|
|
75
|
-
infer ReturnType extends TSchema
|
|
76
|
-
>
|
|
77
|
-
? Types.TConstructor<
|
|
78
|
-
TFromRest<Parameters, Mapping>,
|
|
79
|
-
TFromType<ReturnType, Mapping>
|
|
80
|
-
>
|
|
81
|
-
: Exterior extends Types.TFunction<
|
|
82
|
-
infer Parameters extends TSchema[],
|
|
83
|
-
infer ReturnType extends TSchema
|
|
84
|
-
>
|
|
85
|
-
? Types.TFunction<
|
|
86
|
-
TFromRest<Parameters, Mapping>,
|
|
87
|
-
TFromType<ReturnType, Mapping>
|
|
88
|
-
>
|
|
89
|
-
: Exterior extends Types.TIntersect<infer Types extends TSchema[]>
|
|
90
|
-
? Types.TIntersect<TFromRest<Types, Mapping>>
|
|
91
|
-
: Exterior extends Types.TUnion<infer Types extends TSchema[]>
|
|
92
|
-
? Types.TUnion<TFromRest<Types, Mapping>>
|
|
93
|
-
: Exterior extends Types.TTuple<infer Types extends TSchema[]>
|
|
94
|
-
? Types.TTuple<TFromRest<Types, Mapping>>
|
|
95
|
-
: Exterior extends Types.TArray<infer Type extends TSchema>
|
|
96
|
-
? Types.TArray<TFromType<Type, Mapping>>
|
|
97
|
-
: Exterior extends Types.TAsyncIterator<
|
|
98
|
-
infer Type extends TSchema
|
|
99
|
-
>
|
|
100
|
-
? Types.TAsyncIterator<TFromType<Type, Mapping>>
|
|
101
|
-
: Exterior extends Types.TIterator<infer Type extends TSchema>
|
|
102
|
-
? Types.TIterator<TFromType<Type, Mapping>>
|
|
103
|
-
: Exterior extends Types.TPromise<infer Type extends TSchema>
|
|
104
|
-
? Types.TPromise<TFromType<Type, Mapping>>
|
|
105
|
-
: Exterior extends Types.TObject<
|
|
106
|
-
infer Properties extends TProperties
|
|
107
|
-
>
|
|
108
|
-
? Types.TObject<TFromProperties<Properties, Mapping>>
|
|
109
|
-
: Exterior extends Types.TRecord<
|
|
110
|
-
infer Key extends TSchema,
|
|
111
|
-
infer Value extends TSchema
|
|
112
|
-
>
|
|
113
|
-
? Types.TRecordOrObject<
|
|
114
|
-
TFromType<Key, Mapping>,
|
|
115
|
-
TFromType<Value, Mapping>
|
|
116
|
-
>
|
|
117
|
-
: Exterior,
|
|
118
|
-
// Modifiers Derived from Exterior Type Mapping
|
|
119
|
-
IsOptional extends number = Exterior extends Types.TOptional<TSchema> ? 1 : 0,
|
|
120
|
-
IsReadonly extends number = Exterior extends Types.TReadonly<TSchema> ? 1 : 0,
|
|
121
|
-
Result extends TSchema = [IsReadonly, IsOptional] extends [1, 1]
|
|
122
|
-
? Types.TReadonly<UnwrapDefault<Interior>>
|
|
123
|
-
: [IsReadonly, IsOptional] extends [0, 1]
|
|
124
|
-
? UnwrapDefault<Interior>
|
|
125
|
-
: [IsReadonly, IsOptional] extends [1, 0]
|
|
126
|
-
? Types.TReadonly<UnwrapDefault<Interior>>
|
|
127
|
-
: UnwrapDefault<Interior>,
|
|
128
|
-
> = Result
|