@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,229 +1,88 @@
1
1
  import {
2
2
  type ObjectOptions,
3
3
  type TObject,
4
- type TRecord,
4
+ type TRecordOrObject,
5
+ type TSchema,
5
6
  Type,
6
7
  } from '@sinclair/typebox'
7
- import type { typeStatic } from '../constants.ts'
8
- import type { UnionToTupleString } from '../utils.ts'
9
- import { BaseType, getTypeSchema } from './base.ts'
10
- import { type AnyEnumType, type AnyObjectEnumType, EnumType } from './enum.ts'
11
- import type { AnyLiteralType } from './literal.ts'
12
- import type { AnyStringType } from './string.ts'
13
- import type { AnyUnionType } from './union.ts'
8
+ import { BaseType, type BaseTypeAny } from './base.ts'
9
+ import type { EnumType, ObjectEnumType } from './enum.ts'
10
+ import type { LiteralType } from './literal.ts'
11
+ import type { StringType } from './string.ts'
14
12
 
13
+ export type ObjectTypeProps = { [k: string]: BaseTypeAny<any> }
15
14
  export class ObjectType<
16
- T extends Record<string, BaseType>,
17
- N extends boolean = false,
18
- O extends boolean = false,
19
- D extends boolean = false,
15
+ T extends ObjectTypeProps = ObjectTypeProps,
20
16
  > extends BaseType<
