@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/number.ts
CHANGED
|
@@ -1,124 +1,160 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type IntegerOptions,
|
|
3
|
+
type NumberOptions,
|
|
4
|
+
type TInteger,
|
|
5
|
+
type TNumber,
|
|
6
|
+
Type,
|
|
7
|
+
} from '@sinclair/typebox'
|
|
2
8
|
import { BaseType } from './base.ts'
|
|
3
9
|
|
|
4
10
|
export class NumberType<
|
|
5
11
|
N extends boolean = false,
|
|
6
12
|
O extends boolean = false,
|
|
7
|
-
|
|
13
|
+
D extends boolean = false,
|
|
14
|
+
> extends BaseType<TNumber, N, O, D, NumberOptions> {
|
|
8
15
|
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
16
|
+
protected readonly options: NumberOptions = {},
|
|
17
|
+
isNullable: N = false as N,
|
|
18
|
+
isOptional: O = false as O,
|
|
19
|
+
hasDefault: D = false as D,
|
|
12
20
|
) {
|
|
13
|
-
super(
|
|
21
|
+
super(options, isNullable, isOptional, hasDefault)
|
|
14
22
|
}
|
|
15
23
|
|
|
16
|
-
|
|
24
|
+
protected _constructSchema(options: NumberOptions): TNumber {
|
|
25
|
+
return Type.Number(options)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
nullable() {
|
|
29
|
+
return new NumberType(...this._with({ isNullable: true }))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
optional() {
|
|
33
|
+
return new NumberType(...this._with({ isOptional: true }))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
nullish() {
|
|
37
|
+
return new NumberType(...this._with({ isNullable: true, isOptional: true }))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
default(value: number) {
|
|
17
41
|
return new NumberType(
|
|
18
|
-
|
|
19
|
-
...this._isNullableOptional,
|
|
42
|
+
...this._with({ options: { default: value }, hasDefault: true }),
|
|
20
43
|
)
|
|
21
44
|
}
|
|
22
45
|
|
|
46
|
+
description(description: string) {
|
|
47
|
+
return new NumberType(...this._with({ options: { description } }))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
examples(...examples: [number, ...number[]]) {
|
|
51
|
+
return new NumberType(...this._with({ options: { examples } }))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
positive() {
|
|
55
|
+
return this.min(0, true)
|
|
56
|
+
}
|
|
57
|
+
|
|
23
58
|
negative() {
|
|
24
|
-
return
|
|
25
|
-
Type.Number({ ...this._schema, maximum: 0, exclusiveMaximum: 0 }),
|
|
26
|
-
...this._isNullableOptional,
|
|
27
|
-
)
|
|
59
|
+
return this.max(0, true)
|
|
28
60
|
}
|
|
29
61
|
|
|
30
62
|
max(value: number, exclusive?: true) {
|
|
31
63
|
return new NumberType(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
64
|
+
...this._with({
|
|
65
|
+
options: {
|
|
66
|
+
maximum: value,
|
|
67
|
+
...(exclusive ? {} : { exclusiveMaximum: value }),
|
|
68
|
+
},
|
|
36
69
|
}),
|
|
37
|
-
...this._isNullableOptional,
|
|
38
70
|
)
|
|
39
71
|
}
|
|
40
72
|
|
|
41
73
|
min(value: number, exclusive?: true) {
|
|
42
74
|
return new NumberType(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
75
|
+
...this._with({
|
|
76
|
+
options: {
|
|
77
|
+
minimum: value,
|
|
78
|
+
...(exclusive ? {} : { exclusiveMinimum: value }),
|
|
79
|
+
},
|
|
47
80
|
}),
|
|
48
|
-
...this._isNullableOptional,
|
|
49
81
|
)
|
|
50
82
|
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export class IntegerType<
|
|
86
|
+
N extends boolean = false,
|
|
87
|
+
O extends boolean = false,
|
|
88
|
+
D extends boolean = false,
|
|
89
|
+
> extends BaseType<TInteger, N, O, D, IntegerOptions> {
|
|
90
|
+
constructor(
|
|
91
|
+
options: IntegerOptions = {},
|
|
92
|
+
isNullable: N = false as N,
|
|
93
|
+
isOptional: O = false as O,
|
|
94
|
+
hasDefault: D = false as D,
|
|
95
|
+
) {
|
|
96
|
+
super(options, isNullable, isOptional, hasDefault)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
protected _constructSchema(options: IntegerOptions): TInteger {
|
|
100
|
+
return Type.Integer(options)
|
|
101
|
+
}
|
|
51
102
|
|
|
52
103
|
nullable() {
|
|
53
|
-
return new
|
|
104
|
+
return new IntegerType(...this._with({ isNullable: true }))
|
|
54
105
|
}
|
|
55
106
|
|
|
56
107
|
optional() {
|
|
57
|
-
return new
|
|
108
|
+
return new IntegerType(...this._with({ isOptional: true }))
|
|
58
109
|
}
|
|
59
110
|
|
|
60
111
|
nullish() {
|
|
61
|
-
return new
|
|
112
|
+
return new IntegerType(
|
|
113
|
+
...this._with({ isNullable: true, isOptional: true }),
|
|
114
|
+
)
|
|
62
115
|
}
|
|
63
|
-
}
|
|
64
116
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
117
|
+
default(value: number) {
|
|
118
|
+
return new IntegerType(
|
|
119
|
+
...this._with({ options: { default: value }, hasDefault: true }),
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
description(description: string) {
|
|
124
|
+
return new IntegerType(...this._with({ options: { description } }))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
examples(...examples: [number, ...number[]]) {
|
|
128
|
+
return new IntegerType(...this._with({ options: { examples } }))
|
|
75
129
|
}
|
|
76
130
|
|
|
77
131
|
positive() {
|
|
78
|
-
return
|
|
79
|
-
Type.Integer({ ...this._schema, minimum: 0, exclusiveMinimum: 0 }),
|
|
80
|
-
...this._isNullableOptional,
|
|
81
|
-
)
|
|
132
|
+
return this.min(0, true)
|
|
82
133
|
}
|
|
83
134
|
|
|
84
135
|
negative() {
|
|
85
|
-
return
|
|
86
|
-
Type.Integer({ ...this._schema, maximum: 0, exclusiveMaximum: 0 }),
|
|
87
|
-
...this._isNullableOptional,
|
|
88
|
-
)
|
|
136
|
+
return this.max(0, true)
|
|
89
137
|
}
|
|
90
138
|
|
|
91
139
|
max(value: number, exclusive?: true) {
|
|
92
140
|
return new IntegerType(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
141
|
+
...this._with({
|
|
142
|
+
options: {
|
|
143
|
+
maximum: value,
|
|
144
|
+
...(exclusive ? {} : { exclusiveMaximum: value }),
|
|
145
|
+
},
|
|
97
146
|
}),
|
|
98
|
-
...this._isNullableOptional,
|
|
99
147
|
)
|
|
100
148
|
}
|
|
101
149
|
|
|
102
150
|
min(value: number, exclusive?: true) {
|
|
103
151
|
return new IntegerType(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
152
|
+
...this._with({
|
|
153
|
+
options: {
|
|
154
|
+
minimum: value,
|
|
155
|
+
...(exclusive ? {} : { exclusiveMinimum: value }),
|
|
156
|
+
},
|
|
108
157
|
}),
|
|
109
|
-
...this._isNullableOptional,
|
|
110
158
|
)
|
|
111
159
|
}
|
|
112
|
-
|
|
113
|
-
nullable() {
|
|
114
|
-
return new IntegerType(...this._nullable())
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
optional() {
|
|
118
|
-
return new IntegerType(...this._optional())
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
nullish() {
|
|
122
|
-
return new IntegerType(...this._nullish())
|
|
123
|
-
}
|
|
124
160
|
}
|
package/src/types/object.ts
CHANGED
|
@@ -1,55 +1,97 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type ObjectOptions,
|
|
3
|
+
type StaticEncode,
|
|
4
|
+
type TObject,
|
|
5
|
+
Type,
|
|
6
|
+
} from '@sinclair/typebox'
|
|
7
|
+
import type { typeStatic } from '../constants.ts'
|
|
2
8
|
import type { UnionToTupleString } from '../utils.ts'
|
|
3
|
-
import { BaseType,
|
|
9
|
+
import { BaseType, getTypeSchema } from './base.ts'
|
|
4
10
|
import { EnumType } from './enum.ts'
|
|
5
11
|
|
|
6
12
|
export class ObjectType<
|
|
7
13
|
T extends Record<string, BaseType> = Record<string, BaseType>,
|
|
8
14
|
N extends boolean = false,
|
|
9
15
|
O extends boolean = false,
|
|
10
|
-
|
|
16
|
+
D extends boolean = false,
|
|
17
|
+
> extends BaseType<
|
|
18
|
+
TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,
|
|
19
|
+
N,
|
|
20
|
+
O,
|
|
21
|
+
D
|
|
22
|
+
> {
|
|
11
23
|
constructor(
|
|
12
|
-
readonly properties: T = {} as T,
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
protected readonly properties: T = {} as T,
|
|
25
|
+
options: ObjectOptions = {},
|
|
26
|
+
isNullable: N = false as N,
|
|
27
|
+
isOptional: O = false as O,
|
|
28
|
+
hasDefault: D = false as D,
|
|
15
29
|
) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
super(options, isNullable, isOptional, hasDefault, properties)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected _constructSchema(
|
|
34
|
+
options: ObjectOptions,
|
|
35
|
+
properties: T,
|
|
36
|
+
): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {
|
|
37
|
+
const schemaProperties = {} as {
|
|
38
|
+
[K in keyof T]: T[K][typeStatic]['schema']
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
42
|
+
// @ts-expect-error
|
|
43
|
+
schemaProperties[key] = getTypeSchema(value)
|
|
44
|
+
}
|
|
45
|
+
return Type.Object(schemaProperties, options)
|
|
29
46
|
}
|
|
30
47
|
|
|
31
48
|
nullable() {
|
|
32
|
-
|
|
33
|
-
return new ObjectType(this.properties, ...args)
|
|
49
|
+
return new ObjectType(this.properties, ...this._with({ isNullable: true }))
|
|
34
50
|
}
|
|
35
51
|
|
|
36
52
|
optional() {
|
|
37
|
-
|
|
38
|
-
return new ObjectType(this.properties, ...args)
|
|
53
|
+
return new ObjectType(this.properties, ...this._with({ isOptional: true }))
|
|
39
54
|
}
|
|
40
55
|
|
|
41
56
|
nullish() {
|
|
42
|
-
|
|
43
|
-
|
|
57
|
+
return new ObjectType(
|
|
58
|
+
this.properties,
|
|
59
|
+
...this._with({ isNullable: true, isOptional: true }),
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
default(value: this[typeStatic]['encoded']) {
|
|
64
|
+
return new ObjectType(
|
|
65
|
+
this.properties,
|
|
66
|
+
...this._with({ options: { default: value }, hasDefault: true }),
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
description(description: string) {
|
|
71
|
+
return new ObjectType(
|
|
72
|
+
this.properties,
|
|
73
|
+
...this._with({ options: { description } }),
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
examples(
|
|
78
|
+
...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
|
|
79
|
+
) {
|
|
80
|
+
return new ObjectType(
|
|
81
|
+
this.properties,
|
|
82
|
+
...this._with({ options: { examples } }),
|
|
83
|
+
)
|
|
44
84
|
}
|
|
45
85
|
|
|
46
86
|
pick<P extends { [K in keyof T]?: true }>(pick: P) {
|
|
47
87
|
const properties = Object.fromEntries(
|
|
48
88
|
Object.entries(this.properties).filter(([key]) => pick[key]),
|
|
49
89
|
)
|
|
90
|
+
const [_, ...args] = this._with()
|
|
50
91
|
return new ObjectType(
|
|
51
92
|
properties as Pick<T, Extract<keyof P, keyof T>>,
|
|
52
|
-
|
|
93
|
+
{},
|
|
94
|
+
...args,
|
|
53
95
|
)
|
|
54
96
|
}
|
|
55
97
|
|
|
@@ -57,26 +99,39 @@ export class ObjectType<
|
|
|
57
99
|
const properties = Object.fromEntries(
|
|
58
100
|
Object.entries(this.properties).filter(([key]) => !omit[key]),
|
|
59
101
|
)
|
|
102
|
+
const [_, ...args] = this._with()
|
|
60
103
|
return new ObjectType(
|
|
61
104
|
properties as Omit<T, Extract<keyof P, keyof T>>,
|
|
62
|
-
|
|
105
|
+
{},
|
|
106
|
+
...args,
|
|
63
107
|
)
|
|
64
108
|
}
|
|
65
109
|
|
|
66
110
|
extend<P extends Record<string, BaseType>>(properties: P) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
...this._isNullableOptional,
|
|
70
|
-
)
|
|
111
|
+
const [_, ...args] = this._with()
|
|
112
|
+
return new ObjectType({ ...this.properties, ...properties }, {}, ...args)
|
|
71
113
|
}
|
|
72
114
|
|
|
73
115
|
merge<T extends ObjectType>(object: T) {
|
|
116
|
+
const [_, ...args] = this._with()
|
|
74
117
|
return new ObjectType(
|
|
75
118
|
{ ...this.properties, ...object.properties },
|
|
76
|
-
|
|
119
|
+
{},
|
|
120
|
+
...args,
|
|
77
121
|
)
|
|
78
122
|
}
|
|
79
123
|
|
|
124
|
+
partial() {
|
|
125
|
+
const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =
|
|
126
|
+
{} as any
|
|
127
|
+
for (const [key, value] of Object.entries(this.properties)) {
|
|
128
|
+
// @ts-expect-error
|
|
129
|
+
properties[key] = value.optional()
|
|
130
|
+
}
|
|
131
|
+
const [_, ...args] = this._with()
|
|
132
|
+
return new ObjectType(properties, {}, ...args)
|
|
133
|
+
}
|
|
134
|
+
|
|
80
135
|
keyof(): EnumType<UnionToTupleString<keyof T>> {
|
|
81
136
|
return new EnumType(Object.keys(this.properties) as any)
|
|
82
137
|
}
|
package/src/types/string.ts
CHANGED
|
@@ -1,67 +1,75 @@
|
|
|
1
|
-
import { type TString, Type } from '@sinclair/typebox'
|
|
1
|
+
import { type StringOptions, type TString, Type } from '@sinclair/typebox'
|
|
2
2
|
import { BaseType } from './base.ts'
|
|
3
3
|
|
|
4
4
|
export class StringType<
|
|
5
5
|
N extends boolean = false,
|
|
6
6
|
O extends boolean = false,
|
|
7
|
-
|
|
7
|
+
D extends boolean = false,
|
|
8
|
+
> extends BaseType<TString, N, O, D, StringOptions> {
|
|
8
9
|
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
options: StringOptions = {},
|
|
11
|
+
isNullable: N = false as N,
|
|
12
|
+
isOptional: O = false as O,
|
|
13
|
+
hasDefault: D = false as D,
|
|
12
14
|
) {
|
|
13
|
-
super(
|
|
15
|
+
super(options, isNullable, isOptional, hasDefault)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected _constructSchema(options: StringOptions): TString {
|
|
19
|
+
return Type.String(options)
|
|
14
20
|
}
|
|
15
21
|
|
|
16
22
|
nullable() {
|
|
17
|
-
return new StringType(...this.
|
|
23
|
+
return new StringType(...this._with({ isNullable: true }))
|
|
18
24
|
}
|
|
19
25
|
|
|
20
26
|
optional() {
|
|
21
|
-
return new StringType(...this.
|
|
27
|
+
return new StringType(...this._with({ isOptional: true }))
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
nullish() {
|
|
25
|
-
return new StringType(...this.
|
|
31
|
+
return new StringType(...this._with({ isNullable: true, isOptional: true }))
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
default(value: string) {
|
|
29
35
|
return new StringType(
|
|
30
|
-
{
|
|
31
|
-
...this._schema,
|
|
32
|
-
format,
|
|
33
|
-
},
|
|
34
|
-
...this._isNullableOptional,
|
|
36
|
+
...this._with({ options: { default: value }, hasDefault: true }),
|
|
35
37
|
)
|
|
36
38
|
}
|
|
37
39
|
|
|
40
|
+
description(description: string) {
|
|
41
|
+
return new StringType(...this._with({ options: { description } }))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
examples(...examples: string[]) {
|
|
45
|
+
return new StringType(...this._with({ options: { examples } }))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
format(format: TString['format']) {
|
|
49
|
+
return new StringType(...this._with({ options: { format } }))
|
|
50
|
+
}
|
|
51
|
+
|
|
38
52
|
max(value: number) {
|
|
39
53
|
return new StringType(
|
|
40
|
-
{
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
...this._isNullableOptional,
|
|
54
|
+
...this._with({
|
|
55
|
+
options: { maxLength: value },
|
|
56
|
+
}),
|
|
45
57
|
)
|
|
46
58
|
}
|
|
47
59
|
|
|
48
60
|
min(value: number) {
|
|
49
61
|
return new StringType(
|
|
50
|
-
{
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
54
|
-
...this._isNullableOptional,
|
|
62
|
+
...this._with({
|
|
63
|
+
options: { minLength: value },
|
|
64
|
+
}),
|
|
55
65
|
)
|
|
56
66
|
}
|
|
57
67
|
|
|
58
68
|
pattern(pattern: string) {
|
|
59
69
|
return new StringType(
|
|
60
|
-
{
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
},
|
|
64
|
-
...this._isNullableOptional,
|
|
70
|
+
...this._with({
|
|
71
|
+
options: { pattern },
|
|
72
|
+
}),
|
|
65
73
|
)
|
|
66
74
|
}
|
|
67
75
|
|