@nmtjs/type 0.2.1 → 0.3.1

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 (55) hide show
  1. package/dist/compiler.js +2 -2
  2. package/dist/compiler.js.map +1 -1
  3. package/dist/constants.js +2 -0
  4. package/dist/constants.js.map +1 -0
  5. package/dist/index.js +2 -3
  6. package/dist/index.js.map +1 -1
  7. package/dist/temporal.js +3 -1
  8. package/dist/temporal.js.map +1 -1
  9. package/dist/types/any.js +37 -5
  10. package/dist/types/any.js.map +1 -1
  11. package/dist/types/array.js +55 -24
  12. package/dist/types/array.js.map +1 -1
  13. package/dist/types/base.js +31 -62
  14. package/dist/types/base.js.map +1 -1
  15. package/dist/types/boolean.js +37 -5
  16. package/dist/types/boolean.js.map +1 -1
  17. package/dist/types/custom.js +37 -8
  18. package/dist/types/custom.js.map +1 -1
  19. package/dist/types/datetime.js +41 -22
  20. package/dist/types/datetime.js.map +1 -1
  21. package/dist/types/enum.js +74 -16
  22. package/dist/types/enum.js.map +1 -1
  23. package/dist/types/literal.js +35 -8
  24. package/dist/types/literal.js.map +1 -1
  25. package/dist/types/never.js +17 -2
  26. package/dist/types/never.js.map +1 -1
  27. package/dist/types/number.js +116 -62
  28. package/dist/types/number.js.map +1 -1
  29. package/dist/types/object.js +58 -17
  30. package/dist/types/object.js.map +1 -1
  31. package/dist/types/string.js +57 -21
  32. package/dist/types/string.js.map +1 -1
  33. package/dist/types/temporal.js +292 -74
  34. package/dist/types/temporal.js.map +1 -1
  35. package/dist/types/union.js +75 -17
  36. package/dist/types/union.js.map +1 -1
  37. package/package.json +3 -3
  38. package/src/compiler.ts +2 -2
  39. package/src/constants.ts +5 -0
  40. package/src/index.ts +5 -6
  41. package/src/temporal.ts +4 -2
  42. package/src/types/any.ts +32 -9
  43. package/src/types/array.ts +59 -28
  44. package/src/types/base.ts +59 -112
  45. package/src/types/boolean.ts +31 -9
  46. package/src/types/custom.ts +61 -24
  47. package/src/types/datetime.ts +40 -35
  48. package/src/types/enum.ts +78 -21
  49. package/src/types/literal.ts +42 -12
  50. package/src/types/never.ts +24 -11
  51. package/src/types/number.ts +103 -67
  52. package/src/types/object.ts +87 -32
  53. package/src/types/string.ts +38 -30
  54. package/src/types/temporal.ts +378 -118
  55. package/src/types/union.ts +103 -31
@@ -1,6 +1,13 @@
1
- import { type TIntersect, type TUnion, Type } from '@sinclair/typebox'
1
+ import {
2
+ type SchemaOptions,
3
+ type TIntersect,
4
+ type TUnion,
5
+ Type,
6
+ // type UnionToTuple,
7
+ } from '@sinclair/typebox'
8
+ import type { typeStatic } from '../constants.ts'
2
9
  import type { UnionToTuple } from '../utils.ts'
3
- import { BaseType, typeFinalSchema } from './base.ts'
10
+ import { BaseType, getTypeSchema } from './base.ts'
4
11
 
5
12
  export class UnionType<
6
13
  T extends [BaseType, BaseType, ...BaseType[]] = [
@@ -10,33 +17,64 @@ export class UnionType<
10
17
  ],
11
18
  N extends boolean = false,
12
19
  O extends boolean = false,
