@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.
Files changed (66) hide show
  1. package/dist/compiler.js +84 -38
  2. package/dist/compiler.js.map +1 -1
  3. package/dist/index.js +53 -22
  4. package/dist/index.js.map +1 -1
  5. package/dist/schemas/discriminated-union.js +9 -0
  6. package/dist/schemas/discriminated-union.js.map +1 -0
  7. package/dist/schemas/nullable.js +1 -6
  8. package/dist/schemas/nullable.js.map +1 -1
  9. package/dist/temporal.js +7 -7
  10. package/dist/temporal.js.map +1 -1
  11. package/dist/types/any.js +3 -43
  12. package/dist/types/any.js.map +1 -1
  13. package/dist/types/array.js +17 -63
  14. package/dist/types/array.js.map +1 -1
  15. package/dist/types/base.js +78 -41
  16. package/dist/types/base.js.map +1 -1
  17. package/dist/types/boolean.js +3 -43
  18. package/dist/types/boolean.js.map +1 -1
  19. package/dist/types/custom.js +8 -48
  20. package/dist/types/custom.js.map +1 -1
  21. package/dist/types/date.js +8 -0
  22. package/dist/types/date.js.map +1 -0
  23. package/dist/types/enum.js +10 -94
  24. package/dist/types/enum.js.map +1 -1
  25. package/dist/types/literal.js +3 -43
  26. package/dist/types/literal.js.map +1 -1
  27. package/dist/types/never.js +3 -26
  28. package/dist/types/never.js.map +1 -1
  29. package/dist/types/number.js +52 -186
  30. package/dist/types/number.js.map +1 -1
  31. package/dist/types/object.js +10 -131
  32. package/dist/types/object.js.map +1 -1
  33. package/dist/types/string.js +25 -65
  34. package/dist/types/string.js.map +1 -1
  35. package/dist/types/temporal.js +23 -328
  36. package/dist/types/temporal.js.map +1 -1
  37. package/dist/types/union.js +16 -90
  38. package/dist/types/union.js.map +1 -1
  39. package/dist/utils.js.map +1 -1
  40. package/package.json +3 -3
  41. package/src/compiler.ts +124 -41
  42. package/src/index.ts +145 -63
  43. package/src/schemas/discriminated-union.ts +49 -0
  44. package/src/schemas/nullable.ts +7 -13
  45. package/src/temporal.ts +8 -7
  46. package/src/types/any.ts +6 -46
  47. package/src/types/array.ts +38 -86
  48. package/src/types/base.ts +205 -81
  49. package/src/types/boolean.ts +13 -47
  50. package/src/types/custom.ts +21 -79
  51. package/src/types/date.ts +10 -0
  52. package/src/types/enum.ts +18 -107
  53. package/src/types/literal.ts +7 -63
  54. package/src/types/never.ts +6 -36
  55. package/src/types/number.ts +52 -188
  56. package/src/types/object.ts +61 -202
  57. package/src/types/string.ts +25 -61
  58. package/src/types/temporal.ts +53 -410
  59. package/src/types/union.ts +98 -138
  60. package/src/utils.ts +8 -0
  61. package/dist/constants.js +0 -2
  62. package/dist/constants.js.map +0 -1
  63. package/dist/types/datetime.js +0 -53
  64. package/dist/types/datetime.js.map +0 -1
  65. package/src/constants.ts +0 -5
  66. package/src/types/datetime.ts +0 -65
@@ -1,46 +1,83 @@
1
- import { Type } from '@sinclair/typebox';
2
- import { typeSchema, typeStatic } from "../constants.js";
1
+ import { Optional } from '@sinclair/typebox';
3
2
  import { Nullable } from "../schemas/nullable.js";
4
3
  export class BaseType {
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) {
17
- schema = Nullable(schema);
18
- }
19
- if (this.isOptional) {
20
- schema = Type.Optional(schema);
21
- }
22
- this[typeSchema] = schema;
23
- }
24
- [typeSchema];
25
- get _args() {
26
- return [
27
- this.isNullable,
28
- this.isOptional,
29
- this.hasDefault
30
- ];
31
- }
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
- ];
4
+ schema;
5
+ final;
6
+ props;
7
+ params;
8
+ constructor(schema, props = {}, params = {}){
9
+ const { hasDefault = false, nullable = false, optional = false } = params;
10
+ this.schema = schema;
11
+ this.final = schema;
12
+ if (nullable) this.final = Nullable(this.final);
13
+ if (optional || hasDefault) this.final = Optional(this.final);
14
+ this.props = props;
15
+ this.params = {
16
+ hasDefault,
17
+ nullable,
18
+ optional
19
+ };
20
+ }
21
+ optional() {
22
+ return OptionalType.factory(this);
23
+ }
24
+ nullable() {
25
+ return NullableType.factory(this);
26
+ }
27
+ nullish() {
28
+ return this.nullable().optional();
29
+ }
30
+ default(value) {
31
+ return DefaultType.factory(this, this.params.encode?.(value) ?? value);
32
+ }
33
+ description(description) {
34
+ const ThisConstructor = this.constructor;
35
+ return new ThisConstructor({
36
+ ...this.schema,
37
+ description
38
+ }, this.props, this.params);
39
+ }
40
+ examples(...examples) {
41
+ const ThisConstructor = this.constructor;
42
+ return new ThisConstructor({
43
+ ...this.schema,
44
+ examples
45
+ }, this.props, this.params);
42
46
  }
