@nmtjs/type 0.2.1 → 0.3.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 +2 -2
- package/dist/compiler.js.map +1 -1
- package/dist/constants.js +2 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/temporal.js +3 -1
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +37 -5
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +55 -24
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +31 -62
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +37 -5
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +37 -8
- package/dist/types/custom.js.map +1 -1
- package/dist/types/datetime.js +41 -22
- package/dist/types/datetime.js.map +1 -1
- package/dist/types/enum.js +74 -16
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +35 -8
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +17 -2
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +116 -62
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +58 -17
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +57 -21
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +292 -74
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +75 -17
- package/dist/types/union.js.map +1 -1
- package/package.json +3 -3
- package/src/compiler.ts +2 -2
- package/src/constants.ts +5 -0
- package/src/index.ts +5 -6
- package/src/temporal.ts +4 -2
- package/src/types/any.ts +32 -9
- package/src/types/array.ts +59 -28
- package/src/types/base.ts +59 -112
- package/src/types/boolean.ts +31 -9
- package/src/types/custom.ts +61 -24
- package/src/types/datetime.ts +40 -35
- package/src/types/enum.ts +78 -21
- package/src/types/literal.ts +42 -12
- package/src/types/never.ts +24 -11
- package/src/types/number.ts +103 -67
- package/src/types/object.ts +87 -32
- package/src/types/string.ts +38 -30
- package/src/types/temporal.ts +378 -118
- package/src/types/union.ts +103 -31
package/dist/compiler.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
2
2
|
import { Value } from '@sinclair/typebox/value';
|
|
3
|
-
import {
|
|
3
|
+
import { getTypeSchema } from "./types/base.js";
|
|
4
4
|
const compileType = (type)=>{
|
|
5
|
-
const schema = type
|
|
5
|
+
const schema = getTypeSchema(type);
|
|
6
6
|
const compiled = TypeCompiler.Compile(schema);
|
|
7
7
|
const prepare = (value)=>{
|
|
8
8
|
for (const fn of [
|
package/dist/compiler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport { Value } from '@sinclair/typebox/value'\nimport { type BaseType,
|
|
1
|
+
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport { Value } from '@sinclair/typebox/value'\nimport { type BaseType, getTypeSchema } from './types/base.ts'\n\nexport type Compiled = {\n check: (val: unknown) => boolean\n errors: (val: unknown) => ValueErrorIterator\n prepare: (val: unknown) => unknown\n convert: (val: unknown) => unknown\n decode: (\n val: unknown,\n ) => { success: true; value: unknown } | { success: false; error: any }\n encode: (\n val: unknown,\n ) => { success: true; value: unknown } | { success: false; error: any }\n}\n\nconst compileType = (type: BaseType) => {\n const schema = getTypeSchema(type)\n const compiled = TypeCompiler.Compile(schema)\n const prepare = (value: any) => {\n for (const fn of [Value.Clean, Value.Default]) {\n value = fn(schema, value)\n }\n return value\n }\n const convert = (value: any) => Value.Convert(schema, value)\n return {\n check: compiled.Check.bind(compiled),\n errors: compiled.Errors.bind(compiled),\n decode: compiled.Decode.bind(compiled),\n encode: compiled.Encode.bind(compiled),\n prepare,\n convert,\n }\n}\n\nexport const compile = (schema: BaseType): Compiled => {\n const compiled = compileType(schema)\n\n // TODO: custom error handling/shaping\n return {\n ...compiled,\n decode: (val) => {\n try {\n return {\n success: true as const,\n value: compiled.decode(compiled.prepare(val)),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n },\n encode: (val) => {\n try {\n return {\n success: true as const,\n value: compiled.encode(compiled.prepare(val)),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n },\n }\n}\n"],"names":["TypeCompiler","Value","getTypeSchema","compileType","type","schema","compiled","Compile","prepare","value","fn","Clean","Default","convert","Convert","check","Check","bind","errors","Errors","decode","Decode","encode","Encode","compile","val","success","error"],"mappings":"AAAA,SACEA,YAAY,QAEP,6BAA4B;AACnC,SAASC,KAAK,QAAQ,0BAAyB;AAC/C,SAAwBC,aAAa,QAAQ,kBAAiB;AAe9D,MAAMC,cAAc,CAACC;IACnB,MAAMC,SAASH,cAAcE;IAC7B,MAAME,WAAWN,aAAaO,OAAO,CAACF;IACtC,MAAMG,UAAU,CAACC;QACf,KAAK,MAAMC,MAAM;YAACT,MAAMU,KAAK;YAAEV,MAAMW,OAAO;SAAC,CAAE;YAC7CH,QAAQC,GAAGL,QAAQI;QACrB;QACA,OAAOA;IACT;IACA,MAAMI,UAAU,CAACJ,QAAeR,MAAMa,OAAO,CAACT,QAAQI;IACtD,OAAO;QACLM,OAAOT,SAASU,KAAK,CAACC,IAAI,CAACX;QAC3BY,QAAQZ,SAASa,MAAM,CAACF,IAAI,CAACX;QAC7Bc,QAAQd,SAASe,MAAM,CAACJ,IAAI,CAACX;QAC7BgB,QAAQhB,SAASiB,MAAM,CAACN,IAAI,CAACX;QAC7BE;QACAK;IACF;AACF;AAEA,OAAO,MAAMW,UAAU,CAACnB;IACtB,MAAMC,WAAWH,YAAYE;IAG7B,OAAO;QACL,GAAGC,QAAQ;QACXc,QAAQ,CAACK;YACP,IAAI;gBACF,OAAO;oBACLC,SAAS;oBACTjB,OAAOH,SAASc,MAAM,CAACd,SAASE,OAAO,CAACiB;gBAC1C;YACF,EAAE,OAAOE,OAAO;gBACd,OAAO;oBAAED,SAAS;oBAAgBC;gBAAM;YAC1C;QACF;QACAL,QAAQ,CAACG;YACP,IAAI;gBACF,OAAO;oBACLC,SAAS;oBACTjB,OAAOH,SAASgB,MAAM,CAAChB,SAASE,OAAO,CAACiB;gBAC1C;YACF,EAAE,OAAOE,OAAO;gBACd,OAAO;oBAAED,SAAS;oBAAgBC;gBAAM;YAC1C;QACF;IACF;AACF,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/constants.ts"],"sourcesContent":["export const typeStatic: unique symbol = Symbol()\nexport type typeStatic = typeof typeStatic\n\nexport const typeSchema: unique symbol = Symbol()\nexport type typeSchema = typeof typeSchema\n"],"names":["typeStatic","Symbol","typeSchema"],"mappings":"AAAA,OAAO,MAAMA,aAA4BC,SAAQ;AAGjD,OAAO,MAAMC,aAA4BD,SAAQ"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ArrayType } from "./types/array.js";
|
|
2
2
|
import { BooleanType } from "./types/boolean.js";
|
|
3
3
|
import { CustomType } from "./types/custom.js";
|
|
4
|
-
import {
|
|
4
|
+
import { DateType } from "./types/datetime.js";
|
|
5
5
|
import { EnumType, NativeEnumType } from "./types/enum.js";
|
|
6
6
|
import { LiteralType } from "./types/literal.js";
|
|
7
7
|
import { IntegerType, NumberType } from "./types/number.js";
|
|
@@ -16,7 +16,7 @@ export * from "./schemas/native-enum.js";
|
|
|
16
16
|
export * from "./schemas/union-enum.js";
|
|
17
17
|
export * from "./schemas/nullable.js";
|
|
18
18
|
export { BaseType, getTypeSchema } from "./types/base.js";
|
|
19
|
-
export { ArrayType, BooleanType, CustomType,
|
|
19
|
+
export { ArrayType, BooleanType, CustomType, DateType, EnumType, LiteralType, IntegerType, NumberType, ObjectType, StringType, IntersactionType, UnionType, AnyType, NeverType, };
|
|
20
20
|
export var t;
|
|
21
21
|
(function(t) {
|
|
22
22
|
t.never = ()=>new NeverType();
|
|
@@ -28,7 +28,6 @@ export var t;
|
|
|
28
28
|
t.nativeEnum = (enumLike)=>new NativeEnumType(enumLike);
|
|
29
29
|
t.arrayEnum = (enumLike)=>new EnumType(enumLike);
|
|
30
30
|
t.date = ()=>new DateType();
|
|
31
|
-
t.datetime = ()=>new DateTimeType();
|
|
32
31
|
t.array = (element)=>new ArrayType(element);
|
|
33
32
|
t.object = (properties)=>new ObjectType(properties);
|
|
34
33
|
t.any = ()=>new AnyType();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type { TLiteralValue } from '@sinclair/typebox'\nimport { ArrayType } from './types/array.ts'\nimport type { BaseType
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type { TLiteralValue } from '@sinclair/typebox'\nimport { ArrayType } from './types/array.ts'\nimport type { BaseType } from './types/base.ts'\nimport { BooleanType } from './types/boolean.ts'\nimport { CustomType } from './types/custom.ts'\nimport { DateType } from './types/datetime.ts'\nimport { EnumType, NativeEnumType } from './types/enum.ts'\nimport { LiteralType } from './types/literal.ts'\nimport { IntegerType, NumberType } from './types/number.ts'\nimport { ObjectType } from './types/object.ts'\nimport { StringType } from './types/string.ts'\nimport { IntersactionType, UnionType } from './types/union.ts'\n\nimport type { typeStatic } from './constants.ts'\n// register ajv formats\nimport { register } from './formats.ts'\nimport { AnyType } from './types/any.ts'\nimport { NeverType } from './types/never.ts'\nregister()\n\nexport * from './schemas/native-enum.ts'\nexport * from './schemas/union-enum.ts'\nexport * from './schemas/nullable.ts'\nexport {\n BaseType,\n getTypeSchema,\n} 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 BaseType> = T[typeStatic]['decoded']\n export type encoded<T extends BaseType> = T[typeStatic]['encoded']\n }\n export const never = () => new NeverType()\n export const boolean = () => new BooleanType()\n export const string = () => new StringType()\n export const number = () => new NumberType()\n export const integer = () => new IntegerType()\n export const literal = <T extends TLiteralValue>(value: T) =>\n new LiteralType(value)\n export const nativeEnum = <T extends { [K in string]: K }>(enumLike: T) =>\n new NativeEnumType(enumLike)\n export const arrayEnum = <T extends (string | number)[]>(enumLike: [...T]) =>\n new EnumType(enumLike)\n export const date = () => new DateType()\n export const array = <T extends BaseType>(element: T) =>\n new ArrayType(element)\n export const object = <T extends Record<string, BaseType>>(properties: T) =>\n new ObjectType(properties)\n export const any = () => new AnyType()\n export const or = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new UnionType(types)\n export const and = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new IntersactionType(types)\n export const custom = <T>(\n decode: (value: any) => T,\n encode: (value: T) => any,\n ) => new CustomType<T>(decode, encode)\n}\n"],"names":["ArrayType","BooleanType","CustomType","DateType","EnumType","NativeEnumType","LiteralType","IntegerType","NumberType","ObjectType","StringType","IntersactionType","UnionType","register","AnyType","NeverType","BaseType","getTypeSchema","t","never","boolean","string","number","integer","literal","value","nativeEnum","enumLike","arrayEnum","date","array","element","object","properties","any","or","types","and","custom","decode","encode"],"mappings":"AACA,SAASA,SAAS,QAAQ,mBAAkB;AAE5C,SAASC,WAAW,QAAQ,qBAAoB;AAChD,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,QAAQ,QAAQ,sBAAqB;AAC9C,SAASC,QAAQ,EAAEC,cAAc,QAAQ,kBAAiB;AAC1D,SAASC,WAAW,QAAQ,qBAAoB;AAChD,SAASC,WAAW,EAAEC,UAAU,QAAQ,oBAAmB;AAC3D,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,gBAAgB,EAAEC,SAAS,QAAQ,mBAAkB;AAI9D,SAASC,QAAQ,QAAQ,eAAc;AACvC,SAASC,OAAO,QAAQ,iBAAgB;AACxC,SAASC,SAAS,QAAQ,mBAAkB;AAC5CF;AAEA,cAAc,2BAA0B;AACxC,cAAc,0BAAyB;AACvC,cAAc,wBAAuB;AACrC,SACEG,QAAQ,EACRC,aAAa,QACR,kBAAiB;AAExB,SACEjB,SAAS,EACTC,WAAW,EACXC,UAAU,EACVC,QAAQ,EACRC,QAAQ,EACRE,WAAW,EACXC,WAAW,EACXC,UAAU,EACVC,UAAU,EACVC,UAAU,EACVC,gBAAgB,EAChBC,SAAS,EACTE,OAAO,EACPC,SAAS,KACV;;UAEgBG;MAKFC,QAAQ,IAAM,IAAIJ;MAClBK,UAAU,IAAM,IAAInB;MACpBoB,SAAS,IAAM,IAAIX;MACnBY,SAAS,IAAM,IAAId;MACnBe,UAAU,IAAM,IAAIhB;MACpBiB,UAAU,CAA0BC,QAC/C,IAAInB,YAAYmB;MACLC,aAAa,CAAiCC,WACzD,IAAItB,eAAesB;MACRC,YAAY,CAAgCD,WACvD,IAAIvB,SAASuB;MACFE,OAAO,IAAM,IAAI1B;MACjB2B,QAAQ,CAAqBC,UACxC,IAAI/B,UAAU+B;MACHC,SAAS,CAAqCC,aACzD,IAAIxB,WAAWwB;MACJC,MAAM,IAAM,IAAIpB;MAChBqB,KAAK,CAChB,GAAGC,QACA,IAAIxB,UAAUwB;MACNC,MAAM,CACjB,GAAGD,QACA,IAAIzB,iBAAiByB;MACbE,SAAS,CACpBC,QACAC,SACG,IAAItC,WAAcqC,QAAQC;AACjC,GAhCiBtB,MAAAA"}
|
package/dist/temporal.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { t as baseT } from "./index.js";
|
|
1
2
|
import { DurationType, PlainDateTimeType, PlainDateType, PlainMonthDayType, PlainTimeType, PlainYearMonthType, ZonedDateTimeType } from "./types/temporal.js";
|
|
2
|
-
|
|
3
|
+
function extend(value) {
|
|
3
4
|
return Object.assign({}, value, {
|
|
4
5
|
temporal: {
|
|
5
6
|
plainDate: ()=>new PlainDateType(),
|
|
@@ -12,4 +13,5 @@ export function extend(value) {
|
|
|
12
13
|
}
|
|
13
14
|
});
|
|
14
15
|
}
|
|
16
|
+
export const t = extend(baseT);
|
|
15
17
|
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
|
|
1
|
+
{"version":3,"sources":["../../src/temporal.ts"],"sourcesContent":["import { t as baseT } from './index.ts'\nimport {\n DurationType,\n PlainDateTimeType,\n PlainDateType,\n PlainMonthDayType,\n PlainTimeType,\n PlainYearMonthType,\n ZonedDateTimeType,\n} from './types/temporal.ts'\n\nfunction extend<T extends typeof baseT>(value: T) {\n return Object.assign({}, value, {\n temporal: {\n plainDate: () => new PlainDateType(),\n plainDatetime: () => new PlainDateTimeType(),\n plainTime: () => new PlainTimeType(),\n zonedDatetime: () => new ZonedDateTimeType(),\n duration: () => new DurationType(),\n plainYearMonth: () => new PlainYearMonthType(),\n plainMonthDay: () => new PlainMonthDayType(),\n },\n })\n}\n\nexport const t = extend(baseT)\n\nexport {\n DurationType,\n PlainDateTimeType,\n PlainDateType,\n PlainMonthDayType,\n PlainTimeType,\n PlainYearMonthType,\n ZonedDateTimeType,\n}\n"],"names":["t","baseT","DurationType","PlainDateTimeType","PlainDateType","PlainMonthDayType","PlainTimeType","PlainYearMonthType","ZonedDateTimeType","extend","value","Object","assign","temporal","plainDate","plainDatetime","plainTime","zonedDatetime","duration","plainYearMonth","plainMonthDay"],"mappings":"AAAA,SAASA,KAAKC,KAAK,QAAQ,aAAY;AACvC,SACEC,YAAY,EACZC,iBAAiB,EACjBC,aAAa,EACbC,iBAAiB,EACjBC,aAAa,EACbC,kBAAkB,EAClBC,iBAAiB,QACZ,sBAAqB;AAE5B,SAASC,OAA+BC,KAAQ;IAC9C,OAAOC,OAAOC,MAAM,CAAC,CAAC,GAAGF,OAAO;QAC9BG,UAAU;YACRC,WAAW,IAAM,IAAIV;YACrBW,eAAe,IAAM,IAAIZ;YACzBa,WAAW,IAAM,IAAIV;YACrBW,eAAe,IAAM,IAAIT;YACzBU,UAAU,IAAM,IAAIhB;YACpBiB,gBAAgB,IAAM,IAAIZ;YAC1Ba,eAAe,IAAM,IAAIf;QAC3B;IACF;AACF;AAEA,OAAO,MAAML,IAAIS,OAAOR,OAAM;AAE9B,SACEC,YAAY,EACZC,iBAAiB,EACjBC,aAAa,EACbC,iBAAiB,EACjBC,aAAa,EACbC,kBAAkB,EAClBC,iBAAiB,KAClB"}
|
package/dist/types/any.js
CHANGED
|
@@ -1,16 +1,48 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
3
|
export class AnyType extends BaseType {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
4
|
+
constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
|
|
5
|
+
super(options, isNullable, isOptional, hasDefault);
|
|
6
|
+
}
|
|
7
|
+
_constructSchema(options) {
|
|
8
|
+
return Type.Any(options);
|
|
6
9
|
}
|
|
7
10
|
nullable() {
|
|
8
|
-
return new AnyType(...this.
|
|
11
|
+
return new AnyType(...this._with({
|
|
12
|
+
isNullable: true
|
|
13
|
+
}));
|
|
9
14
|
}
|
|
10
15
|
optional() {
|
|
11
|
-
return new AnyType(...this.
|
|
16
|
+
return new AnyType(...this._with({
|
|
17
|
+
isOptional: true
|
|
18
|
+
}));
|
|
12
19
|
}
|
|
13
20
|
nullish() {
|
|
14
|
-
return new AnyType(...this.
|
|
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
|
+
}));
|
|
15
47
|
}
|
|
16
48
|
}
|
package/dist/types/any.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/any.ts"],"sourcesContent":["import { type TAny, Type } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class AnyType<\n N extends boolean = false,\n O extends boolean = false,\n> extends BaseType<TAny, N, O> {\n constructor(\n
|
|
1
|
+
{"version":3,"sources":["../../../src/types/any.ts"],"sourcesContent":["import { type SchemaOptions, type TAny, Type } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class AnyType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TAny, N, O, D, SchemaOptions> {\n constructor(\n options: SchemaOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(options: SchemaOptions): TAny {\n return Type.Any(options)\n }\n\n nullable() {\n return new AnyType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new AnyType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new AnyType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: any) {\n return new AnyType(\n ...this._with({\n options: { default: value },\n hasDefault: true,\n }),\n )\n }\n\n description(description: string) {\n return new AnyType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [any, ...any[]]) {\n return new AnyType(...this._with({ options: { examples } }))\n }\n}\n"],"names":["Type","BaseType","AnyType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","Any","nullable","_with","optional","nullish","default","value","description","examples"],"mappings":"AAAA,SAAwCA,IAAI,QAAQ,oBAAmB;AACvE,SAASC,QAAQ,QAAQ,YAAW;AAEpC,OAAO,MAAMC,gBAIHD;IACRE,YACEC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC;IACzC;IAEUC,iBAAiBJ,OAAsB,EAAQ;QACvD,OAAOJ,KAAKS,GAAG,CAACL;IAClB;IAEAM,WAAW;QACT,OAAO,IAAIR,WAAW,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;QAAK;IACtD;IAEAO,WAAW;QACT,OAAO,IAAIV,WAAW,IAAI,CAACS,KAAK,CAAC;YAAEL,YAAY;QAAK;IACtD;IAEAO,UAAU;QACR,OAAO,IAAIX,WAAW,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;YAAMC,YAAY;QAAK;IACxE;IAEAQ,QAAQC,KAAU,EAAE;QAClB,OAAO,IAAIb,WACN,IAAI,CAACS,KAAK,CAAC;YACZP,SAAS;gBAAEU,SAASC;YAAM;YAC1BR,YAAY;QACd;IAEJ;IAEAS,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAId,WAAW,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEY;YAAY;QAAE;IAC9D;IAEAC,SAAS,GAAGA,QAAyB,EAAE;QACrC,OAAO,IAAIf,WAAW,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEa;YAAS;QAAE;IAC3D;AACF"}
|
package/dist/types/array.js
CHANGED
|
@@ -1,42 +1,73 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
|
-
import { BaseType,
|
|
2
|
+
import { BaseType, getTypeSchema } from "./base.js";
|
|
3
3
|
export class ArrayType extends BaseType {
|
|
4
4
|
element;
|
|
5
|
-
options
|
|
6
|
-
|
|
7
|
-
super(Type.Array(element[typeFinalSchema]), nullable, optional);
|
|
5
|
+
constructor(element, options = {}, isNullable = false, isOptional = false, hasDefault = false){
|
|
6
|
+
super(options, isNullable, isOptional, hasDefault, element);
|
|
8
7
|
this.element = element;
|
|
9
|
-
|
|
8
|
+
}
|
|
9
|
+
_constructSchema(options, element) {
|
|
10
|
+
return Type.Array(getTypeSchema(element), options);
|
|
10
11
|
}
|
|
11
12
|
nullable() {
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
return new ArrayType(this.element, ...this._with({
|
|
14
|
+
isNullable: true
|
|
15
|
+
}));
|
|
14
16
|
}
|
|
15
17
|
optional() {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
return new ArrayType(this.element, ...this._with({
|
|
19
|
+
isOptional: true
|
|
20
|
+
}));
|
|
18
21
|
}
|
|
19
22
|
nullish() {
|
|
20
|
-
|
|
21
|
-
|
|
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
|
+
}));
|
|
22
50
|
}
|
|
23
51
|
min(value) {
|
|
24
|
-
return new ArrayType(this.element, {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
52
|
+
return new ArrayType(this.element, ...this._with({
|
|
53
|
+
options: {
|
|
54
|
+
minItems: value
|
|
55
|
+
}
|
|
56
|
+
}));
|
|
28
57
|
}
|
|
29
58
|
max(value) {
|
|
30
|
-
return new ArrayType(this.element, {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
59
|
+
return new ArrayType(this.element, ...this._with({
|
|
60
|
+
options: {
|
|
61
|
+
maxItems: value
|
|
62
|
+
}
|
|
63
|
+
}));
|
|
34
64
|
}
|
|
35
65
|
length(value) {
|
|
36
|
-
return new ArrayType(this.element, {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
66
|
+
return new ArrayType(this.element, ...this._with({
|
|
67
|
+
options: {
|
|
68
|
+
minItems: value,
|
|
69
|
+
maxItems: value
|
|
70
|
+
}
|
|
71
|
+
}));
|
|
41
72
|
}
|
|
42
73
|
}
|
package/dist/types/array.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/array.ts"],"sourcesContent":["import { type ArrayOptions, type TArray, Type } from '@sinclair/typebox'\nimport { BaseType,
|
|
1
|
+
{"version":3,"sources":["../../../src/types/array.ts"],"sourcesContent":["import { type ArrayOptions, type TArray, Type } from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\n\nexport class ArrayType<\n T extends BaseType = BaseType,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TArray<T[typeStatic]['schema']>, N, O, D, ArrayOptions> {\n constructor(\n readonly element: T,\n options: ArrayOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault, element)\n }\n\n protected _constructSchema(\n options: ArrayOptions,\n element: T,\n ): TArray<T[typeStatic]['schema']> {\n return Type.Array(getTypeSchema(element), options)\n }\n\n nullable() {\n return new ArrayType(this.element, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ArrayType(this.element, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ArrayType(\n this.element,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new ArrayType(\n this.element,\n ...this._with({\n options: { default: value },\n hasDefault: true,\n }),\n )\n }\n\n description(description: string) {\n return new ArrayType(\n this.element,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new ArrayType(\n this.element,\n ...this._with({\n options: { example: examples[0], examples },\n }),\n )\n }\n\n min(value: number) {\n return new ArrayType(\n this.element,\n ...this._with({\n options: { minItems: value },\n }),\n )\n }\n\n max(value: number) {\n return new ArrayType(\n this.element,\n ...this._with({\n options: { maxItems: value },\n }),\n )\n }\n\n length(value: number) {\n return new ArrayType(\n this.element,\n ...this._with({\n options: { minItems: value, maxItems: value },\n }),\n )\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","ArrayType","constructor","element","options","isNullable","isOptional","hasDefault","_constructSchema","Array","nullable","_with","optional","nullish","default","value","description","examples","example","min","minItems","max","maxItems","length"],"mappings":"AAAA,SAAyCA,IAAI,QAAQ,oBAAmB;AAExE,SAASC,QAAQ,EAAEC,aAAa,QAAQ,YAAW;AAEnD,OAAO,MAAMC,kBAKHF;;IACRG,YACE,AAASC,OAAU,EACnBC,UAAwB,CAAC,CAAC,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aAN1CA,UAAAA;IAOX;IAEUK,iBACRJ,OAAqB,EACrBD,OAAU,EACuB;QACjC,OAAOL,KAAKW,KAAK,CAACT,cAAcG,UAAUC;IAC5C;IAEAM,WAAW;QACT,OAAO,IAAIT,UAAU,IAAI,CAACE,OAAO,KAAK,IAAI,CAACQ,KAAK,CAAC;YAAEN,YAAY;QAAK;IACtE;IAEAO,WAAW;QACT,OAAO,IAAIX,UAAU,IAAI,CAACE,OAAO,KAAK,IAAI,CAACQ,KAAK,CAAC;YAAEL,YAAY;QAAK;IACtE;IAEAO,UAAU;QACR,OAAO,IAAIZ,UACT,IAAI,CAACE,OAAO,KACT,IAAI,CAACQ,KAAK,CAAC;YAAEN,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAQ,QAAQC,KAAkC,EAAE;QAC1C,OAAO,IAAId,UACT,IAAI,CAACE,OAAO,KACT,IAAI,CAACQ,KAAK,CAAC;YACZP,SAAS;gBAAEU,SAASC;YAAM;YAC1BR,YAAY;QACd;IAEJ;IAEAS,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAIf,UACT,IAAI,CAACE,OAAO,KACT,IAAI,CAACQ,KAAK,CAAC;YAAEP,SAAS;gBAAEY;YAAY;QAAE;IAE7C;IAEAC,SACE,GAAGA,QAAyE,EAC5E;QACA,OAAO,IAAIhB,UACT,IAAI,CAACE,OAAO,KACT,IAAI,CAACQ,KAAK,CAAC;YACZP,SAAS;gBAAEc,SAASD,QAAQ,CAAC,EAAE;gBAAEA;YAAS;QAC5C;IAEJ;IAEAE,IAAIJ,KAAa,EAAE;QACjB,OAAO,IAAId,UACT,IAAI,CAACE,OAAO,KACT,IAAI,CAACQ,KAAK,CAAC;YACZP,SAAS;gBAAEgB,UAAUL;YAAM;QAC7B;IAEJ;IAEAM,IAAIN,KAAa,EAAE;QACjB,OAAO,IAAId,UACT,IAAI,CAACE,OAAO,KACT,IAAI,CAACQ,KAAK,CAAC;YACZP,SAAS;gBAAEkB,UAAUP;YAAM;QAC7B;IAEJ;IAEAQ,OAAOR,KAAa,EAAE;QACpB,OAAO,IAAId,UACT,IAAI,CAACE,OAAO,KACT,IAAI,CAACQ,KAAK,CAAC;YACZP,SAAS;gBAAEgB,UAAUL;gBAAOO,UAAUP;YAAM;QAC9C;IAEJ;AACF"}
|
package/dist/types/base.js
CHANGED
|
@@ -1,77 +1,46 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
|
+
import { typeSchema, typeStatic } from "../constants.js";
|
|
2
3
|
import { Nullable } from "../schemas/nullable.js";
|
|
3
|
-
export const typeSchema = Symbol();
|
|
4
|
-
export const typeOptions = Symbol();
|
|
5
|
-
export const typeOptional = Symbol();
|
|
6
|
-
export const typeNullable = Symbol();
|
|
7
|
-
export const staticType = Symbol();
|
|
8
|
-
export const typeFinalSchema = Symbol();
|
|
9
4
|
export class BaseType {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this
|
|
17
|
-
this
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
let schema = this.
|
|
21
|
-
if (this.
|
|
5
|
+
options;
|
|
6
|
+
isNullable;
|
|
7
|
+
isOptional;
|
|
8
|
+
hasDefault;
|
|
9
|
+
[typeStatic];
|
|
10
|
+
constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false, ...contstructArgs){
|
|
11
|
+
this.options = options;
|
|
12
|
+
this.isNullable = isNullable;
|
|
13
|
+
this.isOptional = isOptional;
|
|
14
|
+
this.hasDefault = hasDefault;
|
|
15
|
+
let schema = this._constructSchema(options, ...contstructArgs);
|
|
16
|
+
if (this.isNullable) {
|
|
22
17
|
schema = Nullable(schema);
|
|
23
18
|
}
|
|
24
|
-
if (this.
|
|
19
|
+
if (this.isOptional) {
|
|
25
20
|
schema = Type.Optional(schema);
|
|
26
21
|
}
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
get _schema() {
|
|
30
|
-
return this[typeSchema];
|
|
31
|
-
}
|
|
32
|
-
get _isNullable() {
|
|
33
|
-
return this[typeNullable];
|
|
34
|
-
}
|
|
35
|
-
get _isOptional() {
|
|
36
|
-
return this[typeOptional];
|
|
22
|
+
this[typeSchema] = schema;
|
|
37
23
|
}
|
|
38
|
-
|
|
24
|
+
[typeSchema];
|
|
25
|
+
get _args() {
|
|
39
26
|
return [
|
|
40
|
-
this.
|
|
41
|
-
this.
|
|
27
|
+
this.isNullable,
|
|
28
|
+
this.isOptional,
|
|
29
|
+
this.hasDefault
|
|
42
30
|
];
|
|
43
31
|
}
|
|
44
|
-
|
|
45
|
-
return
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return this._contructSelf(this._schema, true, true);
|
|
55
|
-
}
|
|
56
|
-
default(value) {
|
|
57
|
-
return this._contructSelf({
|
|
58
|
-
...this._schema,
|
|
59
|
-
default: value
|
|
60
|
-
}, this[typeNullable], this[typeOptional]);
|
|
61
|
-
}
|
|
62
|
-
description(description) {
|
|
63
|
-
return this._contructSelf({
|
|
64
|
-
...this._schema,
|
|
65
|
-
description
|
|
66
|
-
}, this[typeNullable], this[typeOptional]);
|
|
67
|
-
}
|
|
68
|
-
examples(...examples) {
|
|
69
|
-
return this._contructSelf({
|
|
70
|
-
...this._schema,
|
|
71
|
-
examples
|
|
72
|
-
}, this[typeNullable], this[typeOptional]);
|
|
32
|
+
_with({ options = this.options, isNullable = this.isNullable, isOptional = this.isOptional, hasDefault = this.hasDefault } = {}) {
|
|
33
|
+
return [
|
|
34
|
+
{
|
|
35
|
+
...this.options,
|
|
36
|
+
...options
|
|
37
|
+
},
|
|
38
|
+
isNullable,
|
|
39
|
+
isOptional,
|
|
40
|
+
hasDefault
|
|
41
|
+
];
|
|
73
42
|
}
|
|
74
43
|
}
|
|
75
44
|
export function getTypeSchema(type) {
|
|
76
|
-
return type[
|
|
45
|
+
return type[typeSchema];
|
|
77
46
|
}
|
package/dist/types/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/base.ts"],"sourcesContent":["import {\n type StaticDecode,\n type StaticEncode,\n type TAny,\n type TOptional,\n type TSchema,\n Type,\n} from '@sinclair/typebox'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../src/types/base.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type StaticDecode,\n type StaticEncode,\n type TAny,\n type TOptional,\n type TSchema,\n Type,\n} from '@sinclair/typebox'\nimport { typeSchema, typeStatic } from '../constants.ts'\nimport { Nullable, type TNullable } from '../schemas/nullable.ts'\n\ntype ResolveNullable<T extends TSchema, Is extends boolean> = Is extends true\n ? TNullable<T>\n : T\n\ntype ResolveOptional<T extends TSchema, Is extends boolean> = Is extends true\n ? TOptional<T>\n : T\n\ntype Resolve<\n Schema extends TSchema,\n IsNullable extends boolean,\n IsOptional extends boolean,\n> = ResolveOptional<ResolveNullable<Schema, IsNullable>, IsOptional>\n\nexport abstract class BaseType<\n Schema extends TSchema = TSchema,\n IsNullable extends boolean = boolean,\n IsOptional extends boolean = boolean,\n HasDefault extends boolean = boolean,\n Options extends SchemaOptions = SchemaOptions,\n> {\n protected abstract _constructSchema(\n options: Options,\n ...constructArgs: any[]\n ): Schema\n\n [typeStatic]!: {\n schema: Resolve<Schema, IsNullable, IsOptional>\n isOptional: IsOptional\n isNullable: IsNullable\n hasDefault: HasDefault\n encoded: StaticEncode<Resolve<Schema, IsNullable, IsOptional>>\n decoded: StaticDecode<\n Resolve<Schema, IsNullable, HasDefault extends false ? false : IsOptional>\n >\n }\n\n constructor(\n protected options: Options = {} as Options,\n protected isNullable: IsNullable = false as IsNullable,\n protected isOptional: IsOptional = false as IsOptional,\n protected hasDefault: HasDefault = false as HasDefault,\n ...contstructArgs: any[]\n ) {\n let schema: TSchema = this._constructSchema(options, ...contstructArgs)\n if (this.isNullable) {\n schema = Nullable(schema)\n }\n if (this.isOptional) {\n schema = Type.Optional(schema)\n }\n this[typeSchema] = schema as Schema\n }\n protected [typeSchema]: Schema\n\n protected get _args(): [IsNullable, IsOptional, HasDefault] {\n return [this.isNullable, this.isOptional, this.hasDefault]\n }\n\n protected _with<\n _IsNullable extends boolean = IsNullable,\n _IsOptional extends boolean = IsOptional,\n _HasDefault extends boolean = HasDefault,\n >({\n options = this.options as Options,\n isNullable = this.isNullable as unknown as _IsNullable,\n isOptional = this.isOptional as unknown as _IsOptional,\n hasDefault = this.hasDefault as unknown as _HasDefault,\n }: {\n options?: Options\n isNullable?: _IsNullable\n isOptional?: _IsOptional\n hasDefault?: _HasDefault\n } = {}): [Options, _IsNullable, _IsOptional, _HasDefault] {\n return [{ ...this.options, ...options }, isNullable, isOptional, hasDefault]\n }\n\n abstract optional(): BaseType<Schema, IsNullable, true, HasDefault>\n abstract nullish(): BaseType<Schema, true, true, HasDefault>\n abstract default(value: any): BaseType<Schema, IsNullable, IsOptional, true>\n abstract description(\n value: string,\n ): BaseType<Schema, IsNullable, IsOptional, HasDefault>\n abstract examples(\n ...values: any[]\n ): BaseType<Schema, IsNullable, IsOptional, HasDefault>\n}\n\nexport function getTypeSchema<T extends BaseType>(\n type: T,\n): T[typeStatic]['schema'] {\n return type[typeSchema]\n}\n"],"names":["Type","typeSchema","typeStatic","Nullable","BaseType","constructor","options","isNullable","isOptional","hasDefault","contstructArgs","schema","_constructSchema","Optional","_args","_with","getTypeSchema","type"],"mappings":"AAAA,SAOEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,UAAU,EAAEC,UAAU,QAAQ,kBAAiB;AACxD,SAASC,QAAQ,QAAwB,yBAAwB;AAgBjE,OAAO,MAAeC;;;;;IAYpB,CAACF,WAAW,CASX;IAEDG,YACE,AAAUC,UAAmB,CAAC,CAAY,EAC1C,AAAUC,aAAyB,KAAmB,EACtD,AAAUC,aAAyB,KAAmB,EACtD,AAAUC,aAAyB,KAAmB,EACtD,GAAGC,cAAqB,CACxB;aALUJ,UAAAA;aACAC,aAAAA;aACAC,aAAAA;aACAC,aAAAA;QAGV,IAAIE,SAAkB,IAAI,CAACC,gBAAgB,CAACN,YAAYI;QACxD,IAAI,IAAI,CAACH,UAAU,EAAE;YACnBI,SAASR,SAASQ;QACpB;QACA,IAAI,IAAI,CAACH,UAAU,EAAE;YACnBG,SAASX,KAAKa,QAAQ,CAACF;QACzB;QACA,IAAI,CAACV,WAAW,GAAGU;IACrB;IACU,CAACV,WAAW,CAAQ;IAE9B,IAAca,QAA8C;QAC1D,OAAO;YAAC,IAAI,CAACP,UAAU;YAAE,IAAI,CAACC,UAAU;YAAE,IAAI,CAACC,UAAU;SAAC;IAC5D;IAEUM,MAIR,EACAT,UAAU,IAAI,CAACA,OAAO,AAAW,EACjCC,aAAa,IAAI,CAACA,UAAU,AAA0B,EACtDC,aAAa,IAAI,CAACA,UAAU,AAA0B,EACtDC,aAAa,IAAI,CAACA,UAAU,AAA0B,EAMvD,GAAG,CAAC,CAAC,EAAoD;QACxD,OAAO;YAAC;gBAAE,GAAG,IAAI,CAACH,OAAO;gBAAE,GAAGA,OAAO;YAAC;YAAGC;YAAYC;YAAYC;SAAW;IAC9E;AAWF;AAEA,OAAO,SAASO,cACdC,IAAO;IAEP,OAAOA,IAAI,CAAChB,WAAW;AACzB"}
|
package/dist/types/boolean.js
CHANGED
|
@@ -1,16 +1,48 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
3
|
export class BooleanType extends BaseType {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
4
|
+
constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
|
|
5
|
+
super(options, isNullable, isOptional, hasDefault);
|
|
6
|
+
}
|
|
7
|
+
_constructSchema(options) {
|
|
8
|
+
return Type.Boolean(options);
|
|
6
9
|
}
|
|
7
10
|
nullable() {
|
|
8
|
-
return new BooleanType(...this.
|
|
11
|
+
return new BooleanType(...this._with({
|
|
12
|
+
isNullable: true
|
|
13
|
+
}));
|
|
9
14
|
}
|
|
10
15
|
optional() {
|
|
11
|
-
return new BooleanType(...this.
|
|
16
|
+
return new BooleanType(...this._with({
|
|
17
|
+
isOptional: true
|
|
18
|
+
}));
|
|
12
19
|
}
|
|
13
20
|
nullish() {
|
|
14
|
-
return new BooleanType(...this.
|
|
21
|
+
return new BooleanType(...this._with({
|
|
22
|
+
isNullable: true,
|
|
23
|
+
isOptional: true
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
default(value) {
|
|
27
|
+
return new BooleanType(...this._with({
|
|
28
|
+
options: {
|
|
29
|
+
default: value
|
|
30
|
+
},
|
|
31
|
+
hasDefault: true
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
description(description) {
|
|
35
|
+
return new BooleanType(...this._with({
|
|
36
|
+
options: {
|
|
37
|
+
description
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
examples(...examples) {
|
|
42
|
+
return new BooleanType(...this._with({
|
|
43
|
+
options: {
|
|
44
|
+
examples
|
|
45
|
+
}
|
|
46
|
+
}));
|
|
15
47
|
}
|
|
16
48
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/boolean.ts"],"sourcesContent":["import { type TBoolean, Type } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class BooleanType<\n N extends boolean = false,\n O extends boolean = false,\n> extends BaseType<TBoolean, N, O> {\n constructor(\n
|
|
1
|
+
{"version":3,"sources":["../../../src/types/boolean.ts"],"sourcesContent":["import { type SchemaOptions, type TBoolean, Type } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class BooleanType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TBoolean, N, O, D> {\n constructor(\n options: SchemaOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(options: SchemaOptions): TBoolean {\n return Type.Boolean(options)\n }\n\n nullable() {\n return new BooleanType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new BooleanType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new BooleanType(\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: boolean) {\n return new BooleanType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new BooleanType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [boolean, ...boolean[]]) {\n return new BooleanType(...this._with({ options: { examples } }))\n }\n}\n"],"names":["Type","BaseType","BooleanType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","Boolean","nullable","_with","optional","nullish","default","value","description","examples"],"mappings":"AAAA,SAA4CA,IAAI,QAAQ,oBAAmB;AAC3E,SAASC,QAAQ,QAAQ,YAAW;AAEpC,OAAO,MAAMC,oBAIHD;IACRE,YACEC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC;IACzC;IAEUC,iBAAiBJ,OAAsB,EAAY;QAC3D,OAAOJ,KAAKS,OAAO,CAACL;IACtB;IAEAM,WAAW;QACT,OAAO,IAAIR,eAAe,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;QAAK;IAC1D;IAEAO,WAAW;QACT,OAAO,IAAIV,eAAe,IAAI,CAACS,KAAK,CAAC;YAAEL,YAAY;QAAK;IAC1D;IAEAO,UAAU;QACR,OAAO,IAAIX,eACN,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAQ,QAAQC,KAAc,EAAE;QACtB,OAAO,IAAIb,eACN,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEU,SAASC;YAAM;YAAGR,YAAY;QAAK;IAElE;IAEAS,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAId,eAAe,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEY;YAAY;QAAE;IAClE;IAEAC,SAAS,GAAGA,QAAiC,EAAE;QAC7C,OAAO,IAAIf,eAAe,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEa;YAAS;QAAE;IAC/D;AACF"}
|
package/dist/types/custom.js
CHANGED
|
@@ -3,21 +3,50 @@ import { BaseType } from "./base.js";
|
|
|
3
3
|
export class CustomType extends BaseType {
|
|
4
4
|
decode;
|
|
5
5
|
encode;
|
|
6
|
-
constructor(decode, encode,
|
|
7
|
-
super(
|
|
6
|
+
constructor(decode, encode, options = {}, isNullable = false, isOptional = false, hasDefault = false){
|
|
7
|
+
super(options, isNullable, isOptional, hasDefault, decode, encode);
|
|
8
8
|
this.decode = decode;
|
|
9
9
|
this.encode = encode;
|
|
10
10
|
}
|
|
11
|
+
_constructSchema(options, decode, encode) {
|
|
12
|
+
return Type.Transform(Type.Any(options)).Decode(decode).Encode(encode);
|
|
13
|
+
}
|
|
11
14
|
nullable() {
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
return new CustomType(this.decode, this.encode, ...this._with({
|
|
16
|
+
isNullable: true
|
|
17
|
+
}));
|
|
14
18
|
}
|
|
15
19
|
optional() {
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
return new CustomType(this.decode, this.encode, ...this._with({
|
|
21
|
+
isOptional: true
|
|
22
|
+
}));
|
|
18
23
|
}
|
|
19
24
|
nullish() {
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
return new CustomType(this.decode, this.encode, ...this._with({
|
|
26
|
+
isNullable: true,
|
|
27
|
+
isOptional: true
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
default(value) {
|
|
31
|
+
return new CustomType(this.decode, this.encode, ...this._with({
|
|
32
|
+
options: {
|
|
33
|
+
default: this.encode(value)
|
|
34
|
+
},
|
|
35
|
+
hasDefault: true
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
description(description) {
|
|
39
|
+
return new CustomType(this.decode, this.encode, ...this._with({
|
|
40
|
+
options: {
|
|
41
|
+
description
|
|
42
|
+
}
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
examples(...examples) {
|
|
46
|
+
return new CustomType(this.decode, this.encode, ...this._with({
|
|
47
|
+
options: {
|
|
48
|
+
examples: examples.map(this.encode)
|
|
49
|
+
}
|
|
50
|
+
}));
|
|
22
51
|
}
|
|
23
52
|
}
|
package/dist/types/custom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/custom.ts"],"sourcesContent":["import { type TTransform
|
|
1
|
+
{"version":3,"sources":["../../../src/types/custom.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TTransform,\n type TUnsafe,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\ntype CustomDecode<T> = (value: any) => T\ntype CustomEncode<T> = (value: T) => any\n\nexport class CustomType<\n T,\n S = T,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TTransform<TUnsafe<S>, T>, N, O, D> {\n constructor(\n protected readonly decode: CustomDecode<T>,\n protected readonly encode: CustomEncode<T>,\n options: SchemaOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault, decode, encode)\n }\n\n protected _constructSchema(\n options: SchemaOptions,\n decode: CustomDecode<T>,\n encode: CustomEncode<T>,\n ): TTransform<TUnsafe<S>, T> {\n return Type.Transform(Type.Any(options) as unknown as TUnsafe<S>)\n .Decode(decode)\n .Encode(encode)\n }\n\n nullable() {\n return new CustomType<T, S, true, O, D>(\n this.decode,\n this.encode,\n ...this._with({ isNullable: true }),\n )\n }\n\n optional() {\n return new CustomType<T, S, N, true, D>(\n this.decode,\n this.encode,\n ...this._with({ isOptional: true }),\n )\n }\n\n nullish() {\n return new CustomType<T, S, true, true, D>(\n this.decode,\n this.encode,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: T) {\n return new CustomType<T, S, N, O, true>(\n this.decode,\n this.encode,\n ...this._with({\n options: { default: this.encode(value) },\n hasDefault: true,\n }),\n )\n }\n\n description(description: string) {\n return new CustomType<T, S, N, O, D>(\n this.decode,\n this.encode,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: [T, ...T[]]) {\n return new CustomType<T, S, N, O, D>(\n this.decode,\n this.encode,\n ...this._with({ options: { examples: examples.map(this.encode) } }),\n )\n }\n}\n"],"names":["Type","BaseType","CustomType","constructor","decode","encode","options","isNullable","isOptional","hasDefault","_constructSchema","Transform","Any","Decode","Encode","nullable","_with","optional","nullish","default","value","description","examples","map"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAAQ,YAAW;AAKpC,OAAO,MAAMC,mBAMHD;;;IACRE,YACE,AAAmBC,MAAuB,EAC1C,AAAmBC,MAAuB,EAC1CC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYL,QAAQC;aAPxCD,SAAAA;aACAC,SAAAA;IAOrB;IAEUK,iBACRJ,OAAsB,EACtBF,MAAuB,EACvBC,MAAuB,EACI;QAC3B,OAAOL,KAAKW,SAAS,CAACX,KAAKY,GAAG,CAACN,UAC5BO,MAAM,CAACT,QACPU,MAAM,CAACT;IACZ;IAEAU,WAAW;QACT,OAAO,IAAIb,WACT,IAAI,CAACE,MAAM,EACX,IAAI,CAACC,MAAM,KACR,IAAI,CAACW,KAAK,CAAC;YAAET,YAAY;QAAK;IAErC;IAEAU,WAAW;QACT,OAAO,IAAIf,WACT,IAAI,CAACE,MAAM,EACX,IAAI,CAACC,MAAM,KACR,IAAI,CAACW,KAAK,CAAC;YAAER,YAAY;QAAK;IAErC;IAEAU,UAAU;QACR,OAAO,IAAIhB,WACT,IAAI,CAACE,MAAM,EACX,IAAI,CAACC,MAAM,KACR,IAAI,CAACW,KAAK,CAAC;YAAET,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAW,QAAQC,KAAQ,EAAE;QAChB,OAAO,IAAIlB,WACT,IAAI,CAACE,MAAM,EACX,IAAI,CAACC,MAAM,KACR,IAAI,CAACW,KAAK,CAAC;YACZV,SAAS;gBAAEa,SAAS,IAAI,CAACd,MAAM,CAACe;YAAO;YACvCX,YAAY;QACd;IAEJ;IAEAY,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAInB,WACT,IAAI,CAACE,MAAM,EACX,IAAI,CAACC,MAAM,KACR,IAAI,CAACW,KAAK,CAAC;YAAEV,SAAS;gBAAEe;YAAY;QAAE;IAE7C;IAEAC,SAAS,GAAGA,QAAqB,EAAE;QACjC,OAAO,IAAIpB,WACT,IAAI,CAACE,MAAM,EACX,IAAI,CAACC,MAAM,KACR,IAAI,CAACW,KAAK,CAAC;YAAEV,SAAS;gBAAEgB,UAAUA,SAASC,GAAG,CAAC,IAAI,CAAClB,MAAM;YAAE;QAAE;IAErE;AACF"}
|