13
- // @ts-expect-error
14
- > extends BaseType<TUnion<UnionToTuple<T[number][typeFinal]>>, N, O> {
20
+ D extends boolean = false,
21
+ > extends BaseType<
22
+ //@ts-expect-error
23
+ TUnion<UnionToTuple<T[number][typeStatic]['schema']>>,
24
+ N,
25
+ O,
26
+ D
27
+ > {
15
28
  constructor(
16
29
  readonly types: T,
17
- nullable: N = false as N,
18
- optional: O = false as O,
30
+ options: SchemaOptions = {},
31
+ isNullable: N = false as N,
32
+ isOptional: O = false as O,
33
+ hasDefault: D = false as D,
19
34
  ) {
20
- super(
21
- Type.Union(types.map((t) => t[typeFinalSchema])) as any,
22
- nullable,
23
- optional,
24
- )
35
+ super(options, isNullable, isOptional, hasDefault)
36
+ }
37
+
38
+ protected _constructSchema(
39
+ options: SchemaOptions,
40
+ //@ts-expect-error
41
+ ): TUnion<UnionToTuple<T[number][typeStatic]['schema']>> {
42
+ return Type.Union(this.types.map(getTypeSchema), options) as any
25
43
  }
26
44
 
27
45
  nullable() {
28
- const [_, ...args] = this._nullable()
29
- return new UnionType(this.types, ...args)
46
+ return new UnionType(this.types, ...this._with({ isNullable: true }))
30
47
  }
31
48
 
32
49
  optional() {
33
- const [_, ...args] = this._optional()
34
- return new UnionType(this.types, ...args)
50
+ return new UnionType(this.types, ...this._with({ isOptional: true }))
35
51
  }
36
52
 
37
53
  nullish() {
38
- const [_, ...args] = this._nullish()
39
- return new UnionType(this.types, ...args)
54
+ return new UnionType(
55
+ this.types,
56
+ ...this._with({ isNullable: true, isOptional: true }),
57
+ )
58
+ }
59
+
60
+ default(value: this[typeStatic]['encoded']) {
61
+ return new UnionType(
62
+ this.types,
63
+ ...this._with({ options: { default: value }, hasDefault: true }),
64
+ )
65
+ }
66
+
67
+ description(description: string) {
68
+ return new UnionType(
69
+ this.types,
70
+ ...this._with({ options: { description } }),
71
+ )
72
+ }
73
+
74
+ examples(
75
+ ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
76
+ ) {
77
+ return new UnionType(this.types, ...this._with({ options: { examples } }))
40
78
  }
41
79
  }
42
80
 
@@ -48,32 +86,66 @@ export class IntersactionType<
48
86
  ],
49
87
  N extends boolean = false,
50
88
  O extends boolean = false,
89
+ D extends boolean = false,
90
+ > extends BaseType<
51
91
  // @ts-expect-error
52
- > extends BaseType<TIntersect<UnionToTuple<T[number][typeFinal]>>, N, O> {
92
+ TIntersect<UnionToTuple<T[number][typeStatic]['schema']>>,
93
+ N,
94
+ O,
95
+ D
96
+ > {
53
97
  constructor(
54
98
  readonly types: T,
55
- nullable: N = false as N,
56
- optional: O = false as O,
99
+ options: SchemaOptions = {},
100
+ isNullable: N = false as N,
101
+ isOptional: O = false as O,
102
+ hasDefault: D = false as D,
57
103
  ) {
58
- super(
59
- Type.Intersect(types.map((t) => t[typeFinalSchema])) as any,
60
- nullable,
61
- optional,
62
- )
104
+ super(options, isNullable, isOptional, hasDefault)
105
+ }
106
+
107
+ protected _constructSchema(
108
+ options: SchemaOptions,
109
+ // @ts-expect-error
110
+ ): TIntersect<UnionToTuple<T[number][typeStatic]['schema']>> {
111
+ return Type.Intersect(this.types.map(getTypeSchema), options) as any
63
112
  }
64
113
 
65
114
  nullable() {
66
- const [_, ...args] = this._nullable()
67
- return new IntersactionType(this.types, ...args)
115
+ return new IntersactionType(this.types, ...this._with({ isNullable: true }))
68
116
  }
69
117
 
70
118
  optional() {
71
- const [_, ...args] = this._optional()
72
- return new IntersactionType(this.types, ...args)
119
+ return new IntersactionType(this.types, ...this._with({ isOptional: true }))
73
120
  }
74
121
 
75
122
  nullish() {
76
- const [_, ...args] = this._nullish()
77
- return new IntersactionType(this.types, ...args)
123
+ return new IntersactionType(
124
+ this.types,
125
+ ...this._with({ isNullable: true, isOptional: true }),
126
+ )
127
+ }
128
+
129
+ default(value: this[typeStatic]['encoded']) {
130
+ return new IntersactionType(
131
+ this.types,
132
+ ...this._with({ options: { default: value }, hasDefault: true }),
133
+ )
134
+ }
135
+
136
+ description(description: string) {
137
+ return new IntersactionType(
138
+ this.types,
139
+ ...this._with({ options: { description } }),
140
+ )
141
+ }
142
+
143
+ examples(
144
+ ...values: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
145
+ ) {
146
+ return new IntersactionType(
147
+ this.types,
148
+ ...this._with({ options: { examples: values } }),
149
+ )
78
150
  }
79
151
  }