43
47
  }
44
- export function getTypeSchema(type) {
45
- return type[typeSchema];
48
+ export class OptionalType extends BaseType {
49
+ _;
50
+ static factory(type) {
51
+ return new OptionalType(type.schema, {
52
+ inner: type
53
+ }, {
54
+ ...type.params,
55
+ optional: true
56
+ });
57
+ }
58
+ }
59
+ export class NullableType extends BaseType {
60
+ _;
61
+ static factory(type) {
62
+ return new NullableType(type.schema, {
63
+ inner: type
64
+ }, {
65
+ ...type.params,
66
+ nullable: true
67
+ });
68
+ }
69
+ }
70
+ export class DefaultType extends BaseType {
71
+ _;
72
+ static factory(type, defaultValue) {
73
+ return new DefaultType({
74
+ ...type.schema,
75
+ default: defaultValue
76
+ }, {
77
+ inner: type
78
+ }, {
79
+ ...type.params,
80
+ hasDefault: true
81
+ });
82
+ }
46
83
  }
@@ -1 +1 @@
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<\n Schema,\n IsNullable,\n HasDefault extends true ? false : IsOptional\n >\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 true ? 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,CAaX;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"}
1
+ {"version":3,"sources":["../../../src/types/base.ts"],"sourcesContent":["import {\n Optional,\n type SchemaOptions,\n type StaticDecode,\n type StaticEncode,\n type TSchema,\n} from '@sinclair/typebox'\nimport {\n Nullable,\n type TNullable,\n type TOptionalUndefined,\n} from '../schemas/nullable.ts'\nimport type { Merge } from '../utils.ts'\n\nexport type TypeProps = Record<string, any>\n\nexport type TypeParams = {\n optional?: boolean\n nullable?: boolean\n hasDefault?: boolean\n encode?: (value: any) => any\n}\n\nexport type DefaultTypeParams = {\n optional: false\n nullable: false\n hasDefault: false\n encode?: TypeParams['encode']\n}\n\nexport type BaseTypeAny<T extends TSchema = TSchema> = BaseType<T, any, any>\n\ntype ResolveNullable<\n T extends TSchema,\n P extends TypeParams,\n> = P['nullable'] extends true\n ? T extends TNullable<infer S>\n ? TNullable<S>\n : TNullable<T>\n : T\n\ntype ResolveOptional<\n T extends TSchema,\n P extends TypeParams,\n> = P['optional'] extends true\n ? T extends TOptionalUndefined<infer S>\n ? TOptionalUndefined<S>\n : TOptionalUndefined<T>\n : T\n\ntype ResolveDefault<\n T extends TSchema,\n P extends TypeParams,\n> = P['hasDefault'] extends true\n ? T extends TOptionalUndefined<infer U>\n ? U\n : T\n : T\n\nexport abstract class BaseType<\n Schema extends TSchema = TSchema,\n Props extends TypeProps = TypeProps,\n Params extends TypeParams = DefaultTypeParams,\n> {\n abstract _: {\n encoded: {\n input: TSchema\n output: TSchema\n }\n decoded: {\n input: TSchema\n output: TSchema\n }\n }\n\n readonly schema: Schema\n readonly final: TSchema\n readonly props: Props\n readonly params: Params\n\n constructor(\n schema: Schema,\n props: Props = {} as Props,\n params: Params = {} as Params,\n ) {\n const { hasDefault = false, nullable = false, optional = false } = params\n this.schema = schema\n this.final = schema\n if (nullable) this.final = Nullable(this.final) as any\n if (optional || hasDefault) this.final = Optional(this.final) as any\n\n this.props = props\n this.params = {\n hasDefault,\n nullable,\n optional,\n } as Params\n }\n\n optional(): OptionalType<this> {\n return OptionalType.factory(this) as any\n }\n\n nullable(): NullableType<this> {\n return NullableType.factory(this) as any\n }\n\n nullish() {\n return this.nullable().optional()\n }\n\n default(\n value: StaticDecode<this['_']['decoded']['output']>,\n ): DefaultType<this> {\n return DefaultType.factory(\n this,\n this.params.encode?.(value) ?? value,\n ) as any\n }\n\n description(description: string): this {\n const ThisConstructor = this.constructor as any\n return new ThisConstructor(\n {\n ...this.schema,\n description,\n },\n this.props,\n this.params,\n ) as any\n }\n\n examples(...examples: any[]): this {\n const ThisConstructor = this.constructor as any\n return new ThisConstructor(\n {\n ...this.schema,\n examples,\n },\n this.props,\n this.params,\n ) as any\n }\n}\n\nexport type ConstantType<T extends TSchema> = {\n encoded: {\n input: T\n output: T\n }\n decoded: {\n input: T\n output: T\n }\n}\n\nexport type Static<\n T extends BaseTypeAny,\n P extends TypeProps,\n Params extends Merge<T['params'], P> = Merge<T['params'], P>,\n> = {\n encoded: {\n input: ResolveOptional<\n ResolveNullable<T['_']['encoded']['input'], Params>,\n Params\n >\n output: ResolveDefault<\n ResolveOptional<\n ResolveNullable<T['_']['encoded']['output'], Params>,\n Params\n >,\n Params\n >\n }\n decoded: {\n input: ResolveOptional<\n ResolveNullable<T['_']['decoded']['input'], Params>,\n Params\n >\n output: ResolveDefault<\n ResolveOptional<\n ResolveNullable<T['_']['decoded']['output'], Params>,\n Params\n >,\n Params\n >\n }\n}\n\nexport class OptionalType<\n Type extends BaseTypeAny<any>,\n Params extends TypeParams = DefaultTypeParams,\n> extends BaseType<Type['schema'], { inner: Type }, Params> {\n _!: Static<Type, Params>\n\n static factory<T extends BaseTypeAny<any>>(type: T) {\n return new OptionalType<T, Merge<T['params'], { optional: true }>>(\n type.schema,\n { inner: type },\n { ...type.params, optional: true } as any,\n )\n }\n}\n\nexport class NullableType<\n Type extends BaseTypeAny<any>,\n Params extends TypeParams = DefaultTypeParams,\n> extends BaseType<Type['schema'], { inner: Type }, Params> {\n _!: Static<Type, Params>\n\n static factory<T extends BaseTypeAny<any>>(type: T) {\n return new NullableType<T, Merge<T['params'], { nullable: true }>>(\n type.schema,\n { inner: type },\n { ...type.params, nullable: true } as any,\n )\n }\n}\n\nexport class DefaultType<\n Type extends BaseTypeAny<any>,\n Params extends TypeParams = DefaultTypeParams,\n> extends BaseType<Type['schema'], { inner: Type }, Params> {\n _!: Static<Type, Params>\n\n static factory<T extends BaseTypeAny<any>>(type: T, defaultValue: any) {\n return new DefaultType<T, Merge<T['params'], { hasDefault: true }>>(\n { ...type.schema, default: defaultValue },\n { inner: type },\n { ...type.params, hasDefault: true } as any,\n )\n }\n}\n"],"names":["Optional","Nullable","BaseType","schema","final","props","params","constructor","hasDefault","nullable","optional","OptionalType","factory","NullableType","nullish","default","value","DefaultType","encode","description","ThisConstructor","examples","_","type","inner","defaultValue"],"mappings":"AAAA,SACEA,QAAQ,QAKH,oBAAmB;AAC1B,SACEC,QAAQ,QAGH,yBAAwB;AAgD/B,OAAO,MAAeC;IAgBXC,OAAc;IACdC,MAAc;IACdC,MAAY;IACZC,OAAc;IAEvBC,YACEJ,MAAc,EACdE,QAAe,CAAC,CAAU,EAC1BC,SAAiB,CAAC,CAAW,CAC7B;QACA,MAAM,EAAEE,aAAa,KAAK,EAAEC,WAAW,KAAK,EAAEC,WAAW,KAAK,EAAE,GAAGJ;QACnE,IAAI,CAACH,MAAM,GAAGA;QACd,IAAI,CAACC,KAAK,GAAGD;QACb,IAAIM,UAAU,IAAI,CAACL,KAAK,GAAGH,SAAS,IAAI,CAACG,KAAK;QAC9C,IAAIM,YAAYF,YAAY,IAAI,CAACJ,KAAK,GAAGJ,SAAS,IAAI,CAACI,KAAK;QAE5D,IAAI,CAACC,KAAK,GAAGA;QACb,IAAI,CAACC,MAAM,GAAG;YACZE;YACAC;YACAC;QACF;IACF;IAEAA,WAA+B;QAC7B,OAAOC,aAAaC,OAAO,CAAC,IAAI;IAClC;IAEAH,WAA+B;QAC7B,OAAOI,aAAaD,OAAO,CAAC,IAAI;IAClC;IAEAE,UAAU;QACR,OAAO,IAAI,CAACL,QAAQ,GAAGC,QAAQ;IACjC;IAEAK,QACEC,KAAmD,EAChC;QACnB,OAAOC,YAAYL,OAAO,CACxB,IAAI,EACJ,IAAI,CAACN,MAAM,CAACY,MAAM,GAAGF,UAAUA;IAEnC;IAEAG,YAAYA,WAAmB,EAAQ;QACrC,MAAMC,kBAAkB,IAAI,CAACb,WAAW;QACxC,OAAO,IAAIa,gBACT;YACE,GAAG,IAAI,CAACjB,MAAM;YACdgB;QACF,GACA,IAAI,CAACd,KAAK,EACV,IAAI,CAACC,MAAM;IAEf;IAEAe,SAAS,GAAGA,QAAe,EAAQ;QACjC,MAAMD,kBAAkB,IAAI,CAACb,WAAW;QACxC,OAAO,IAAIa,gBACT;YACE,GAAG,IAAI,CAACjB,MAAM;YACdkB;QACF,GACA,IAAI,CAAChB,KAAK,EACV,IAAI,CAACC,MAAM;IAEf;AACF;AA8CA,OAAO,MAAMK,qBAGHT;IACRoB,EAAwB;IAExB,OAAOV,QAAoCW,IAAO,EAAE;QAClD,OAAO,IAAIZ,aACTY,KAAKpB,MAAM,EACX;YAAEqB,OAAOD;QAAK,GACd;YAAE,GAAGA,KAAKjB,MAAM;YAAEI,UAAU;QAAK;IAErC;AACF;AAEA,OAAO,MAAMG,qBAGHX;IACRoB,EAAwB;IAExB,OAAOV,QAAoCW,IAAO,EAAE;QAClD,OAAO,IAAIV,aACTU,KAAKpB,MAAM,EACX;YAAEqB,OAAOD;QAAK,GACd;YAAE,GAAGA,KAAKjB,MAAM;YAAEG,UAAU;QAAK;IAErC;AACF;AAEA,OAAO,MAAMQ,oBAGHf;IACRoB,EAAwB;IAExB,OAAOV,QAAoCW,IAAO,EAAEE,YAAiB,EAAE;QACrE,OAAO,IAAIR,YACT;YAAE,GAAGM,KAAKpB,MAAM;YAAEY,SAASU;QAAa,GACxC;YAAED,OAAOD;QAAK,GACd;YAAE,GAAGA,KAAKjB,MAAM;YAAEE,YAAY;QAAK;IAEvC;AACF"}
@@ -1,48 +1,8 @@
1
1
  import { Type } from '@sinclair/typebox';
2
2
  import { BaseType } from "./base.js";
3
3
  export class BooleanType extends BaseType {
4
- constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
5
- super(options, isNullable, isOptional, hasDefault);
6
- }
7
- _constructSchema(options) {
8
- return Type.Boolean(options);
9
- }
10
- nullable() {
11
- return new BooleanType(...this._with({
12
- isNullable: true
13
- }));
14
- }
15
- optional() {
16
- return new BooleanType(...this._with({
17
- isOptional: true
18
- }));
19
- }
20
- nullish() {
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
- }));
4
+ _;
5
+ static factory() {
6
+ return new BooleanType(Type.Boolean());
47
7
  }
