@nmtjs/type 0.2.1 → 0.3.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 +2 -2
- package/dist/compiler.js.map +1 -1
- package/dist/constants.js +2 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/temporal.js +3 -1
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +37 -5
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +55 -24
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +31 -62
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +37 -5
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +37 -8
- package/dist/types/custom.js.map +1 -1
- package/dist/types/datetime.js +41 -22
- package/dist/types/datetime.js.map +1 -1
- package/dist/types/enum.js +74 -16
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +35 -8
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +17 -2
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +116 -62
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +58 -17
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +57 -21
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +292 -74
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +75 -17
- package/dist/types/union.js.map +1 -1
- package/package.json +3 -3
- package/src/compiler.ts +2 -2
- package/src/constants.ts +5 -0
- package/src/index.ts +5 -6
- package/src/temporal.ts +4 -2
- package/src/types/any.ts +32 -9
- package/src/types/array.ts +59 -28
- package/src/types/base.ts +59 -112
- package/src/types/boolean.ts +31 -9
- package/src/types/custom.ts +61 -24
- package/src/types/datetime.ts +40 -35
- package/src/types/enum.ts +78 -21
- package/src/types/literal.ts +42 -12
- package/src/types/never.ts +24 -11
- package/src/types/number.ts +103 -67
- package/src/types/object.ts +87 -32
- package/src/types/string.ts +38 -30
- package/src/types/temporal.ts +378 -118
- package/src/types/union.ts +103 -31
package/src/types/union.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import {
|
|
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,
|
|
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
|
-
|
|
14
|
-
> extends BaseType<
|
|
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
|
-
|
|
18
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
39
|
-
|
|
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
|
-
|
|
92
|
+
TIntersect<UnionToTuple<T[number][typeStatic]['schema']>>,
|
|
93
|
+
N,
|
|
94
|
+
O,
|
|
95
|
+
D
|
|
96
|
+
> {
|
|
53
97
|
constructor(
|
|
54
98
|
readonly types: T,
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
77
|
-
|
|
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
|
}
|