@nmtjs/type 0.4.7 → 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 (69) hide show
  1. package/dist/compiler.js +84 -38
  2. package/dist/compiler.js.map +1 -1
  3. package/dist/formats.js +1 -1
  4. package/dist/formats.js.map +1 -1
  5. package/dist/index.js +53 -22
  6. package/dist/index.js.map +1 -1
  7. package/dist/schemas/discriminated-union.js +9 -0
  8. package/dist/schemas/discriminated-union.js.map +1 -0
  9. package/dist/schemas/nullable.js +1 -6
  10. package/dist/schemas/nullable.js.map +1 -1
  11. package/dist/temporal.js +7 -7
  12. package/dist/temporal.js.map +1 -1
  13. package/dist/types/any.js +3 -43
  14. package/dist/types/any.js.map +1 -1
  15. package/dist/types/array.js +17 -63
  16. package/dist/types/array.js.map +1 -1
  17. package/dist/types/base.js +78 -41
  18. package/dist/types/base.js.map +1 -1
  19. package/dist/types/boolean.js +3 -43
  20. package/dist/types/boolean.js.map +1 -1
  21. package/dist/types/custom.js +8 -48
  22. package/dist/types/custom.js.map +1 -1
  23. package/dist/types/date.js +8 -0
  24. package/dist/types/date.js.map +1 -0
  25. package/dist/types/enum.js +10 -94
  26. package/dist/types/enum.js.map +1 -1
  27. package/dist/types/literal.js +3 -43
  28. package/dist/types/literal.js.map +1 -1
  29. package/dist/types/never.js +3 -26
  30. package/dist/types/never.js.map +1 -1
  31. package/dist/types/number.js +52 -186
  32. package/dist/types/number.js.map +1 -1
  33. package/dist/types/object.js +10 -131
  34. package/dist/types/object.js.map +1 -1
  35. package/dist/types/string.js +25 -65
  36. package/dist/types/string.js.map +1 -1
  37. package/dist/types/temporal.js +23 -328
  38. package/dist/types/temporal.js.map +1 -1
  39. package/dist/types/union.js +16 -90
  40. package/dist/types/union.js.map +1 -1
  41. package/dist/utils.js.map +1 -1
  42. package/package.json +3 -3
  43. package/src/compiler.ts +124 -41
  44. package/src/formats.ts +1 -1
  45. package/src/index.ts +145 -63
  46. package/src/schemas/discriminated-union.ts +49 -0
  47. package/src/schemas/nullable.ts +7 -13
  48. package/src/temporal.ts +8 -7
  49. package/src/types/any.ts +6 -46
  50. package/src/types/array.ts +38 -86
  51. package/src/types/base.ts +205 -81
  52. package/src/types/boolean.ts +13 -47
  53. package/src/types/custom.ts +21 -79
  54. package/src/types/date.ts +10 -0
  55. package/src/types/enum.ts +18 -107
  56. package/src/types/literal.ts +7 -63
  57. package/src/types/never.ts +6 -36
  58. package/src/types/number.ts +52 -188
  59. package/src/types/object.ts +61 -202
  60. package/src/types/string.ts +25 -61
  61. package/src/types/temporal.ts +53 -410
  62. package/src/types/union.ts +98 -138
  63. package/src/utils.ts +8 -0
  64. package/dist/constants.js +0 -2
  65. package/dist/constants.js.map +0 -1
  66. package/dist/types/datetime.js +0 -53
  67. package/dist/types/datetime.js.map +0 -1
  68. package/src/constants.ts +0 -5
  69. package/src/types/datetime.ts +0 -65
@@ -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"}
@@ -1,51 +1,11 @@
1
- import { Type, TypeBoxError } from '@sinclair/typebox';
1
+ import { Type } from '@sinclair/typebox';
2
2
  import { BaseType } from "./base.js";
