@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,12 +1,6 @@
1
- import {
2
- type SchemaOptions,
3
- type StringOptions,
4
- type TString,
5
- type TTransform,
6
- Type,
7
- } from '@sinclair/typebox'
1
+ import { type TString, Type } from '@sinclair/typebox'
8
2
  import { Temporal } from 'temporal-polyfill'
9
- import { BaseType } from './base.ts'
3
+ import { CustomType, TransformType } from './custom.ts'
10
4
 
11
5
  type Types = Exclude<
12
6
  keyof typeof Temporal,
@@ -35,453 +29,102 @@ const createTemporalTransformer = <T extends Types>(
35
29
  } as TemporalTransformer<T>
36
30
  }
37
31
 
38
- export class PlainDateType<
39
- N extends boolean = false,
40
- O extends boolean = false,
41
- D extends boolean = false,
42
- > extends BaseType<
43
- TTransform<TString, Temporal.PlainDate>,
44
- N,
45
- O,
46
- D,
47
- StringOptions
48
- > {
32
+ export class PlainDateType extends TransformType<Temporal.PlainDate, TString> {
49
33
  static readonly transformer = createTemporalTransformer('PlainDate')
50
34
 
51
- constructor(
52
- options: StringOptions = {},
53
- isNullable: N = false as N,
54
- isOptional: O = false as O,
55
- hasDefault: D = false as D,
56
- ) {
57
- super(options, isNullable, isOptional, hasDefault)
58
- }
59
-
60
- protected _constructSchema(
61
- options: SchemaOptions,
62
- ): TTransform<TString, Temporal.PlainDate> {
63
- return Type.Transform(Type.String({ ...options, format: 'iso-date' }))
64
- .Decode(PlainDateType.transformer.decode)
65
- .Encode(PlainDateType.transformer.encode)
66
- }
67
-
68
- nullable() {
69
- return new PlainDateType(...this._with({ isNullable: true }))
70
- }
71
-
72
- optional() {
73
- return new PlainDateType(...this._with({ isOptional: true }))
74
- }
75
-
76
- nullish() {
77
- return new PlainDateType(
78
- ...this._with({ isNullable: true, isOptional: true }),
79
- )
80
- }
81
-
82
- default(value: Temporal.PlainDate) {
83
- return new PlainDateType(
84
- ...this._with({
85
- options: { default: PlainDateType.transformer.encode(value) },
86
- hasDefault: true,
87
- }),
88
- )
89
- }
90
-
91
- description(description: string) {
92
- return new PlainDateType(...this._with({ options: { description } }))
93
- }
94
-
95
- examples(...examples: [Temporal.PlainDate, ...Temporal.PlainDate[]]) {
96
- return new PlainDateType(
97
- ...this._with({
98
- options: { examples: examples.map(PlainDateType.transformer.encode) },
99
- }),
35
+ static factory() {
36
+ return CustomType.factory(
37
+ PlainDateType.transformer.decode,
38
+ PlainDateType.transformer.encode,
39
+ Type.String(),
100
40
  )
101
41
  }
102
42
  }
103
43
 
104
- export class PlainDateTimeType<
105
- N extends boolean = false,
106
- O extends boolean = false,
107
- D extends boolean = false,
108
- > extends BaseType<
109
- TTransform<TString, Temporal.PlainDateTime>,
110
- N,
111
- O,
112
- D,
113
- StringOptions
44
+ export class PlainDateTimeType extends TransformType<
45
+ Temporal.PlainDateTime,
46
+ TString
114
47
  > {
115
48
  static readonly transformer = createTemporalTransformer('PlainDateTime')
49
+ protected _encode = PlainDateTimeType.transformer.encode
116
50
 
117
- constructor(
118
- options: StringOptions = {},
119
- isNullable: N = false as N,
120
- isOptional: O = false as O,
121
- hasDefault: D = false as D,
122
- ) {
123
- super(options, isNullable, isOptional, hasDefault)
124
- }
125
-
126
- protected _constructSchema(
127
- options: SchemaOptions,
128
- ): TTransform<TString, Temporal.PlainDateTime> {
129
- return Type.Transform(Type.String({ ...options, format: 'iso-date-time' }))
130
- .Decode(PlainDateTimeType.transformer.decode)
131
- .Encode(PlainDateTimeType.transformer.encode)
132
- }
133
-
134
- nullable() {
135
- return new PlainDateTimeType(...this._with({ isNullable: true }))
136
- }
137
-
138
- optional() {
139
- return new PlainDateTimeType(...this._with({ isOptional: true }))
140
- }
141
-
142
- nullish() {
143
- return new PlainDateTimeType(
144
- ...this._with({ isNullable: true, isOptional: true }),
145
- )
146
- }
147
-
148
- default(value: Temporal.PlainDateTime) {
149
- return new PlainDateTimeType(
150
- ...this._with({
151
- options: { default: PlainDateTimeType.transformer.encode(value) },
152
- hasDefault: true,
153
- }),
51
+ static factory() {
52
+ return CustomType.factory(
53
+ PlainDateTimeType.transformer.decode,
54
+ PlainDateTimeType.transformer.encode,
55
+ Type.String(),
154
56
  )
155
57
  }
156
-
157
- description(description: string) {
158
- return new PlainDateTimeType(...this._with({ options: { description } }))
159
- }
160
-
161
- examples(...examples: string[]) {
162
- return new PlainDateTimeType(...this._with({ options: { examples } }))
163
- }
164
58
  }
165
59
 
166
- export class ZonedDateTimeType<
167
- N extends boolean = false,
168
- O extends boolean = false,
169
- D extends boolean = false,
170
- > extends BaseType<
171
- TTransform<TString, Temporal.ZonedDateTime>,
172
- N,
173
- O,
174
- D,
175
- StringOptions
60
+ export class ZonedDateTimeType extends TransformType<
61
+ Temporal.ZonedDateTime,
62
+ TString
176
63
  > {
177
64
  static readonly transformer = createTemporalTransformer(
178
65
  'ZonedDateTime',
179
66
  (value) => Temporal.Instant.from(value).toZonedDateTimeISO('UTC'),
180
67
  )
181
68
 
182
- constructor(
183
- options: StringOptions = {},
184
- isNullable: N = false as N,
185
- isOptional: O = false as O,
186
- hasDefault: D = false as D,
187
- ) {
188
- super(options, isNullable, isOptional, hasDefault)
189
- }
190
-
191
- protected _constructSchema(
192
- options: SchemaOptions,
193
- ): TTransform<TString, Temporal.ZonedDateTime> {
194
- return Type.Transform(Type.String({ ...options, format: 'date-time' }))
195
- .Decode(ZonedDateTimeType.transformer.decode)
196
- .Encode(ZonedDateTimeType.transformer.encode)
197
- }
198
-
199
- nullable() {
200
- return new ZonedDateTimeType(...this._with({ isNullable: true }))
201
- }
202
-
203
- optional() {
204
- return new ZonedDateTimeType(...this._with({ isOptional: true }))
205
- }
206
-
207
- nullish() {
208
- return new ZonedDateTimeType(
209
- ...this._with({ isNullable: true, isOptional: true }),
210
- )
211
- }
212
-
213
- default(value: Temporal.ZonedDateTime) {
214
- return new ZonedDateTimeType(
215
- ...this._with({
216
- options: { default: ZonedDateTimeType.transformer.encode(value) },
217
- hasDefault: true,
218
- }),
69
+ static factory() {
70
+ return CustomType.factory(
71
+ ZonedDateTimeType.transformer.decode,
72
+ ZonedDateTimeType.transformer.encode,
73
+ Type.String(),
219
74
  )
220
75
  }
221
-
222
- description(description: string) {
223
- return new ZonedDateTimeType(...this._with({ options: { description } }))
224
- }
225
-
226
- examples(...examples: string[]) {
227
- return new ZonedDateTimeType(...this._with({ options: { examples } }))
228
- }
229
76
  }
230
77
 
231
- export class PlainTimeType<
232
- N extends boolean = false,
233
- O extends boolean = false,
234
- D extends boolean = false,
235
- > extends BaseType<
236
- TTransform<TString, Temporal.PlainTime>,
237
- N,
238
- O,
239
- D,
240
- StringOptions
241
- > {
78
+ export class PlainTimeType extends TransformType<Temporal.PlainTime, TString> {
242
79
  static readonly transformer = createTemporalTransformer('PlainTime')
243
80
 
244
- constructor(
245
- options: StringOptions = {},
246
- isNullable: N = false as N,
247
- isOptional: O = false as O,
248
- hasDefault: D = false as D,
249
- ) {
250
- super(options, isNullable, isOptional, hasDefault)
251
- }
252
-
253
- protected _constructSchema(
254
- options: SchemaOptions,
255
- ): TTransform<TString, Temporal.PlainTime> {
256
- return Type.Transform(Type.String({ ...options, format: 'time' }))
257
- .Decode(PlainTimeType.transformer.decode)
258
- .Encode(PlainTimeType.transformer.encode)
259
- }
260
-
261
- nullable() {
262
- return new PlainTimeType(...this._with({ isNullable: true }))
263
- }
264
-
265
- optional() {
266
- return new PlainTimeType(...this._with({ isOptional: true }))
267
- }
268
-
269
- nullish() {
270
- return new PlainTimeType(
271
- ...this._with({ isNullable: true, isOptional: true }),
81
+ static factory() {
82
+ return CustomType.factory(
83
+ PlainTimeType.transformer.decode,
84
+ PlainTimeType.transformer.encode,
85
+ Type.String(),
272
86
  )
273
87
  }
274
-
275
- default(value: Temporal.PlainTime) {
276
- return new PlainTimeType(
277
- ...this._with({
278
- options: { default: PlainTimeType.transformer.encode(value) },
279
- hasDefault: true,
280
- }),
281
- )
282
- }
283
-
284
- description(description: string) {
285
- return new PlainTimeType(...this._with({ options: { description } }))
286
- }
287
-
288
- examples(...examples: string[]) {
289
- return new PlainTimeType(...this._with({ options: { examples } }))
290
- }
291
88
  }
292
89
 
293
- export class DurationType<
294
- N extends boolean = false,
295
- O extends boolean = false,
296
- D extends boolean = false,
297
- > extends BaseType<
298
- TTransform<TString, Temporal.Duration>,
299
- N,
300
- O,
301
- D,
302
- StringOptions
303
- > {
90
+ export class DurationType extends TransformType<Temporal.Duration, TString> {
304
91
  static readonly transformer = createTemporalTransformer('Duration')
305
92
 
306
- constructor(
307
- options: StringOptions = {},
308
- isNullable: N = false as N,
309
- isOptional: O = false as O,
310
- hasDefault: D = false as D,
311
- ) {
312
- super(options, isNullable, isOptional, hasDefault)
313
- }
314
-
315
- protected _constructSchema(
316
- options: SchemaOptions,
317
- ): TTransform<TString, Temporal.Duration> {
318
- return Type.Transform(Type.String({ ...options, format: 'duration' }))
319
- .Decode(DurationType.transformer.decode)
320
- .Encode(DurationType.transformer.encode)
321
- }
322
-
323
- nullable() {
324
- return new DurationType(...this._with({ isNullable: true }))
325
- }
326
-
327
- optional() {
328
- return new DurationType(...this._with({ isOptional: true }))
329
- }
330
-
331
- nullish() {
332
- return new DurationType(
333
- ...this._with({ isNullable: true, isOptional: true }),
93
+ static factory() {
94
+ return CustomType.factory(
95
+ DurationType.transformer.decode,
96
+ DurationType.transformer.encode,
97
+ Type.String(),
334
98
  )
335
99
  }
336
-
337
- default(value: Temporal.Duration) {
338
- return new DurationType(
339
- ...this._with({
340
- options: { default: DurationType.transformer.encode(value) },
341
- hasDefault: true,
342
- }),
343
- )
344
- }
345
-
346
- description(description: string) {
347
- return new DurationType(...this._with({ options: { description } }))
348
- }
349
-
350
- examples(...examples: string[]) {
351
- return new DurationType(...this._with({ options: { examples } }))
352
- }
353
100
  }
354
101
 
355
- export class PlainYearMonthType<
356
- N extends boolean = false,
357
- O extends boolean = false,
358
- D extends boolean = false,
359
- > extends BaseType<
360
- TTransform<TString, Temporal.PlainYearMonth>,
361
- N,
362
- O,
363
- D,
364
- StringOptions
102
+ export class PlainYearMonthType extends TransformType<
103
+ Temporal.PlainYearMonth,
104
+ TString
365
105
  > {
366
106
  static readonly transformer = createTemporalTransformer('PlainYearMonth')
367
107
 
368
- constructor(
369
- options: StringOptions = {},
370
- isNullable: N = false as N,
371
- isOptional: O = false as O,
372
- hasDefault: D = false as D,
373
- ) {
374
- super(options, isNullable, isOptional, hasDefault)
375
- }
376
-
377
- protected _constructSchema(
378
- options: SchemaOptions,
379
- ): TTransform<TString, Temporal.PlainYearMonth> {
380
- return Type.Transform(
381
- Type.String({
382
- ...options,
383
- // TODO: duration format, or regex?
384
- }),
108
+ static factory() {
109
+ return CustomType.factory(
110
+ PlainYearMonthType.transformer.decode,
111
+ PlainYearMonthType.transformer.encode,
112
+ Type.String(),
385
113
  )
386
- .Decode(PlainYearMonthType.transformer.decode)
387
- .Encode(PlainYearMonthType.transformer.encode)
388
- }
389
-
390
- nullable() {
391
- return new PlainYearMonthType(...this._with({ isNullable: true }))
392
- }
393
-
394
- optional() {
395
- return new PlainYearMonthType(...this._with({ isOptional: true }))
396
- }
397
-
398
- nullish() {
399
- return new PlainYearMonthType(
400
- ...this._with({ isNullable: true, isOptional: true }),
401
- )
402
- }
403
-
404
- default(value: Temporal.PlainYearMonth) {
405
- return new PlainYearMonthType(
406
- ...this._with({
407
- options: { default: PlainYearMonthType.transformer.encode(value) },
408
- hasDefault: true,
409
- }),
410
- )
411
- }
412
-
413
- description(description: string) {
414
- return new PlainYearMonthType(...this._with({ options: { description } }))
415
- }
416
-
417
- examples(...examples: string[]) {
418
- return new PlainYearMonthType(...this._with({ options: { examples } }))
419
114
  }
420
115
  }
421
116
 
422
- export class PlainMonthDayType<
423
- N extends boolean = false,
424
- O extends boolean = false,
425
- D extends boolean = false,
426
- > extends BaseType<
427
- TTransform<TString, Temporal.PlainMonthDay>,
428
- N,
429
- O,
430
- D,
431
- StringOptions
117
+ export class PlainMonthDayType extends TransformType<
118
+ Temporal.PlainMonthDay,
119
+ TString
432
120
  > {
433
121
  static readonly transformer = createTemporalTransformer('PlainMonthDay')
434
122
 
435
- constructor(
436
- options: StringOptions = {},
437
- isNullable: N = false as N,
438
- isOptional: O = false as O,
439
- hasDefault: D = false as D,
440
- ) {
441
- super(options, isNullable, isOptional, hasDefault)
442
- }
443
-
444
- protected _constructSchema(
445
- options: SchemaOptions,
446
- ): TTransform<TString, Temporal.PlainMonthDay> {
447
- return Type.Transform(
448
- Type.String({
449
- ...options,
450
- // TODO: duration format, or regex?
451
- }),
123
+ static factory() {
124
+ return CustomType.factory(
125
+ PlainMonthDayType.transformer.decode,
126
+ PlainMonthDayType.transformer.encode,
127
+ Type.String(),
452
128
  )
453
- .Decode(PlainMonthDayType.transformer.decode)
454
- .Encode(PlainMonthDayType.transformer.encode)
455
- }
456
-
457
- nullable() {
458
- return new PlainMonthDayType(...this._with({ isNullable: true }))
459
- }
460
-
461
- optional() {
462
- return new PlainMonthDayType(...this._with({ isOptional: true }))
463
- }
464
-
465
- nullish() {
466
- return new PlainMonthDayType(
467
- ...this._with({ isNullable: true, isOptional: true }),
468
- )
469
- }
470
-
471
- default(value: Temporal.PlainMonthDay) {
472
- return new PlainMonthDayType(
473
- ...this._with({
474
- options: { default: PlainMonthDayType.transformer.encode(value) },
475
- hasDefault: true,
476
- }),
477
- )
478
- }
479
-
480
- description(description: string) {
481
- return new PlainMonthDayType(...this._with({ options: { description } }))
482
- }
483
-
484
- examples(...examples: string[]) {
485
- return new PlainMonthDayType(...this._with({ options: { examples } }))
486
129
  }
487
130
  }