21
- TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,
22
- N,
23
- O,
24
- D
17
+ TObject<{ [K in keyof T]: T[K]['schema'] }>,
18
+ { properties: T }
25
19
  > {
26
- constructor(
27
- protected readonly properties: T = {} as T,
28
- options: ObjectOptions = {},
29
- isNullable: N = false as N,
30
- isOptional: O = false as O,
31
- hasDefault: D = false as D,
32
- ) {
33
- super(options, isNullable, isOptional, hasDefault, properties)
34
- }
35
-
36
- protected _constructSchema(
37
- options: ObjectOptions,
38
- properties: T,
39
- ): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {
40
- const schemaProperties = {} as {
41
- [K in keyof T]: T[K][typeStatic]['schema']
20
+ declare _: {
21
+ encoded: {
22
+ input: TObject<{ [K in keyof T]: T[K]['_']['encoded']['input'] }>
23
+ output: TObject<{ [K in keyof T]: T[K]['_']['encoded']['output'] }>
42
24
  }
43
-
44
- for (const [key, value] of Object.entries(properties)) {
45
- // @ts-expect-error
46
- schemaProperties[key] = getTypeSchema(value)
25
+ decoded: {
26
+ input: TObject<{ [K in keyof T]: T[K]['_']['decoded']['input'] }>
27
+ output: TObject<{ [K in keyof T]: T[K]['_']['decoded']['output'] }>
47
28
  }
48
- return Type.Object(schemaProperties, options)
49
- }
50
-
51
- nullable() {
52
- return new ObjectType(this.properties, ...this._with({ isNullable: true }))
53
- }
54
-
55
- optional() {
56
- return new ObjectType(this.properties, ...this._with({ isOptional: true }))
57
- }
58
-
59
- nullish() {
60
- return new ObjectType(
61
- this.properties,
62
- ...this._with({ isNullable: true, isOptional: true }),
63
- )
64
- }
65
-
66
- default(value: this[typeStatic]['encoded']) {
67
- return new ObjectType(
68
- this.properties,
69
- ...this._with({ options: { default: value }, hasDefault: true }),
70
- )
71
- }
72
-
73
- description(description: string) {
74
- return new ObjectType(
75
- this.properties,
76
- ...this._with({ options: { description } }),
77
- )
78
29
  }
79
30
 
80
- examples(
81
- ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
31
+ static factory<T extends ObjectTypeProps = ObjectTypeProps>(
32
+ properties: T,
33
+ options: ObjectOptions = {},
82
34
  ) {
83
- return new ObjectType(
84
- this.properties,
85
- ...this._with({ options: { examples } }),
86
- )
87
- }
88
-
89
- pick<P extends { [K in keyof T]?: true }>(pick: P) {
90
- const properties = Object.fromEntries(
91
- Object.entries(this.properties).filter(([key]) => pick[key]),
92
- )
93
- const [_, ...args] = this._with()
94
- return new ObjectType(
95
- properties as Pick<T, Extract<keyof P, keyof T>>,
96
- {},
97
- ...args,
98
- )
99
- }
100
-
101
- omit<P extends { [K in keyof T]?: true }>(omit: P) {
102
- const properties = Object.fromEntries(
103
- Object.entries(this.properties).filter(([key]) => !omit[key]),
104
- )
105
- const [_, ...args] = this._with()
106
- return new ObjectType(
107
- properties as Omit<T, Extract<keyof P, keyof T>>,
108
- {},
109
- ...args,
110
- )
111
- }
112
-
113
- extend<P extends Record<string, BaseType>>(properties: P) {
114
- const [_, ...args] = this._with()
115
- return new ObjectType({ ...this.properties, ...properties }, {}, ...args)
116
- }
117
-
118
- merge<T extends ObjectType<Record<string, BaseType>>>(object: T) {
119
- const [_, ...args] = this._with()
120
- return new ObjectType(
121
- { ...this.properties, ...object.properties },
122
- {},
123
- ...args,
124
- )
125
- }
35
+ const schemaProperties = {} as {
36
+ [K in keyof T]: T[K]['schema']
37
+ }
126
38
 
127
- partial() {
128
- const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =
129
- {} as any
130
- for (const [key, value] of Object.entries(this.properties)) {
131
- // @ts-expect-error
132
- properties[key] = value.optional()
39
+ for (const key in properties) {
40
+ schemaProperties[key] = properties[key].schema
133
41
  }
134
- const [_, ...args] = this._with()
135
- return new ObjectType(properties, {}, ...args)
136
- }
137
42
 
138
- keyof(): EnumType<UnionToTupleString<keyof T>> {
139
- return new EnumType(Object.keys(this.properties) as any)
43
+ return new ObjectType<T>(Type.Object(schemaProperties, options) as any, {
44
+ properties,
45
+ })
140
46
  }
141
47
  }
142
48
 
143
49
  export class RecordType<
144
- K extends
145
- | AnyLiteralType
146
- | AnyEnumType
147
- | AnyObjectEnumType
148
- | AnyStringType
149
- | AnyUnionType,
50
+ K extends LiteralType | EnumType | ObjectEnumType | StringType,
150
51
  E extends BaseType,
151
- N extends boolean = false,
152
- O extends boolean = false,
153
- D extends boolean = false,
154
- > extends BaseType<
155
- TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>,
156
- N,
157
- O,
158
- D,
159
- ObjectOptions
160
- > {
161
- constructor(
162
- protected readonly key: K,
163
- protected readonly element: E,
164
- options: ObjectOptions = {},
165
- isNullable: N = false as N,
166
- isOptional: O = false as O,
167
- hasDefault: D = false as D,
168
- ) {
169
- super(options, isNullable, isOptional, hasDefault, key, element)
170
- }
171
-
172
- protected _constructSchema(options: ObjectOptions, key: K, element: E) {
173
- return Type.Record(
174
- getTypeSchema(key),
175
- getTypeSchema(element),
176
- options,
177
- ) as unknown as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>
178
- }
179
-
180
- nullable() {
181
- return new RecordType(
182
- this.key,
183
- this.element,
184
- ...this._with({ isNullable: true }),
185
- )
186
- }
187
-
188
- optional() {
189
- return new RecordType(
190
- this.key,
191
- this.element,
192
- ...this._with({ isOptional: true }),
193
- )
194
- }
195
-
196
- nullish() {
197
- return new RecordType(
198
- this.key,
199
- this.element,
200
- ...this._with({ isNullable: true, isOptional: true }),
201
- )
202
- }
203
-
204
- default(value: this[typeStatic]['encoded']) {
205
- return new RecordType(
206
- this.key,
207
- this.element,
208
- ...this._with({ options: { default: value }, hasDefault: true }),
209
- )
210
- }
211
-
212
- description(description: string) {
213
- return new RecordType(
214
- this.key,
215
- this.element,
216
- ...this._with({ options: { description } }),
217
- )
52
+ > extends BaseType<TRecordOrObject<K['schema'], E['schema']>> {
53
+ _!: {
54
+ encoded: {
55
+ input: TRecordOrObject<
56
+ K['_']['encoded']['input'],
57
+ E['_']['encoded']['input']
58
+ >
59
+ output: TRecordOrObject<
60
+ K['_']['encoded']['output'],
61
+ E['_']['encoded']['output']
62
+ >
63
+ }
64
+ decoded: {
65
+ input: TRecordOrObject<
66
+ K['_']['decoded']['input'],
67
+ E['_']['decoded']['input']
68
+ >
69
+ output: TRecordOrObject<
70
+ K['_']['decoded']['output'],
71
+ E['_']['decoded']['output']
72
+ >
73
+ }
218
74
  }
219
75
 
220
- examples(
221
- ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
222
- ) {
223
- return new RecordType(
224
- this.key,
225
- this.element,
226
- ...this._with({ options: { examples } }),
76
+ static factory<
77
+ K extends
78
+ | LiteralType<any>
79
+ | EnumType<any>
80
+ | ObjectEnumType<any>
81
+ | StringType,
82
+ E extends BaseType,
83
+ >(key: K, element: E, options: ObjectOptions = {}) {
84
+ return new RecordType<K, E>(
85
+ Type.Record(key.schema, element.schema, options) as any,
227
86
  )
228
87
  }
229
88
  }
@@ -1,77 +1,41 @@
1
1
  import { type StringOptions, type TString, Type } from '@sinclair/typebox'
2
- import { BaseType } from './base.ts'
2
+ import { BaseType, type ConstantType, type TypeParams } from './base.ts'
3
3
 
4
- export type AnyStringType = StringType<boolean, boolean, boolean>
5
- export class StringType<
6
- N extends boolean = false,
7
- O extends boolean = false,
8
- D extends boolean = false,
9
- > extends BaseType<TString, N, O, D, StringOptions> {
10
- constructor(
11
- options: StringOptions = {},
12
- isNullable: N = false as N,
13
- isOptional: O = false as O,
14
- hasDefault: D = false as D,
15
- ) {
16
- super(options, isNullable, isOptional, hasDefault)
17
- }
18
-
19
- protected _constructSchema(options: StringOptions): TString {
20
- return Type.String(options)
21
- }
22
-
23
- nullable() {
24
- return new StringType(...this._with({ isNullable: true }))
25
- }
26
-
27
- optional() {
28
- return new StringType(...this._with({ isOptional: true }))
29
- }
30
-
31
- nullish() {
32
- return new StringType(...this._with({ isNullable: true, isOptional: true }))
33
- }
4
+ export class StringType extends BaseType<TString, { options: StringOptions }> {
5
+ _!: ConstantType<TString>
34
6
 
35
- default(value: string) {
36
- return new StringType(
37
- ...this._with({ options: { default: value }, hasDefault: true }),
38
- )
7
+ static factory(options: StringOptions = {}) {
8
+ return new StringType(Type.String(options), { options })
39
9
  }
40
10
 
41
- description(description: string) {
42
- return new StringType(...this._with({ options: { description } }))
11
+ max(value: number) {
12
+ return StringType.factory({
13
+ ...this.props.options,
14
+ maxLength: value,
15
+ })
43
16
  }
44
17
 
45
- examples(...examples: string[]) {
46
- return new StringType(...this._with({ options: { examples } }))
18
+ min(value: number) {
19
+ return StringType.factory({
20
+ ...this.props.options,
21
+ minLength: value,
22
+ })
47
23
  }
48
24
 
49
25
  format(format: TString['format']) {
50
- return new StringType(...this._with({ options: { format } }))
51
- }
52
-
53
- max(value: number) {
54
- return new StringType(
55
- ...this._with({
56
- options: { maxLength: value },
57
- }),
58
- )
59
- }
60
-
61
- min(value: number) {
62
- return new StringType(
63
- ...this._with({
64
- options: { minLength: value },
65
- }),
66
- )
26
+ return StringType.factory({
27
+ ...this.props.options,
28
+ pattern: undefined,
29
+ format,
30
+ })
67
31
  }
68
32
 
69
33
  pattern(pattern: string) {
70
- return new StringType(
71
- ...this._with({
72
- options: { pattern },
73
- }),
74
- )
34
+ return StringType.factory({
35
+ ...this.props.options,
36
+ format: undefined,
37
+ pattern,
38
+ })
75
39
  }
76
40
 
77
41
  email() {