@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
package/src/types/enum.ts CHANGED
@@ -1,119 +1,30 @@
1
- import { Enum, type SchemaOptions, type TEnum } from '@sinclair/typebox'
2
- import { BaseType } from './base.ts'
1
+ import {
2
+ Enum,
3
+ type SchemaOptions,
4
+ type StaticDecode,
5
+ type TEnum,
6
+ Type,
7
+ } from '@sinclair/typebox'
8
+ import { BaseType, type ConstantType } from './base.ts'
3
9
 
4
- export type AnyObjectEnumType<T extends { [K in string]: K } = any> =
5
- ObjectEnumType<T, boolean, boolean, boolean>
6
10
  export class ObjectEnumType<
7
- T extends { [K in string]: K },
8
- N extends boolean = false,
9
- O extends boolean = false,
10
- D extends boolean = false,
11
- > extends BaseType<TEnum<T>, N, O, D> {
12
- constructor(
13
- private readonly values: T,
14
- options: SchemaOptions = {},
15
- isNullable: N = false as N,
16
- isOptional: O = false as O,
17
- hasDefault: D = false as D,
18
- ) {
19
- super(options, isNullable, isOptional, hasDefault, values)
20
- }
21
-
22
- protected _constructSchema(options: SchemaOptions, values: T): TEnum<T> {
23
- return Enum(values, options)
24
- }
25
-
26
- nullable() {
27
- return new ObjectEnumType(this.values, ...this._with({ isNullable: true }))
28
- }
29
-
30
- optional() {
31
- return new ObjectEnumType(this.values, ...this._with({ isOptional: true }))
32
- }
33
-
34
- nullish() {
35
- return new ObjectEnumType(
36
- this.values,
37
- ...this._with({ isNullable: true, isOptional: true }),
38
- )
39
- }
40
-
41
- default(value: keyof T) {
42
- return new ObjectEnumType(
43
- this.values,
44
- ...this._with({ options: { default: value }, hasDefault: true }),
45
- )
46
- }
47
-
48
- description(description: string) {
49
- return new ObjectEnumType(
50
- this.values,
51
- ...this._with({ options: { description } }),
52
- )
53
- }
11
+ T extends { [K in string]: K } = { [K in string]: K },
12
+ > extends BaseType<TEnum<T>> {
13
+ _!: ConstantType<this['schema']>
54
14
 
55
- examples(...examples: (keyof T)[]) {
56
- return new ObjectEnumType(
57
- this.values,
58
- ...this._with({ options: { examples } }),
59
- )
15
+ static factory<T extends { [K in string]: K }>(values: T) {
16
+ return new ObjectEnumType<T>(Type.Enum(values as any))
60
17
  }
61
18
  }
62
19
 
63
- export type AnyEnumType = EnumType<any, boolean, boolean, boolean>
64
20
  export class EnumType<
65
21
  T extends (string | number)[] = (string | number)[],
66
- N extends boolean = false,
67
- O extends boolean = false,
68
- D extends boolean = false,
69
- > extends BaseType<TEnum<Record<string, T[number]>>, N, O, D> {
70
- constructor(
71
- private readonly values: [...T],
72
- options: SchemaOptions = {},
73
- isNullable: N = false as N,
74
- isOptional: O = false as O,
75
- hasDefault: D = false as D,
76
- ) {
77
- super(options, isNullable, isOptional, hasDefault, values)
78
- }
79
-
80
- protected _constructSchema(
81
- options: SchemaOptions,
82
- values: [...T],
83
- ): TEnum<Record<string, T[number]>> {
84
- return Enum(Object.fromEntries(values.map((k) => [k, k])), options)
85
- }
86
-
87
- nullable() {
88
- return new EnumType(this.values, ...this._with({ isNullable: true }))
89
- }
90
-
91
- optional() {
92
- return new EnumType(this.values, ...this._with({ isOptional: true }))
93
- }
22
+ > extends BaseType<TEnum<Record<string, T[number]>>> {
23
+ _!: ConstantType<this['schema']>
94
24
 
95
- nullish() {
96
- return new EnumType(
97
- this.values,
98
- ...this._with({ isNullable: true, isOptional: true }),
25
+ static factory<T extends (string | number)[]>(values: [...T]) {
26
+ return new EnumType<T>(
27
+ Type.Enum(Object.fromEntries(values.map((v) => [v, v])) as any),
99
28
  )
100
29
  }
101
-
102
- default(value: T[number]) {
103
- return new EnumType(
104
- this.values,
105
- ...this._with({ options: { default: value }, hasDefault: true }),
106
- )
107
- }
108
-
109
- description(description: string) {
110
- return new EnumType(
111
- this.values,
112
- ...this._with({ options: { description } }),
113
- )
114
- }
115
-
116
- examples(...examples: [T[number], ...T[number][]]) {
117
- return new EnumType(this.values, ...this._with({ options: { examples } }))
118
- }
119
30
  }
@@ -1,68 +1,12 @@
1
- import {
2
- Extends,
3
- type SchemaOptions,
4
- type TLiteral,
5
- type TLiteralValue,
6
- Type,
7
- } from '@sinclair/typebox'
8
- import { BaseType } from './base.ts'
1
+ import { type TLiteral, type TLiteralValue, Type } from '@sinclair/typebox'
2
+ import { BaseType, type ConstantType } from './base.ts'
9
3
 
10
- export type AnyLiteralType<T extends TLiteralValue = any> = LiteralType<
11
- T,
12
- boolean,
13
- boolean,
14
- boolean
15
- >
16
4
  export class LiteralType<
17
- T extends TLiteralValue,
18
- N extends boolean = false,
19
- O extends boolean = false,
20
- D extends boolean = false,
21
- > extends BaseType<TLiteral<T>, N, O, D> {
22
- constructor(
23
- protected readonly value: T,
24
- options: SchemaOptions = {},
25
- isNullable: N = false as N,
26
- isOptional: O = false as O,
27
- hasDefault: D = false as D,
28
- ) {
29
- super(options, isNullable, isOptional, hasDefault, value)
30
- }
31
-
32
- protected _constructSchema(options: SchemaOptions, value: T): TLiteral<T> {
33
- return Type.Literal(value, options)
34
- }
35
-
36
- nullable() {
37
- return new LiteralType(this.value, this.options, true, this.isOptional)
38
- }
39
-
40
- optional() {
41
- return new LiteralType(this.value, ...this._with({ isOptional: true }))
42
- }
43
-
44
- nullish() {
45
- return new LiteralType(
46
- this.value,
47
- ...this._with({ isNullable: true, isOptional: true }),
48
- )
49
- }
50
-
51
- default(value: T = this.value) {
52
- return new LiteralType(
53
- this.value,
54
- ...this._with({ options: { default: value }, hasDefault: true }),
55
- )
56
- }
57
-
58
- description(description: string) {
59
- return new LiteralType(
60
- this.value,
61
- ...this._with({ options: { description } }),
62
- )
63
- }
5
+ T extends TLiteralValue = TLiteralValue,
6
+ > extends BaseType<TLiteral<T>> {
7
+ _!: ConstantType<this['schema']>
64
8
 
65
- examples(...examples: [T, ...T[]]) {
66
- return new LiteralType(this.value, ...this._with({ options: { examples } }))
9
+ static factory<T extends TLiteralValue>(value: T) {
10
+ return new LiteralType<T>(Type.Literal(value))
67
11
  }
68
12
  }
@@ -1,40 +1,10 @@
1
- import { type SchemaOptions, type TNever, Type } from '@sinclair/typebox'
2
- import { BaseType } from './base.ts'
1
+ import { type TNever, Type } from '@sinclair/typebox'
2
+ import { BaseType, type ConstantType } from './base.ts'
3
3
 
4
- export class NeverType<
5
- N extends boolean = false,
6
- O extends boolean = false,
7
- D extends boolean = false,
8
- > extends BaseType<TNever, N, O, D> {
9
- constructor(options: SchemaOptions = {}) {
10
- super(options, false as N, false as O, false as D)
11
- }
12
-
13
- protected _constructSchema(options: SchemaOptions): TNever {
14
- return Type.Never(options)
15
- }
16
-
17
- nullable(): NeverType<true, O, D> {
18
- throw new Error('NeverType cannot be nullable')
19
- }
20
-
21
- optional(): NeverType<N, true, D> {
22
- throw new Error('NeverType cannot be optional')
23
- }
24
-
25
- nullish(): NeverType<true, true, D> {
26
- throw new Error('NeverType cannot be nullish')
27
- }
28
-
29
- default(): NeverType<N, O, true> {
30
- throw new Error('NeverType cannot have a default value')
31
- }
32
-
33
- description(description: string): NeverType<N, O, D> {
34
- return new NeverType({ ...this.options, description })
35
- }
4
+ export class NeverType extends BaseType<TNever> {
5
+ _!: ConstantType<this['schema']>
36
6
 
37
- examples(): NeverType<N, O, D> {
38
- throw new Error('NeverType cannot have examples')
7
+ static factory() {
8
+ return new NeverType(Type.Never())
39
9
  }
40
10
  }
@@ -1,7 +1,11 @@
1
1
  import {
2
+ type BigIntOptions,
2
3
  type IntegerOptions,
3
4
  type NumberOptions,
5
+ type Static,
6
+ type StaticDecode,
4
7
  type StringOptions,
8
+ type TBigInt,
5
9
  type TInteger,
6
10
  type TNumber,
7
11
  type TString,
@@ -9,50 +13,14 @@ import {
9
13
  Type,
10
14
  TypeBoxError,
11
15
  } from '@sinclair/typebox'
12
- import { BaseType } from './base.ts'
13
-
14
- export class NumberType<
15
- N extends boolean = false,
16
- O extends boolean = false,
17
- D extends boolean = false,
18
- > extends BaseType<TNumber, N, O, D, NumberOptions> {
19
- constructor(
20
- protected readonly options: NumberOptions = {},
21
- isNullable: N = false as N,
22
- isOptional: O = false as O,
23
- hasDefault: D = false as D,
24
- ) {
25
- super(options, isNullable, isOptional, hasDefault)
26
- }
27
-
28
- protected _constructSchema(options: NumberOptions): TNumber {
29
- return Type.Number(options)
30
- }
31
-
32
- nullable() {
33
- return new NumberType(...this._with({ isNullable: true }))
34
- }
35
-
36
- optional() {
37
- return new NumberType(...this._with({ isOptional: true }))
38
- }
39
-
40
- nullish() {
41
- return new NumberType(...this._with({ isNullable: true, isOptional: true }))
42
- }
43
-
44
- default(value: number) {
45
- return new NumberType(
46
- ...this._with({ options: { default: value }, hasDefault: true }),
47
- )
48
- }
16
+ import { BaseType, type ConstantType } from './base.ts'
17
+ import { CustomType, TransformType } from './custom.ts'
49
18
 
50
- description(description: string) {
51
- return new NumberType(...this._with({ options: { description } }))
52
- }
19
+ export class NumberType extends BaseType<TNumber, { options: NumberOptions }> {
20
+ _!: ConstantType<this['schema']>
53
21
 
54
- examples(...examples: [number, ...number[]]) {
55
- return new NumberType(...this._with({ options: { examples } }))
22
+ static factory(options: NumberOptions = {}) {
23
+ return new NumberType(Type.Number(options), { options })
56
24
  }
57
25
 
58
26
  positive() {
@@ -64,72 +32,30 @@ export class NumberType<
64
32
  }
65
33
 
66
34
  max(value: number, exclusive?: true) {
67
- return new NumberType(
68
- ...this._with({
69
- options: {
70
- maximum: value,
71
- ...(!exclusive ? {} : { exclusiveMaximum: value }),
72
- },
73
- }),
74
- )
35
+ return NumberType.factory({
36
+ ...this.props.options,
37
+ maximum: value,
38
+ ...(!exclusive ? {} : { exclusiveMaximum: value }),
39
+ })
75
40
  }
76
41
 
77
42
  min(value: number, exclusive?: true) {
78
- return new NumberType(
79
- ...this._with({
80
- options: {
81
- minimum: value,
82
- ...(!exclusive ? {} : { exclusiveMinimum: value }),
83
- },
84
- }),
85
- )
43
+ return NumberType.factory({
44
+ ...this.props.options,
45
+ minimum: value,
46
+ ...(!exclusive ? {} : { exclusiveMinimum: value }),
47
+ })
86
48
  }
87
49
  }
88
50
 
89
- export class IntegerType<
90
- N extends boolean = false,
91
- O extends boolean = false,
92
- D extends boolean = false,
93
- > extends BaseType<TInteger, N, O, D, IntegerOptions> {
94
- constructor(
95
- options: IntegerOptions = {},
96
- isNullable: N = false as N,
97
- isOptional: O = false as O,
98
- hasDefault: D = false as D,
99
- ) {
100
- super(options, isNullable, isOptional, hasDefault)
101
- }
102
-
103
- protected _constructSchema(options: IntegerOptions): TInteger {
104
- return Type.Integer(options)
105
- }
106
-
107
- nullable() {
108
- return new IntegerType(...this._with({ isNullable: true }))
109
- }
110
-
111
- optional() {
112
- return new IntegerType(...this._with({ isOptional: true }))
113
- }
114
-
115
- nullish() {
116
- return new IntegerType(
117
- ...this._with({ isNullable: true, isOptional: true }),
118
- )
119
- }
120
-
121
- default(value: number) {
122
- return new IntegerType(
123
- ...this._with({ options: { default: value }, hasDefault: true }),
124
- )
125
- }
126
-
127
- description(description: string) {
128
- return new IntegerType(...this._with({ options: { description } }))
129
- }
51
+ export class IntegerType extends BaseType<
52
+ TInteger,
53
+ { options: IntegerOptions }
54
+ > {
55
+ _!: ConstantType<this['schema']>
130
56
 
131
- examples(...examples: [number, ...number[]]) {
132
- return new IntegerType(...this._with({ options: { examples } }))
57
+ static factory(options: IntegerOptions = {}) {
58
+ return new IntegerType(Type.Integer(options), { options })
133
59
  }
134
60
 
135
61
  positive() {
@@ -141,84 +67,28 @@ export class IntegerType<
141
67
  }
142
68
 
143
69
  max(value: number, exclusive?: true) {
144
- return new IntegerType(
145
- ...this._with({
146
- options: {
147
- maximum: value,
148
- ...(!exclusive ? {} : { exclusiveMaximum: value }),
149
- },
150
- }),
151
- )
70
+ return IntegerType.factory({
71
+ ...this.props.options,
72
+ maximum: value,
73
+ ...(!exclusive ? {} : { exclusiveMaximum: value }),
74
+ })
152
75
  }
153
76
 
154
77
  min(value: number, exclusive?: true) {
155
- return new IntegerType(
156
- ...this._with({
157
- options: {
158
- minimum: value,
159
- ...(!exclusive ? {} : { exclusiveMinimum: value }),
160
- },
161
- }),
162
- )
78
+ return IntegerType.factory({
79
+ ...this.props.options,
80
+ minimum: value,
81
+ ...(!exclusive ? {} : { exclusiveMinimum: value }),
82
+ })
163
83
  }
164
84
  }
165
85
 
166
- export class BigIntType<
167
- N extends boolean = false,
168
- O extends boolean = false,
169
- D extends boolean = false,
170
- > extends BaseType<TTransform<TString, bigint>, N, O, D, StringOptions> {
171
- constructor(
172
- options: StringOptions = {},
173
- isNullable: N = false as N,
174
- isOptional: O = false as O,
175
- hasDefault: D = false as D,
176
- ) {
177
- super(options, isNullable, isOptional, hasDefault)
178
- }
179
-
180
- protected _constructSchema(options: StringOptions) {
181
- return Type.Transform(Type.String({ ...options, pattern: '^\\d$' }))
182
- .Decode((v: string) => {
183
- try {
184
- return BigInt(v)
185
- } catch (error) {
186
- throw new TypeBoxError('Invalid bigint value')
187
- }
188
- })
189
- .Encode(this.encode)
190
- }
191
-
192
- protected encode = (value: bigint): string => {
193
- return value.toString()
194
- }
195
-
196
- nullable() {
197
- return new BigIntType(...this._with({ isNullable: true }))
198
- }
199
-
200
- optional() {
201
- return new BigIntType(...this._with({ isOptional: true }))
202
- }
203
-
204
- nullish() {
205
- return new BigIntType(...this._with({ isNullable: true, isOptional: true }))
206
- }
207
-
208
- default(value: bigint) {
209
- return new BigIntType(
210
- ...this._with({ options: { default: value }, hasDefault: true }),
211
- )
212
- }
213
-
214
- description(description: string) {
215
- return new BigIntType(...this._with({ options: { description } }))
216
- }
86
+ // TODO: this is not json schema compatible
87
+ export class BigIntType extends BaseType<TBigInt, { options: BigIntOptions }> {
88
+ _!: ConstantType<this['schema']>
217
89
 
218
- examples(...examples: [bigint, ...bigint[]]) {
219
- return new BigIntType(
220
- ...this._with({ options: { examples: examples.map(this.encode) } }),
221
- )
90
+ static factory(options: BigIntOptions = {}) {
91
+ return new BigIntType(Type.BigInt(options), { options })
222
92
  }
223
93
 
224
94
  positive() {
@@ -230,24 +100,18 @@ export class BigIntType<
230
100
  }
231
101
 
232
102
  max(value: bigint, exclusive?: true) {
233
- return new BigIntType(
234
- ...this._with({
235
- options: {
236
- maximum: this.encode(value),
237
- ...(!exclusive ? {} : { exclusiveMaximum: this.encode(value) }),
238
- },
239
- }),
240
- )
103
+ return BigIntType.factory({
104
+ ...this.props.options,
105
+ maximum: value,
106
+ ...(!exclusive ? {} : { exclusiveMaximum: value }),
107
+ })
241
108
  }
242
109
 
243
110
  min(value: bigint, exclusive?: true) {
244
- return new BigIntType(
245
- ...this._with({
246
- options: {
247
- minimum: `${value}`,
248
- ...(!exclusive ? {} : { exclusiveMinimum: `${value}` }),
249
- },
250
- }),
251
- )
111
+ return BigIntType.factory({
112
+ ...this.props.options,
113
+ minimum: value,
114
+ ...(!exclusive ? {} : { exclusiveMinimum: value }),
115
+ })
252
116
  }
253
117
  }