@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.
- package/dist/compiler.js +84 -38
- package/dist/compiler.js.map +1 -1
- package/dist/formats.js +1 -1
- package/dist/formats.js.map +1 -1
- package/dist/index.js +53 -22
- package/dist/index.js.map +1 -1
- package/dist/schemas/discriminated-union.js +9 -0
- package/dist/schemas/discriminated-union.js.map +1 -0
- package/dist/schemas/nullable.js +1 -6
- package/dist/schemas/nullable.js.map +1 -1
- package/dist/temporal.js +7 -7
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +3 -43
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +17 -63
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +78 -41
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +3 -43
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +8 -48
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +8 -0
- package/dist/types/date.js.map +1 -0
- package/dist/types/enum.js +10 -94
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +3 -43
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +3 -26
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +52 -186
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +10 -131
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +25 -65
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +23 -328
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +16 -90
- package/dist/types/union.js.map +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/compiler.ts +124 -41
- package/src/formats.ts +1 -1
- package/src/index.ts +145 -63
- package/src/schemas/discriminated-union.ts +49 -0
- package/src/schemas/nullable.ts +7 -13
- package/src/temporal.ts +8 -7
- package/src/types/any.ts +6 -46
- package/src/types/array.ts +38 -86
- package/src/types/base.ts +205 -81
- package/src/types/boolean.ts +13 -47
- package/src/types/custom.ts +21 -79
- package/src/types/date.ts +10 -0
- package/src/types/enum.ts +18 -107
- package/src/types/literal.ts +7 -63
- package/src/types/never.ts +6 -36
- package/src/types/number.ts +52 -188
- package/src/types/object.ts +61 -202
- package/src/types/string.ts +25 -61
- package/src/types/temporal.ts +53 -410
- package/src/types/union.ts +98 -138
- package/src/utils.ts +8 -0
- package/dist/constants.js +0 -2
- package/dist/constants.js.map +0 -1
- package/dist/types/datetime.js +0 -53
- package/dist/types/datetime.js.map +0 -1
- package/src/constants.ts +0 -5
- package/src/types/datetime.ts +0 -65
package/src/types/object.ts
CHANGED
|
@@ -1,229 +1,88 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type ObjectOptions,
|
|
3
3
|
type TObject,
|
|
4
|
-
type
|
|
4
|
+
type TRecordOrObject,
|
|
5
|
+
type TSchema,
|
|
5
6
|
Type,
|
|
6
7
|
} from '@sinclair/typebox'
|
|
7
|
-
import type
|
|
8
|
-
import type {
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
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
|
|
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][
|
|
22
|
-
|
|
23
|
-
O,
|
|
24
|
-
D
|
|
17
|
+
TObject<{ [K in keyof T]: T[K]['schema'] }>,
|
|
18
|
+
{ properties: T }
|
|
25
19
|
> {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
45
|
-
|
|
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
|
-
|
|
81
|
-
|
|
31
|
+
static factory<T extends ObjectTypeProps = ObjectTypeProps>(
|
|
32
|
+
properties: T,
|
|
33
|
+
options: ObjectOptions = {},
|
|
82
34
|
) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
128
|
-
|
|
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
|
-
|
|
139
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
}
|
package/src/types/string.ts
CHANGED
|
@@ -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
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
42
|
-
return
|
|
11
|
+
max(value: number) {
|
|
12
|
+
return StringType.factory({
|
|
13
|
+
...this.props.options,
|
|
14
|
+
maxLength: value,
|
|
15
|
+
})
|
|
43
16
|
}
|
|
44
17
|
|
|
45
|
-
|
|
46
|
-
return
|
|
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
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
71
|
-
...this.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
)
|
|
34
|
+
return StringType.factory({
|
|
35
|
+
...this.props.options,
|
|
36
|
+
format: undefined,
|
|
37
|
+
pattern,
|
|
38
|
+
})
|
|
75
39
|
}
|
|
76
40
|
|
|
77
41
|
email() {
|