48
8
  }
@@ -1 +1 @@
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"}
1
+ {"version":3,"sources":["../../../src/types/boolean.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type StaticDecode,\n type TBoolean,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport class BooleanType extends BaseType<TBoolean> {\n _!: ConstantType<this['schema']>\n\n static factory() {\n return new BooleanType(Type.Boolean())\n }\n}\n"],"names":["Type","BaseType","BooleanType","_","factory","Boolean"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAA2B,YAAW;AAEvD,OAAO,MAAMC,oBAAoBD;IAC/BE,EAAgC;IAEhC,OAAOC,UAAU;QACf,OAAO,IAAIF,YAAYF,KAAKK,OAAO;IACrC;AACF"}
@@ -1,52 +1,12 @@
1
1
  import { Type } from '@sinclair/typebox';
2
2
  import { BaseType } from "./base.js";
3
- export class CustomType extends BaseType {
4
- decode;
5
- encode;
6
- constructor(decode, encode, options = {}, isNullable = false, isOptional = false, hasDefault = false){
7
- super(options, isNullable, isOptional, hasDefault, decode, encode);
8
- this.decode = decode;
9
- this.encode = encode;
10
- }
11
- _constructSchema(options, decode, encode) {
12
- return Type.Transform(Type.Any(options)).Decode(decode).Encode(encode);
13
- }
14
- nullable() {
15
- return new CustomType(this.decode, this.encode, ...this._with({
16
- isNullable: true
17
- }));
18
- }
19
- optional() {
20
- return new CustomType(this.decode, this.encode, ...this._with({
21
- isOptional: true
22
- }));
23
- }
24
- nullish() {
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
- }));
3
+ export class TransformType extends BaseType {
4
+ _;
5
+ }
6
+ export class CustomType extends TransformType {
7
+ static factory(decode, encode, schema = Type.Any()) {
8
+ return new CustomType(Type.Transform(schema).Decode(decode).Encode(encode), {}, {
9
+ encode
10
+ });
51
11
  }
52
12
  }
@@ -1 +1 @@
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"}
1
+ {"version":3,"sources":["../../../src/types/custom.ts"],"sourcesContent":["import {\n type TAny,\n type TTransform,\n type TUnsafe,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport type CustomTypeDecode<T> = (value: any) => T\nexport type CustomTypeEncode<T> = (value: T) => any\n\nexport abstract class TransformType<T, S = TAny> extends BaseType<\n TTransform<TUnsafe<S>, T>\n> {\n _!: ConstantType<this['schema']>\n}\n\nexport class CustomType<T, S = TAny> extends TransformType<T, S> {\n static factory<T, S = TAny>(\n decode: CustomTypeDecode<T>,\n encode: CustomTypeEncode<T>,\n schema = Type.Any() as unknown as TUnsafe<S>,\n ) {\n return new CustomType<T, S>(\n Type.Transform(schema as unknown as TUnsafe<S>)\n .Decode(decode)\n .Encode(encode),\n {},\n { encode } as any,\n )\n }\n}\n"],"names":["Type","BaseType","TransformType","_","CustomType","factory","decode","encode","schema","Any","Transform","Decode","Encode"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAA2B,YAAW;AAKvD,OAAO,MAAeC,sBAAmCD;IAGvDE,EAAgC;AAClC;AAEA,OAAO,MAAMC,mBAAgCF;IAC3C,OAAOG,QACLC,MAA2B,EAC3BC,MAA2B,EAC3BC,SAASR,KAAKS,GAAG,EAA2B,EAC5C;QACA,OAAO,IAAIL,WACTJ,KAAKU,SAAS,CAACF,QACZG,MAAM,CAACL,QACPM,MAAM,CAACL,SACV,CAAC,GACD;YAAEA;QAAO;IAEb;AACF"}
@@ -0,0 +1,8 @@
1
+ import { CustomType, TransformType } from "./custom.js";
2
+ const decode = (value)=>new Date(value);
3
+ const encode = (value)=>value.toISOString();
4
+ export class DateType extends TransformType {
5
+ static factory() {
6
+ return CustomType.factory(decode, encode);
7
+ }
8
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/types/date.ts"],"sourcesContent":["import { CustomType, TransformType } from './custom.ts'\n\nconst decode = (value: any): Date => new Date(value)\nconst encode = (value: Date): any => value.toISOString()\n\nexport class DateType extends TransformType<Date> {\n static factory() {\n return CustomType.factory<Date>(decode, encode)\n }\n}\n"],"names":["CustomType","TransformType","decode","value","Date","encode","toISOString","DateType","factory"],"mappings":"AAAA,SAASA,UAAU,EAAEC,aAAa,QAAQ,cAAa;AAEvD,MAAMC,SAAS,CAACC,QAAqB,IAAIC,KAAKD;AAC9C,MAAME,SAAS,CAACF,QAAqBA,MAAMG,WAAW;AAEtD,OAAO,MAAMC,iBAAiBN;IAC5B,OAAOO,UAAU;QACf,OAAOR,WAAWQ,OAAO,CAAON,QAAQG;IAC1C;AACF"}
@@ -1,101 +1,17 @@
1
- import { Enum } from '@sinclair/typebox';
1
+ import { Type } from '@sinclair/typebox';
2
2
  import { BaseType } from "./base.js";
3
3
  export class ObjectEnumType extends BaseType {
4
- values;
5
- constructor(values, options = {}, isNullable = false, isOptional = false, hasDefault = false){
6
- super(options, isNullable, isOptional, hasDefault, values);
7
- this.values = values;
8
- }
9
- _constructSchema(options, values) {
10
- return Enum(values, options);
11
- }
12
- nullable() {
13
- return new ObjectEnumType(this.values, ...this._with({
14
- isNullable: true
15
- }));
16
- }
17
- optional() {
18
- return new ObjectEnumType(this.values, ...this._with({
19
- isOptional: true
20
- }));
21
- }
22
- nullish() {
23
- return new ObjectEnumType(this.values, ...this._with({
24
- isNullable: true,
25
- isOptional: true
26
- }));
27
- }
28
- default(value) {
29
- return new ObjectEnumType(this.values, ...this._with({
30
- options: {
31
- default: value
32
- },
33
- hasDefault: true
34
- }));
35
- }
36
- description(description) {
37
- return new ObjectEnumType(this.values, ...this._with({
38
- options: {
39
- description
40
- }
41
- }));
42
- }
43
- examples(...examples) {
44
- return new ObjectEnumType(this.values, ...this._with({
45
- options: {
46
- examples
47
- }
48
- }));
4
+ _;
5
+ static factory(values) {
6
+ return new ObjectEnumType(Type.Enum(values));
49
7
  }
50
8
  }
51
9
  export class EnumType extends BaseType {
52
- values;
53
- constructor(values, options = {}, isNullable = false, isOptional = false, hasDefault = false){
54
- super(options, isNullable, isOptional, hasDefault, values);
55
- this.values = values;
56
- }
57
- _constructSchema(options, values) {
58
- return Enum(Object.fromEntries(values.map((k)=>[
59
- k,
60
- k
61
- ])), options);
62
- }
63
- nullable() {
64
- return new EnumType(this.values, ...this._with({
65
- isNullable: true
66
- }));
67
- }
68
- optional() {
69
- return new EnumType(this.values, ...this._with({
70
- isOptional: true
71
- }));
72
- }
73
- nullish() {
74
- return new EnumType(this.values, ...this._with({
75
- isNullable: true,
76
- isOptional: true
77
- }));
78
- }
79
- default(value) {
80
- return new EnumType(this.values, ...this._with({
81
- options: {
82
- default: value
83
- },
84
- hasDefault: true
85
- }));
86
- }
87
- description(description) {
88
- return new EnumType(this.values, ...this._with({
89
- options: {
90
- description
91
- }
92
- }));
93
- }
94
- examples(...examples) {
95
- return new EnumType(this.values, ...this._with({
96
- options: {
97
- examples
98
- }
99
- }));
10
+ _;
11
+ static factory(values) {
12
+ return new EnumType(Type.Enum(Object.fromEntries(values.map((v)=>[
13
+ v,
14
+ v
15
+ ]))));
100
16
  }
101
17
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import { Enum, type SchemaOptions, type TEnum } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport type AnyObjectEnumType<T extends { [K in string]: K } = any> =\n ObjectEnumType<T, boolean, boolean, boolean>\nexport class ObjectEnumType<\n T extends { [K in string]: K },\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TEnum<T>, N, O, D> {\n constructor(\n private readonly values: 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, values)\n }\n\n protected _constructSchema(options: SchemaOptions, values: T): TEnum<T> {\n return Enum(values, options)\n }\n\n nullable() {\n return new ObjectEnumType(this.values, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ObjectEnumType(this.values, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ObjectEnumType(\n this.values,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: keyof T) {\n return new ObjectEnumType(\n this.values,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new ObjectEnumType(\n this.values,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: (keyof T)[]) {\n return new ObjectEnumType(\n this.values,\n ...this._with({ options: { examples } }),\n )\n }\n}\n\nexport type AnyEnumType = EnumType<any, boolean, boolean, boolean>\nexport class EnumType<\n T extends (string | number)[] = (string | number)[],\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TEnum<Record<string, T[number]>>, N, O, D> {\n constructor(\n private readonly values: [...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, values)\n }\n\n protected _constructSchema(\n options: SchemaOptions,\n values: [...T],\n ): TEnum<Record<string, T[number]>> {\n return Enum(Object.fromEntries(values.map((k) => [k, k])), options)\n }\n\n nullable() {\n return new EnumType(this.values, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new EnumType(this.values, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new EnumType(\n this.values,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: T[number]) {\n return new EnumType(\n this.values,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new EnumType(\n this.values,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: [T[number], ...T[number][]]) {\n return new EnumType(this.values, ...this._with({ options: { examples } }))\n }\n}\n"],"names":["Enum","BaseType","ObjectEnumType","constructor","values","options","isNullable","isOptional","hasDefault","_constructSchema","nullable","_with","optional","nullish","default","value","description","examples","EnumType","Object","fromEntries","map","k"],"mappings":"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"}
1
+ {"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import {\n Enum,\n type SchemaOptions,\n type StaticDecode,\n type TEnum,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport class ObjectEnumType<\n T extends { [K in string]: K } = { [K in string]: K },\n> extends BaseType<TEnum<T>> {\n _!: ConstantType<this['schema']>\n\n static factory<T extends { [K in string]: K }>(values: T) {\n return new ObjectEnumType<T>(Type.Enum(values as any))\n }\n}\n\nexport class EnumType<\n T extends (string | number)[] = (string | number)[],\n> extends BaseType<TEnum<Record<string, T[number]>>> {\n _!: ConstantType<this['schema']>\n\n static factory<T extends (string | number)[]>(values: [...T]) {\n return new EnumType<T>(\n Type.Enum(Object.fromEntries(values.map((v) => [v, v])) as any),\n )\n }\n}\n"],"names":["Type","BaseType","ObjectEnumType","_","factory","values","Enum","EnumType","Object","fromEntries","map","v"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAA2B,YAAW;AAEvD,OAAO,MAAMC,uBAEHD;IACRE,EAAgC;IAEhC,OAAOC,QAAwCC,MAAS,EAAE;QACxD,OAAO,IAAIH,eAAkBF,KAAKM,IAAI,CAACD;IACzC;AACF;AAEA,OAAO,MAAME,iBAEHN;IACRE,EAAgC;IAEhC,OAAOC,QAAuCC,MAAc,EAAE;QAC5D,OAAO,IAAIE,SACTP,KAAKM,IAAI,CAACE,OAAOC,WAAW,CAACJ,OAAOK,GAAG,CAAC,CAACC,IAAM;gBAACA;gBAAGA;aAAE;IAEzD;AACF"}
@@ -1,48 +1,8 @@
1
1
  import { Type } from '@sinclair/typebox';
2
2
  import { BaseType } from "./base.js";
3
3
  export class LiteralType extends BaseType {
4
- value;
5
- constructor(value, options = {}, isNullable = false, isOptional = false, hasDefault = false){
6
- super(options, isNullable, isOptional, hasDefault, value);
7
- this.value = value;
8
- }
9
- _constructSchema(options, value) {
10
- return Type.Literal(value, options);
11
- }
12
- nullable() {
13
- return new LiteralType(this.value, this.options, true, this.isOptional);
14
- }
15
- optional() {
16
- return new LiteralType(this.value, ...this._with({
17
- isOptional: true
18
- }));
19
- }
20
- nullish() {
21
- return new LiteralType(this.value, ...this._with({
22
- isNullable: true,
23
- isOptional: true
24
- }));
25
- }
26
- default(value = this.value) {
27
- return new LiteralType(this.value, ...this._with({
28
- options: {
29
- default: value
30
- },
31
- hasDefault: true
32
- }));
33
- }
34
- description(description) {
35
- return new LiteralType(this.value, ...this._with({
36
- options: {
37
- description
38
- }
39
- }));
40
- }
41
- examples(...examples) {
42
- return new LiteralType(this.value, ...this._with({
43
- options: {
44
- examples
45
- }
46
- }));
4
+ _;
5
+ static factory(value) {
6
+ return new LiteralType(Type.Literal(value));
47
7
  }
48
8
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/types/literal.ts"],"sourcesContent":["import {\n Extends,\n type SchemaOptions,\n type TLiteral,\n type TLiteralValue,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport type AnyLiteralType<T extends TLiteralValue = any> = LiteralType<\n T,\n boolean,\n boolean,\n boolean\n>\nexport class LiteralType<\n T extends TLiteralValue,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TLiteral<T>, N, O, D> {\n constructor(\n protected readonly value: 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, value)\n }\n\n protected _constructSchema(options: SchemaOptions, value: T): TLiteral<T> {\n return Type.Literal(value, options)\n }\n\n nullable() {\n return new LiteralType(this.value, this.options, true, this.isOptional)\n }\n\n optional() {\n return new LiteralType(this.value, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new LiteralType(\n this.value,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: T = this.value) {\n return new LiteralType(\n this.value,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new LiteralType(\n this.value,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: [T, ...T[]]) {\n return new LiteralType(this.value, ...this._with({ options: { examples } }))\n }\n}\n"],"names":["Type","BaseType","LiteralType","constructor","value","options","isNullable","isOptional","hasDefault","_constructSchema","Literal","nullable","optional","_with","nullish","default","description","examples"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAAQ,YAAW;AAQpC,OAAO,MAAMC,oBAKHD;;IACRE,YACE,AAAmBC,KAAQ,EAC3BC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aANhCA,QAAAA;IAOrB;IAEUK,iBAAiBJ,OAAsB,EAAED,KAAQ,EAAe;QACxE,OAAOJ,KAAKU,OAAO,CAACN,OAAOC;IAC7B;IAEAM,WAAW;QACT,OAAO,IAAIT,YAAY,IAAI,CAACE,KAAK,EAAE,IAAI,CAACC,OAAO,EAAE,MAAM,IAAI,CAACE,UAAU;IACxE;IAEAK,WAAW;QACT,OAAO,IAAIV,YAAY,IAAI,CAACE,KAAK,KAAK,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;QAAK;IACtE;IAEAO,UAAU;QACR,OAAO,IAAIZ,YACT,IAAI,CAACE,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAEP,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAQ,QAAQX,QAAW,IAAI,CAACA,KAAK,EAAE;QAC7B,OAAO,IAAIF,YACT,IAAI,CAACE,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEU,SAASX;YAAM;YAAGI,YAAY;QAAK;IAElE;IAEAQ,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAId,YACT,IAAI,CAACE,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEW;YAAY;QAAE;IAE7C;IAEAC,SAAS,GAAGA,QAAqB,EAAE;QACjC,OAAO,IAAIf,YAAY,IAAI,CAACE,KAAK,KAAK,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEY;YAAS;QAAE;IAC3E;AACF"}
1
+ {"version":3,"sources":["../../../src/types/literal.ts"],"sourcesContent":["import { type TLiteral, type TLiteralValue, Type } from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport class LiteralType<\n T extends TLiteralValue = TLiteralValue,\n> extends BaseType<TLiteral<T>> {\n _!: ConstantType<this['schema']>\n\n static factory<T extends TLiteralValue>(value: T) {\n return new LiteralType<T>(Type.Literal(value))\n }\n}\n"],"names":["Type","BaseType","LiteralType","_","factory","value","Literal"],"mappings":"AAAA,SAA4CA,IAAI,QAAQ,oBAAmB;AAC3E,SAASC,QAAQ,QAA2B,YAAW;AAEvD,OAAO,MAAMC,oBAEHD;IACRE,EAAgC;IAEhC,OAAOC,QAAiCC,KAAQ,EAAE;QAChD,OAAO,IAAIH,YAAeF,KAAKM,OAAO,CAACD;IACzC;AACF"}
@@ -1,31 +1,8 @@
1
1
  import { Type } from '@sinclair/typebox';
2
2
  import { BaseType } from "./base.js";
3
3
  export class NeverType extends BaseType {
4
- constructor(options = {}){
5
- super(options, false, false, false);
6
- }
7
- _constructSchema(options) {
8
- return Type.Never(options);
9
- }
10
- nullable() {
11
- throw new Error('NeverType cannot be nullable');
12
- }
13
- optional() {
14
- throw new Error('NeverType cannot be optional');
15
- }
16
- nullish() {
17
- throw new Error('NeverType cannot be nullish');
18
- }
19
- default() {
20
- throw new Error('NeverType cannot have a default value');
21
- }
22
- description(description) {
23
- return new NeverType({
24
- ...this.options,
25
- description
26
- });
27
- }
28
- examples() {
29
- throw new Error('NeverType cannot have examples');
4
+ _;
5
+ static factory() {
6
+ return new NeverType(Type.Never());
30
7
  }
31
8
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/types/never.ts"],"sourcesContent":["import { type SchemaOptions, type TNever, Type } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class NeverType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TNever, N, O, D> {\n constructor(options: SchemaOptions = {}) {\n super(options, false as N, false as O, false as D)\n }\n\n protected _constructSchema(options: SchemaOptions): TNever {\n return Type.Never(options)\n }\n\n nullable(): NeverType<true, O, D> {\n throw new Error('NeverType cannot be nullable')\n }\n\n optional(): NeverType<N, true, D> {\n throw new Error('NeverType cannot be optional')\n }\n\n nullish(): NeverType<true, true, D> {\n throw new Error('NeverType cannot be nullish')\n }\n\n default(): NeverType<N, O, true> {\n throw new Error('NeverType cannot have a default value')\n }\n\n description(description: string): NeverType<N, O, D> {\n return new NeverType({ ...this.options, description })\n }\n\n examples(): NeverType<N, O, D> {\n throw new Error('NeverType cannot have examples')\n }\n}\n"],"names":["Type","BaseType","NeverType","constructor","options","_constructSchema","Never","nullable","Error","optional","nullish","default","description","examples"],"mappings":"AAAA,SAA0CA,IAAI,QAAQ,oBAAmB;AACzE,SAASC,QAAQ,QAAQ,YAAW;AAEpC,OAAO,MAAMC,kBAIHD;IACRE,YAAYC,UAAyB,CAAC,CAAC,CAAE;QACvC,KAAK,CAACA,SAAS,OAAY,OAAY;IACzC;IAEUC,iBAAiBD,OAAsB,EAAU;QACzD,OAAOJ,KAAKM,KAAK,CAACF;IACpB;IAEAG,WAAkC;QAChC,MAAM,IAAIC,MAAM;IAClB;IAEAC,WAAkC;QAChC,MAAM,IAAID,MAAM;IAClB;IAEAE,UAAoC;QAClC,MAAM,IAAIF,MAAM;IAClB;IAEAG,UAAiC;QAC/B,MAAM,IAAIH,MAAM;IAClB;IAEAI,YAAYA,WAAmB,EAAsB;QACnD,OAAO,IAAIV,UAAU;YAAE,GAAG,IAAI,CAACE,OAAO;YAAEQ;QAAY;IACtD;IAEAC,WAA+B;QAC7B,MAAM,IAAIL,MAAM;IAClB;AACF"}
1
+ {"version":3,"sources":["../../../src/types/never.ts"],"sourcesContent":["import { type TNever, Type } from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport class NeverType extends BaseType<TNever> {\n _!: ConstantType<this['schema']>\n\n static factory() {\n return new NeverType(Type.Never())\n }\n}\n"],"names":["Type","BaseType","NeverType","_","factory","Never"],"mappings":"AAAA,SAAsBA,IAAI,QAAQ,oBAAmB;AACrD,SAASC,QAAQ,QAA2B,YAAW;AAEvD,OAAO,MAAMC,kBAAkBD;IAC7BE,EAAgC;IAEhC,OAAOC,UAAU;QACf,OAAO,IAAIF,UAAUF,KAAKK,KAAK;IACjC;AACF"}