3
3
  export class NumberType extends BaseType {
4
- options;
5
- constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
6
- super(options, isNullable, isOptional, hasDefault);
7
- this.options = options;
8
- }
9
- _constructSchema(options) {
10
- return Type.Number(options);
11
- }
12
- nullable() {
13
- return new NumberType(...this._with({
14
- isNullable: true
15
- }));
16
- }
17
- optional() {
18
- return new NumberType(...this._with({
19
- isOptional: true
20
- }));
21
- }
22
- nullish() {
23
- return new NumberType(...this._with({
24
- isNullable: true,
25
- isOptional: true
26
- }));
27
- }
28
- default(value) {
29
- return new NumberType(...this._with({
30
- options: {
31
- default: value
32
- },
33
- hasDefault: true
34
- }));
35
- }
36
- description(description) {
37
- return new NumberType(...this._with({
38
- options: {
39
- description
40
- }
41
- }));
42
- }
43
- examples(...examples) {
44
- return new NumberType(...this._with({
45
- options: {
46
- examples
47
- }
48
- }));
4
+ _;
5
+ static factory(options = {}) {
6
+ return new NumberType(Type.Number(options), {
7
+ options
8
+ });
49
9
  }
50
10
  positive() {
51
11
  return this.min(0, true);
@@ -54,70 +14,30 @@ export class NumberType extends BaseType {
54
14
  return this.max(0, true);
55
15
  }
56
16
  max(value, exclusive) {
57
- return new NumberType(...this._with({
58
- options: {
59
- maximum: value,
60
- ...!exclusive ? {} : {
61
- exclusiveMaximum: value
62
- }
17
+ return NumberType.factory({
18
+ ...this.props.options,
19
+ maximum: value,
20
+ ...!exclusive ? {} : {
21
+ exclusiveMaximum: value
63
22
  }
64
- }));
23
+ });
65
24
  }
66
25
  min(value, exclusive) {
67
- return new NumberType(...this._with({
68
- options: {
69
- minimum: value,
70
- ...!exclusive ? {} : {
71
- exclusiveMinimum: value
72
- }
26
+ return NumberType.factory({
27
+ ...this.props.options,
28
+ minimum: value,
29
+ ...!exclusive ? {} : {
30
+ exclusiveMinimum: value
73
31
  }
74
- }));
32
+ });
75
33
  }
76
34
  }
77
35
  export class IntegerType extends BaseType {
78
- constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
79
- super(options, isNullable, isOptional, hasDefault);
80
- }
81
- _constructSchema(options) {
82
- return Type.Integer(options);
83
- }
84
- nullable() {
85
- return new IntegerType(...this._with({
86
- isNullable: true
87
- }));
88
- }
89
- optional() {
90
- return new IntegerType(...this._with({
91
- isOptional: true
92
- }));
93
- }
94
- nullish() {
95
- return new IntegerType(...this._with({
96
- isNullable: true,
97
- isOptional: true
98
- }));
99
- }
100
- default(value) {
101
- return new IntegerType(...this._with({
102
- options: {
103
- default: value
104
- },
105
- hasDefault: true
106
- }));
107
- }
108
- description(description) {
109
- return new IntegerType(...this._with({
110
- options: {
111
- description
112
- }
113
- }));
114
- }
115
- examples(...examples) {
116
- return new IntegerType(...this._with({
117
- options: {
118
- examples
119
- }
120
- }));
36
+ _;
37
+ static factory(options = {}) {
38
+ return new IntegerType(Type.Integer(options), {
39
+ options
40
+ });
121
41
  }
122
42
  positive() {
123
43
  return this.min(0, true);
@@ -126,82 +46,30 @@ export class IntegerType extends BaseType {
126
46
  return this.max(0, true);
127
47
  }
128
48
  max(value, exclusive) {
129
- return new IntegerType(...this._with({
130
- options: {
131
- maximum: value,
132
- ...!exclusive ? {} : {
133
- exclusiveMaximum: value
134
- }
49
+ return IntegerType.factory({
50
+ ...this.props.options,
51
+ maximum: value,
52
+ ...!exclusive ? {} : {
53
+ exclusiveMaximum: value
135
54
  }
136
- }));
55
+ });
137
56
  }
138
57
  min(value, exclusive) {
139
- return new IntegerType(...this._with({
140
- options: {
141
- minimum: value,
142
- ...!exclusive ? {} : {
143
- exclusiveMinimum: value
144
- }
58
+ return IntegerType.factory({
59
+ ...this.props.options,
60
+ minimum: value,
61
+ ...!exclusive ? {} : {
62
+ exclusiveMinimum: value
145
63
  }
146
- }));
64
+ });
147
65
  }
148
66
  }
149
67
  export class BigIntType extends BaseType {
150
- constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
151
- super(options, isNullable, isOptional, hasDefault);
152
- }
153
- _constructSchema(options) {
154
- return Type.Transform(Type.String({
155
- ...options,
156
- pattern: '^\\d$'
157
- })).Decode((v)=>{
158
- try {
159
- return BigInt(v);
160
- } catch (error) {
161
- throw new TypeBoxError('Invalid bigint value');
162
- }
163
- }).Encode(this.encode);
164
- }
165
- encode = (value)=>{
166
- return value.toString();
167
- };
168
- nullable() {
169
- return new BigIntType(...this._with({
170
- isNullable: true
171
- }));
172
- }
173
- optional() {
174
- return new BigIntType(...this._with({
175
- isOptional: true
176
- }));
177
- }
178
- nullish() {
179
- return new BigIntType(...this._with({
180
- isNullable: true,
181
- isOptional: true
182
- }));
183
- }
184
- default(value) {
185
- return new BigIntType(...this._with({
186
- options: {
187
- default: value
188
- },
189
- hasDefault: true
190
- }));
191
- }
192
- description(description) {
193
- return new BigIntType(...this._with({
194
- options: {
195
- description
196
- }
197
- }));
198
- }
199
- examples(...examples) {
200
- return new BigIntType(...this._with({
201
- options: {
202
- examples: examples.map(this.encode)
203
- }
204
- }));
68
+ _;
69
+ static factory(options = {}) {
70
+ return new BigIntType(Type.BigInt(options), {
71
+ options
72
+ });
205
73
  }
206
74
  positive() {
207
75
  return this.min(0n, true);
@@ -210,23 +78,21 @@ export class BigIntType extends BaseType {
210
78
  return this.max(0n, true);
211
79
  }
212
80
  max(value, exclusive) {
213
- return new BigIntType(...this._with({
214
- options: {
215
- maximum: this.encode(value),
216
- ...!exclusive ? {} : {
217
- exclusiveMaximum: this.encode(value)
218
- }
81
+ return BigIntType.factory({
82
+ ...this.props.options,
83
+ maximum: value,
84
+ ...!exclusive ? {} : {
85
+ exclusiveMaximum: value
219
86
  }
220
- }));
87
+ });
221
88
  }
222
89
  min(value, exclusive) {
223
- return new BigIntType(...this._with({
224
- options: {
225
- minimum: `${value}`,
226
- ...!exclusive ? {} : {
227
- exclusiveMinimum: `${value}`
228
- }
90
+ return BigIntType.factory({
91
+ ...this.props.options,
92
+ minimum: value,
93
+ ...!exclusive ? {} : {
94
+ exclusiveMinimum: value
229
95
  }
230
- }));
96
+ });
231
97
  }
232
98
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/types/number.ts"],"sourcesContent":["import {\n type IntegerOptions,\n type NumberOptions,\n type StringOptions,\n type TInteger,\n type TNumber,\n type TString,\n type TTransform,\n Type,\n TypeBoxError,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class NumberType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TNumber, N, O, D, NumberOptions> {\n constructor(\n protected readonly options: NumberOptions = {},\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: NumberOptions): TNumber {\n return Type.Number(options)\n }\n\n nullable() {\n return new NumberType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new NumberType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new NumberType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: number) {\n return new NumberType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new NumberType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [number, ...number[]]) {\n return new NumberType(...this._with({ options: { examples } }))\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return new NumberType(\n ...this._with({\n options: {\n maximum: value,\n ...(!exclusive ? {} : { exclusiveMaximum: value }),\n },\n }),\n )\n }\n\n min(value: number, exclusive?: true) {\n return new NumberType(\n ...this._with({\n options: {\n minimum: value,\n ...(!exclusive ? {} : { exclusiveMinimum: value }),\n },\n }),\n )\n }\n}\n\nexport class IntegerType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TInteger, N, O, D, IntegerOptions> {\n constructor(\n options: IntegerOptions = {},\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: IntegerOptions): TInteger {\n return Type.Integer(options)\n }\n\n nullable() {\n return new IntegerType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new IntegerType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new IntegerType(\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: number) {\n return new IntegerType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new IntegerType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [number, ...number[]]) {\n return new IntegerType(...this._with({ options: { examples } }))\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return new IntegerType(\n ...this._with({\n options: {\n maximum: value,\n ...(!exclusive ? {} : { exclusiveMaximum: value }),\n },\n }),\n )\n }\n\n min(value: number, exclusive?: true) {\n return new IntegerType(\n ...this._with({\n options: {\n minimum: value,\n ...(!exclusive ? {} : { exclusiveMinimum: value }),\n },\n }),\n )\n }\n}\n\nexport class BigIntType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TTransform<TString, bigint>, N, O, D, StringOptions> {\n constructor(\n options: StringOptions = {},\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: StringOptions) {\n return Type.Transform(Type.String({ ...options, pattern: '^\\\\d$' }))\n .Decode((v: string) => {\n try {\n return BigInt(v)\n } catch (error) {\n throw new TypeBoxError('Invalid bigint value')\n }\n })\n .Encode(this.encode)\n }\n\n protected encode = (value: bigint): string => {\n return value.toString()\n }\n\n nullable() {\n return new BigIntType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new BigIntType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new BigIntType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: bigint) {\n return new BigIntType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new BigIntType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [bigint, ...bigint[]]) {\n return new BigIntType(\n ...this._with({ options: { examples: examples.map(this.encode) } }),\n )\n }\n\n positive() {\n return this.min(0n, true)\n }\n\n negative() {\n return this.max(0n, true)\n }\n\n max(value: bigint, exclusive?: true) {\n return new BigIntType(\n ...this._with({\n options: {\n maximum: this.encode(value),\n ...(!exclusive ? {} : { exclusiveMaximum: this.encode(value) }),\n },\n }),\n )\n }\n\n min(value: bigint, exclusive?: true) {\n return new BigIntType(\n ...this._with({\n options: {\n minimum: `${value}`,\n ...(!exclusive ? {} : { exclusiveMinimum: `${value}` }),\n },\n }),\n )\n }\n}\n"],"names":["Type","TypeBoxError","BaseType","NumberType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","Number","nullable","_with","optional","nullish","default","value","description","examples","positive","min","negative","max","exclusive","maximum","exclusiveMaximum","minimum","exclusiveMinimum","IntegerType","Integer","BigIntType","Transform","String","pattern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ourcesContent":["import {\n type BigIntOptions,\n type IntegerOptions,\n type NumberOptions,\n type Static,\n type StaticDecode,\n type StringOptions,\n type TBigInt,\n type TInteger,\n type TNumber,\n type TString,\n type TTransform,\n Type,\n TypeBoxError,\n} from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\nimport { CustomType, TransformType } from './custom.ts'\n\nexport class NumberType extends BaseType<TNumber, { options: NumberOptions }> {\n _!: ConstantType<this['schema']>\n\n static factory(options: NumberOptions = {}) {\n return new NumberType(Type.Number(options), { options })\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return NumberType.factory({\n ...this.props.options,\n maximum: value,\n ...(!exclusive ? {} : { exclusiveMaximum: value }),\n })\n }\n\n min(value: number, exclusive?: true) {\n return NumberType.factory({\n ...this.props.options,\n minimum: value,\n ...(!exclusive ? {} : { exclusiveMinimum: value }),\n })\n }\n}\n\nexport class IntegerType extends BaseType<\n TInteger,\n { options: IntegerOptions }\n> {\n _!: ConstantType<this['schema']>\n\n static factory(options: IntegerOptions = {}) {\n return new IntegerType(Type.Integer(options), { options })\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return IntegerType.factory({\n ...this.props.options,\n maximum: value,\n ...(!exclusive ? {} : { exclusiveMaximum: value }),\n })\n }\n\n min(value: number, exclusive?: true) {\n return IntegerType.factory({\n ...this.props.options,\n minimum: value,\n ...(!exclusive ? {} : { exclusiveMinimum: value }),\n })\n }\n}\n\n// TODO: this is not json schema compatible\nexport class BigIntType extends BaseType<TBigInt, { options: BigIntOptions }> {\n _!: ConstantType<this['schema']>\n\n static factory(options: BigIntOptions = {}) {\n return new BigIntType(Type.BigInt(options), { options })\n }\n\n positive() {\n return this.min(0n, true)\n }\n\n negative() {\n return this.max(0n, true)\n }\n\n max(value: bigint, exclusive?: true) {\n return BigIntType.factory({\n ...this.props.options,\n maximum: value,\n ...(!exclusive ? {} : { exclusiveMaximum: value }),\n })\n }\n\n min(value: bigint, exclusive?: true) {\n return BigIntType.factory({\n ...this.props.options,\n minimum: value,\n ...(!exclusive ? {} : { exclusiveMinimum: value }),\n })\n }\n}\n"],"names":["Type","BaseType","NumberType","_","factory","options","Number","positive","min","negative","max","value","exclusive","props","maximum","exclusiveMaximum","minimum","exclusiveMinimum","IntegerType","Integer","BigIntType","BigInt"],"mappings":"AAAA,SAYEA,IAAI,QAEC,oBAAmB;AAC1B,SAASC,QAAQ,QAA2B,YAAW;AAGvD,OAAO,MAAMC,mBAAmBD;IAC9BE,EAAgC;IAEhC,OAAOC,QAAQC,UAAyB,CAAC,CAAC,EAAE;QAC1C,OAAO,IAAIH,WAAWF,KAAKM,MAAM,CAACD,UAAU;YAAEA;QAAQ;IACxD;IAEAE,WAAW;QACT,OAAO,IAAI,CAACC,GAAG,CAAC,GAAG;IACrB;IAEAC,WAAW;QACT,OAAO,IAAI,CAACC,GAAG,CAAC,GAAG;IACrB;IAEAA,IAAIC,KAAa,EAAEC,SAAgB,EAAE;QACnC,OAAOV,WAAWE,OAAO,CAAC;YACxB,GAAG,IAAI,CAACS,KAAK,CAACR,OAAO;YACrBS,SAASH;YACT,GAAI,CAACC,YAAY,CAAC,IAAI;gBAAEG,kBAAkBJ;YAAM,CAAC;QACnD;IACF;IAEAH,IAAIG,KAAa,EAAEC,SAAgB,EAAE;QACnC,OAAOV,WAAWE,OAAO,CAAC;YACxB,GAAG,IAAI,CAACS,KAAK,CAACR,OAAO;YACrBW,SAASL;YACT,GAAI,CAACC,YAAY,CAAC,IAAI;gBAAEK,kBAAkBN;YAAM,CAAC;QACnD;IACF;AACF;AAEA,OAAO,MAAMO,oBAAoBjB;IAI/BE,EAAgC;IAEhC,OAAOC,QAAQC,UAA0B,CAAC,CAAC,EAAE;QAC3C,OAAO,IAAIa,YAAYlB,KAAKmB,OAAO,CAACd,UAAU;YAAEA;QAAQ;IAC1D;IAEAE,WAA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ACnD;IACF;AACF"}
@@ -1,140 +1,19 @@
1
1
  import { Type } from '@sinclair/typebox';
2
- import { BaseType, getTypeSchema } from "./base.js";
3
- import { EnumType } from "./enum.js";
2
+ import { BaseType } from "./base.js";
4
3
  export class ObjectType extends BaseType {
5
- properties;
6
- constructor(properties = {}, options = {}, isNullable = false, isOptional = false, hasDefault = false){
7
- super(options, isNullable, isOptional, hasDefault, properties);
8
- this.properties = properties;
9
- }
10
- _constructSchema(options, properties) {
4
+ static factory(properties, options = {}) {
11
5
  const schemaProperties = {};
12
- for (const [key, value] of Object.entries(properties)){
13
- schemaProperties[key] = getTypeSchema(value);
6
+ for(const key in properties){
7
+ schemaProperties[key] = properties[key].schema;
14
8
  }
15
- return Type.Object(schemaProperties, options);
16
- }
17
- nullable() {
18
- return new ObjectType(this.properties, ...this._with({
19
- isNullable: true
20
- }));
21
- }
22
- optional() {
23
- return new ObjectType(this.properties, ...this._with({
24
- isOptional: true
25
- }));
26
- }
27
- nullish() {
28
- return new ObjectType(this.properties, ...this._with({
29
- isNullable: true,
30
- isOptional: true
31
- }));
32
- }
33
- default(value) {
34
- return new ObjectType(this.properties, ...this._with({
35
- options: {
36
- default: value
37
- },
38
- hasDefault: true
39
- }));
40
- }
41
- description(description) {
42
- return new ObjectType(this.properties, ...this._with({
43
- options: {
44
- description
45
- }
46
- }));
47
- }
48
- examples(...examples) {
49
- return new ObjectType(this.properties, ...this._with({
50
- options: {
51
- examples
52
- }
53
- }));
54
- }
55
- pick(pick) {
56
- const properties = Object.fromEntries(Object.entries(this.properties).filter(([key])=>pick[key]));
57
- const [_, ...args] = this._with();
58
- return new ObjectType(properties, {}, ...args);
59
- }
60
- omit(omit) {
61
- const properties = Object.fromEntries(Object.entries(this.properties).filter(([key])=>!omit[key]));
62
- const [_, ...args] = this._with();
63
- return new ObjectType(properties, {}, ...args);
64
- }
65
- extend(properties) {
66
- const [_, ...args] = this._with();
67
- return new ObjectType({
68
- ...this.properties,
69
- ...properties
70
- }, {}, ...args);
71
- }
72
- merge(object) {
73
- const [_, ...args] = this._with();
74
- return new ObjectType({
75
- ...this.properties,
76
- ...object.properties
77
- }, {}, ...args);
78
- }
79
- partial() {
80
- const properties = {};
81
- for (const [key, value] of Object.entries(this.properties)){
82
- properties[key] = value.optional();
83
- }
84
- const [_, ...args] = this._with();
85
- return new ObjectType(properties, {}, ...args);
86
- }
87
- keyof() {
88
- return new EnumType(Object.keys(this.properties));
9
+ return new ObjectType(Type.Object(schemaProperties, options), {
10
+ properties
11
+ });
89
12
  }
90
13
  }
91
14
  export class RecordType extends BaseType {
92
- key;
93
- element;
94
- constructor(key, element, options = {}, isNullable = false, isOptional = false, hasDefault = false){
95
- super(options, isNullable, isOptional, hasDefault, key, element);
96
- this.key = key;
97
- this.element = element;
98
- }
99
- _constructSchema(options, key, element) {
100
- return Type.Record(getTypeSchema(key), getTypeSchema(element), options);
101
- }
102
- nullable() {
103
- return new RecordType(this.key, this.element, ...this._with({
104
- isNullable: true
105
- }));
106
- }
107
- optional() {
108
- return new RecordType(this.key, this.element, ...this._with({
109
- isOptional: true
110
- }));
111
- }
112
- nullish() {
113
- return new RecordType(this.key, this.element, ...this._with({
114
- isNullable: true,
115
- isOptional: true
116
- }));
117
- }
118
- default(value) {
119
- return new RecordType(this.key, this.element, ...this._with({
120
- options: {
121
- default: value
122
- },
123
- hasDefault: true
124
- }));
125
- }
126
- description(description) {
127
- return new RecordType(this.key, this.element, ...this._with({
128
- options: {
129
- description
130
- }
131
- }));
132
- }
133
- examples(...examples) {
134
- return new RecordType(this.key, this.element, ...this._with({
135
- options: {
136
- examples
137
- }
138
- }));
15
+ _;
16
+ static factory(key, element, options = {}) {
17
+ return new RecordType(Type.Record(key.schema, element.schema, options));
139
18
  }
140
19
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/types/object.ts"],"sourcesContent":["import {\n type ObjectOptions,\n type TObject,\n type TRecord,\n Type,\n} from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport type { UnionToTupleString } from '../utils.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\nimport { type AnyEnumType, type AnyObjectEnumType, EnumType } from './enum.ts'\nimport type { AnyLiteralType } from './literal.ts'\nimport type { AnyStringType } from './string.ts'\nimport type { AnyUnionType } from './union.ts'\n\nexport class ObjectType<\n T extends Record<string, BaseType>,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,\n N,\n O,\n D\n> {\n constructor(\n protected readonly properties: T = {} as T,\n options: ObjectOptions = {},\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, properties)\n }\n\n protected _constructSchema(\n options: ObjectOptions,\n properties: T,\n ): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {\n const schemaProperties = {} as {\n [K in keyof T]: T[K][typeStatic]['schema']\n }\n\n for (const [key, value] of Object.entries(properties)) {\n // @ts-expect-error\n schemaProperties[key] = getTypeSchema(value)\n }\n return Type.Object(schemaProperties, options)\n }\n\n nullable() {\n return new ObjectType(this.properties, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ObjectType(this.properties, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ObjectType(\n this.properties,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { examples } }),\n )\n }\n\n pick<P extends { [K in keyof T]?: true }>(pick: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => pick[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Pick<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n omit<P extends { [K in keyof T]?: true }>(omit: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => !omit[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Omit<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n extend<P extends Record<string, BaseType>>(properties: P) {\n const [_, ...args] = this._with()\n return new ObjectType({ ...this.properties, ...properties }, {}, ...args)\n }\n\n merge<T extends ObjectType<Record<string, BaseType>>>(object: T) {\n const [_, ...args] = this._with()\n return new ObjectType(\n { ...this.properties, ...object.properties },\n {},\n ...args,\n )\n }\n\n partial() {\n const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =\n {} as any\n for (const [key, value] of Object.entries(this.properties)) {\n // @ts-expect-error\n properties[key] = value.optional()\n }\n const [_, ...args] = this._with()\n return new ObjectType(properties, {}, ...args)\n }\n\n keyof(): EnumType<UnionToTupleString<keyof T>> {\n return new EnumType(Object.keys(this.properties) as any)\n }\n}\n\nexport class RecordType<\n K extends\n | AnyLiteralType\n | AnyEnumType\n | AnyObjectEnumType\n | AnyStringType\n | AnyUnionType,\n E extends BaseType,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>,\n N,\n O,\n D,\n ObjectOptions\n> {\n constructor(\n protected readonly key: K,\n protected readonly element: E,\n options: ObjectOptions = {},\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, key, element)\n }\n\n protected _constructSchema(options: ObjectOptions, key: K, element: E) {\n return Type.Record(\n getTypeSchema(key),\n getTypeSchema(element),\n options,\n ) as unknown as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>\n }\n\n nullable() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true }),\n )\n }\n\n optional() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isOptional: true }),\n )\n }\n\n nullish() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new RecordType(\n this.key,\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 RecordType(\n this.key,\n this.element,\n ...this._with({ options: { examples } }),\n )\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","EnumType","ObjectType","constructor","properties","options","isNullable","isOptional","hasDefault","_constructSchema","schemaProperties","key","value","Object","entries","nullable","_with","optional","nullish","default","description","examples","pick","fromEntries","filter","_","args","omit","extend","merge","object","partial","keyof","keys","RecordType","element","Record"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAG1B,SAASC,QAAQ,EAAEC,aAAa,QAAQ,YAAW;AACnD,SAAmDC,QAAQ,QAAQ,YAAW;AAK9E,OAAO,MAAMC,mBAKHH;;IAMRI,YACE,AAAmBC,aAAgB,CAAC,CAAM,EAC1CC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aANhCA,aAAAA;IAOrB;IAEUK,iBACRJ,OAAsB,EACtBD,UAAa,EAC4C;QACzD,MAAMM,mBAAmB,CAAC;QAI1B,KAAK,MAAM,CAACC,KAAKC,MAAM,IAAIC,OAAOC,OAAO,CAACV,YAAa;YAErDM,gBAAgB,CAACC,IAAI,GAAGX,cAAcY;QACxC;QACA,OAAOd,KAAKe,MAAM,CAACH,kBAAkBL;IACvC;IAEAU,WAAW;QACT,OAAO,IAAIb,WAAW,IAAI,CAACE,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AAKsC,MAAM,CAChBpC,cAAcW,MACdX,cAAcmC,UACd9B;IAEJ;IAEAU,WAAW;QACT,OAAO,IAAImB,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEV,YAAY;QAAK;IAErC;IAEAW,WAAW;QACT,OAAO,IAAIiB,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAET,YAAY;QAAK;IAErC;IAEAW,UAAU;QACR,OAAO,IAAIgB,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEV,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAY,QAAQP,KAAkC,EAAE;QAC1C,OAAO,IAAIsB,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEX,SAAS;gBAAEc,SAASP;YAAM;YAAGJ,YAAY;QAAK;IAElE;IAEAY,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAIc,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEX,SAAS;gBAAEe;YAAY;QAAE;IAE7C;IAEAC,SACE,GAAGA,QAAyE,EAC5E;QACA,OAAO,IAAIa,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEX,SAAS;gBAAEgB;YAAS;QAAE;IAE1C;AACF"}
1
+ {"version":3,"sources":["../../../src/types/object.ts"],"sourcesContent":["import {\n type ObjectOptions,\n type TObject,\n type TRecordOrObject,\n type TSchema,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType, type BaseTypeAny } from './base.ts'\nimport type { EnumType, ObjectEnumType } from './enum.ts'\nimport type { LiteralType } from './literal.ts'\nimport type { StringType } from './string.ts'\n\nexport type ObjectTypeProps = { [k: string]: BaseTypeAny<any> }\nexport class ObjectType<\n T extends ObjectTypeProps = ObjectTypeProps,\n> extends BaseType<\n TObject<{ [K in keyof T]: T[K]['schema'] }>,\n { properties: T }\n> {\n declare _: {\n encoded: {\n input: TObject<{ [K in keyof T]: T[K]['_']['encoded']['input'] }>\n output: TObject<{ [K in keyof T]: T[K]['_']['encoded']['output'] }>\n }\n decoded: {\n input: TObject<{ [K in keyof T]: T[K]['_']['decoded']['input'] }>\n output: TObject<{ [K in keyof T]: T[K]['_']['decoded']['output'] }>\n }\n }\n\n static factory<T extends ObjectTypeProps = ObjectTypeProps>(\n properties: T,\n options: ObjectOptions = {},\n ) {\n const schemaProperties = {} as {\n [K in keyof T]: T[K]['schema']\n }\n\n for (const key in properties) {\n schemaProperties[key] = properties[key].schema\n }\n\n return new ObjectType<T>(Type.Object(schemaProperties, options) as any, {\n properties,\n })\n }\n}\n\nexport class RecordType<\n K extends LiteralType | EnumType | ObjectEnumType | StringType,\n E extends BaseType,\n> extends BaseType<TRecordOrObject<K['schema'], E['schema']>> {\n _!: {\n encoded: {\n input: TRecordOrObject<\n K['_']['encoded']['input'],\n E['_']['encoded']['input']\n >\n output: TRecordOrObject<\n K['_']['encoded']['output'],\n E['_']['encoded']['output']\n >\n }\n decoded: {\n input: TRecordOrObject<\n K['_']['decoded']['input'],\n E['_']['decoded']['input']\n >\n output: TRecordOrObject<\n K['_']['decoded']['output'],\n E['_']['decoded']['output']\n >\n }\n }\n\n static factory<\n K extends\n | LiteralType<any>\n | EnumType<any>\n | ObjectEnumType<any>\n | StringType,\n E extends BaseType,\n >(key: K, element: E, options: ObjectOptions = {}) {\n return new RecordType<K, E>(\n Type.Record(key.schema, element.schema, options) as any,\n )\n }\n}\n"],"names":["Type","BaseType","ObjectType","factory","properties","options","schemaProperties","key","schema","Object","RecordType","_","element","Record"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAA0B,YAAW;AAMtD,OAAO,MAAMC,mBAEHD;IAeR,OAAOE,QACLC,UAAa,EACbC,UAAyB,CAAC,CAAC,EAC3B;QACA,MAAMC,mBAAmB,CAAC;QAI1B,IAAK,MAAMC,OAAOH,WAAY;YAC5BE,gBAAgB,CAACC,IAAI,GAAGH,UAAU,CAACG,IAAI,CAACC,MAAM;QAChD;QAEA,OAAO,IAAIN,WAAcF,KAAKS,MAAM,CAACH,kBAAkBD,UAAiB;YACtED;QACF;IACF;AACF;AAEA,OAAO,MAAMM,mBAGHT;IACRU,EAqBC;IAED,OAAOR,QAOLI,GAAM,EAAEK,OAAU,EAAEP,UAAyB,CAAC,CAAC,EAAE;QACjD,OAAO,IAAIK,WACTV,KAAKa,MAAM,CAACN,IAAIC,MAAM,EAAEI,QAAQJ,MAAM,EAAEH;IAE5C;AACF"}
@@ -1,77 +1,37 @@
1
1
  import { Type } from '@sinclair/typebox';
2
2
  import { BaseType } from "./base.js";
3
3
  export class StringType extends BaseType {
4
- constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
5
- super(options, isNullable, isOptional, hasDefault);
6
- }
7
- _constructSchema(options) {
8
- return Type.String(options);
9
- }
10
- nullable() {
11
- return new StringType(...this._with({
12
- isNullable: true
13
- }));
14
- }
15
- optional() {
16
- return new StringType(...this._with({
17
- isOptional: true
18
- }));
19
- }
20
- nullish() {
21
- return new StringType(...this._with({
22
- isNullable: true,
23
- isOptional: true
24
- }));
25
- }
26
- default(value) {
27
- return new StringType(...this._with({
28
- options: {
29
- default: value
30
- },
31
- hasDefault: true
32
- }));
33
- }
34
- description(description) {
35
- return new StringType(...this._with({
36
- options: {
37
- description
38
- }
39
- }));
40
- }
41
- examples(...examples) {
42
- return new StringType(...this._with({
43
- options: {
44
- examples
45
- }
46
- }));
47
- }
48
- format(format) {
49
- return new StringType(...this._with({
50
- options: {
51
- format
52
- }
53
- }));
4
+ _;
5
+ static factory(options = {}) {
6
+ return new StringType(Type.String(options), {
7
+ options
8
+ });
54
9
  }
55
10
  max(value) {
56
- return new StringType(...this._with({
57
- options: {
58
- maxLength: value
59
- }
60
- }));
11
+ return StringType.factory({
12
+ ...this.props.options,
13
+ maxLength: value
14
+ });
61
15
  }
62
16
  min(value) {
63
- return new StringType(...this._with({
64
- options: {
65
- minLength: value
66
- }
67
- }));
17
+ return StringType.factory({
18
+ ...this.props.options,
19
+ minLength: value
20
+ });
21
+ }
22
+ format(format) {
23
+ return StringType.factory({
24
+ ...this.props.options,
25
+ pattern: undefined,
26
+ format
27
+ });
68
28
  }
69
29
  pattern(pattern) {
70
- return new StringType(...this._with({
71
- options: {
72
- pattern
73
- }
74
- }));
30
+ return StringType.factory({
31
+ ...this.props.options,
32
+ format: undefined,
33
+ pattern
34
+ });
75
35
  }
76
36
  email() {
77
37
  return this.format('email');