@nmtjs/type 0.1.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/LICENSE.md +7 -0
- package/README.md +9 -0
- package/dist/compiler.js +57 -0
- package/dist/compiler.js.map +1 -0
- package/dist/formats.js +127 -0
- package/dist/formats.js.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/schemas/native-enum.js +14 -0
- package/dist/schemas/native-enum.js.map +1 -0
- package/dist/schemas/nullable.js +5 -0
- package/dist/schemas/nullable.js.map +1 -0
- package/dist/schemas/union-enum.js +13 -0
- package/dist/schemas/union-enum.js.map +1 -0
- package/dist/temporal.js +15 -0
- package/dist/temporal.js.map +1 -0
- package/dist/types/any.js +16 -0
- package/dist/types/any.js.map +1 -0
- package/dist/types/array.js +42 -0
- package/dist/types/array.js.map +1 -0
- package/dist/types/base.js +77 -0
- package/dist/types/base.js.map +1 -0
- package/dist/types/boolean.js +16 -0
- package/dist/types/boolean.js.map +1 -0
- package/dist/types/custom.js +23 -0
- package/dist/types/custom.js.map +1 -0
- package/dist/types/datetime.js +34 -0
- package/dist/types/datetime.js.map +1 -0
- package/dist/types/enum.js +41 -0
- package/dist/types/enum.js.map +1 -0
- package/dist/types/literal.js +21 -0
- package/dist/types/literal.js.map +1 -0
- package/dist/types/never.js +16 -0
- package/dist/types/never.js.map +1 -0
- package/dist/types/number.js +94 -0
- package/dist/types/number.js.map +1 -0
- package/dist/types/object.js +49 -0
- package/dist/types/object.js.map +1 -0
- package/dist/types/string.js +55 -0
- package/dist/types/string.js.map +1 -0
- package/dist/types/temporal.js +144 -0
- package/dist/types/temporal.js.map +1 -0
- package/dist/types/union.js +40 -0
- package/dist/types/union.js.map +1 -0
- package/dist/utils.js +1 -0
- package/dist/utils.js.map +1 -0
- package/package.json +43 -0
- package/src/compiler.ts +68 -0
- package/src/formats.ts +182 -0
- package/src/index.ts +79 -0
- package/src/schemas/native-enum.ts +29 -0
- package/src/schemas/nullable.ts +13 -0
- package/src/schemas/union-enum.ts +43 -0
- package/src/temporal.ts +34 -0
- package/src/types/any.ts +27 -0
- package/src/types/array.ts +66 -0
- package/src/types/base.ts +158 -0
- package/src/types/boolean.ts +27 -0
- package/src/types/custom.ts +36 -0
- package/src/types/datetime.ts +60 -0
- package/src/types/enum.ts +62 -0
- package/src/types/literal.ts +31 -0
- package/src/types/never.ts +30 -0
- package/src/types/number.ts +124 -0
- package/src/types/object.ts +83 -0
- package/src/types/string.ts +87 -0
- package/src/types/temporal.ts +227 -0
- package/src/types/union.ts +79 -0
- package/src/utils.ts +16 -0
- package/tsconfig.json +3 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type StaticDecode,
|
|
3
|
+
type StaticEncode,
|
|
4
|
+
type TAny,
|
|
5
|
+
type TOptional,
|
|
6
|
+
type TSchema,
|
|
7
|
+
Type,
|
|
8
|
+
} from '@sinclair/typebox'
|
|
9
|
+
import { Nullable, type TNullable } from '../schemas/nullable.ts'
|
|
10
|
+
|
|
11
|
+
export const typeSchema: unique symbol = Symbol()
|
|
12
|
+
export type typeSchema = typeof typeSchema
|
|
13
|
+
|
|
14
|
+
export const typeOptions: unique symbol = Symbol()
|
|
15
|
+
export type typeOptions = typeof typeOptions
|
|
16
|
+
|
|
17
|
+
export const typeOptional: unique symbol = Symbol()
|
|
18
|
+
export type typeOptional = typeof typeOptional
|
|
19
|
+
|
|
20
|
+
export const typeNullable: unique symbol = Symbol()
|
|
21
|
+
export type typeNullable = typeof typeNullable
|
|
22
|
+
|
|
23
|
+
export const staticType: unique symbol = Symbol()
|
|
24
|
+
export type staticType = typeof staticType
|
|
25
|
+
|
|
26
|
+
export const typeFinalSchema: unique symbol = Symbol()
|
|
27
|
+
export type typeFinalSchema = typeof typeFinalSchema
|
|
28
|
+
|
|
29
|
+
type ResolveNullable<T extends TSchema, Is extends boolean> = Is extends true
|
|
30
|
+
? T | TNullable<T>
|
|
31
|
+
: T
|
|
32
|
+
|
|
33
|
+
type ResolveOptional<T extends TSchema, Is extends boolean> = Is extends true
|
|
34
|
+
? T | TOptional<T>
|
|
35
|
+
: T
|
|
36
|
+
|
|
37
|
+
type Resolve<
|
|
38
|
+
Schema extends TSchema,
|
|
39
|
+
IsNullable extends boolean,
|
|
40
|
+
IsOptional extends boolean,
|
|
41
|
+
> = ResolveOptional<ResolveNullable<Schema, IsNullable>, IsOptional>
|
|
42
|
+
|
|
43
|
+
export abstract class BaseType<
|
|
44
|
+
Schema extends TSchema = any,
|
|
45
|
+
IsNullable extends boolean = boolean,
|
|
46
|
+
IsOptional extends boolean = boolean,
|
|
47
|
+
Final extends Resolve<Schema, IsNullable, IsOptional> = Resolve<
|
|
48
|
+
Schema,
|
|
49
|
+
IsNullable,
|
|
50
|
+
IsOptional
|
|
51
|
+
>,
|
|
52
|
+
> {
|
|
53
|
+
[typeSchema]: Schema;
|
|
54
|
+
[typeNullable]: IsNullable;
|
|
55
|
+
[typeOptional]: IsOptional;
|
|
56
|
+
|
|
57
|
+
[staticType]!: {
|
|
58
|
+
final: Final
|
|
59
|
+
isOptional: IsOptional
|
|
60
|
+
isNullable: IsNullable
|
|
61
|
+
encoded: StaticEncode<Final>
|
|
62
|
+
decoded: StaticDecode<Final>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
constructor(
|
|
66
|
+
schema: Schema,
|
|
67
|
+
nullable: IsNullable = false as IsNullable,
|
|
68
|
+
optional: IsOptional = false as IsOptional,
|
|
69
|
+
) {
|
|
70
|
+
this[typeSchema] = schema
|
|
71
|
+
this[typeNullable] = nullable
|
|
72
|
+
this[typeOptional] = optional
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get [typeFinalSchema](): Final {
|
|
76
|
+
let schema: TSchema = this._schema
|
|
77
|
+
if (this._isNullable) {
|
|
78
|
+
schema = Nullable(schema)
|
|
79
|
+
}
|
|
80
|
+
if (this._isOptional) {
|
|
81
|
+
schema = Type.Optional(schema)
|
|
82
|
+
}
|
|
83
|
+
return schema as Final
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected get _schema() {
|
|
87
|
+
return this[typeSchema]
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
protected get _isNullable(): IsNullable {
|
|
91
|
+
return this[typeNullable]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
protected get _isOptional(): IsOptional {
|
|
95
|
+
return this[typeOptional]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
protected get _isNullableOptional(): [IsNullable, IsOptional] {
|
|
99
|
+
return [this._isNullable, this._isOptional]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected _contructSelf<T extends any[]>(...args: T) {
|
|
103
|
+
return args
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
protected _nullable() {
|
|
107
|
+
return this._contructSelf(this._schema, true as const, this[typeOptional])
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
protected _optional() {
|
|
111
|
+
return this._contructSelf(this._schema, this[typeNullable], true as const)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
protected _nullish() {
|
|
115
|
+
return this._contructSelf(this._schema, true as const, true as const)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
abstract nullable(): BaseType<Schema, true, IsOptional>
|
|
119
|
+
abstract optional(): BaseType<Schema, IsNullable, true>
|
|
120
|
+
abstract nullish(): BaseType<Schema, true, true>
|
|
121
|
+
|
|
122
|
+
default(value: StaticDecode<Schema>): this {
|
|
123
|
+
return this._contructSelf(
|
|
124
|
+
{
|
|
125
|
+
...this._schema,
|
|
126
|
+
default: value,
|
|
127
|
+
},
|
|
128
|
+
this[typeNullable],
|
|
129
|
+
this[typeOptional],
|
|
130
|
+
) as unknown as this
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
description(description: string): this {
|
|
134
|
+
return this._contructSelf(
|
|
135
|
+
{
|
|
136
|
+
...this._schema,
|
|
137
|
+
description,
|
|
138
|
+
},
|
|
139
|
+
this[typeNullable],
|
|
140
|
+
this[typeOptional],
|
|
141
|
+
) as unknown as this
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
examples(...examples: StaticDecode<Schema>[]): this {
|
|
145
|
+
return this._contructSelf(
|
|
146
|
+
{
|
|
147
|
+
...this._schema,
|
|
148
|
+
examples,
|
|
149
|
+
},
|
|
150
|
+
this[typeNullable],
|
|
151
|
+
this[typeOptional],
|
|
152
|
+
) as unknown as this
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function getTypeSchema<T extends BaseType>(type: T): T[typeFinalSchema] {
|
|
157
|
+
return type[typeFinalSchema]
|
|
158
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type TBoolean, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
|
+
|
|
4
|
+
export class BooleanType<
|
|
5
|
+
N extends boolean = false,
|
|
6
|
+
O extends boolean = false,
|
|
7
|
+
> extends BaseType<TBoolean, N, O> {
|
|
8
|
+
constructor(
|
|
9
|
+
schema: TBoolean = Type.Boolean(),
|
|
10
|
+
nullable: N = false as N,
|
|
11
|
+
optional: O = false as O,
|
|
12
|
+
) {
|
|
13
|
+
super(schema, nullable, optional)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
nullable() {
|
|
17
|
+
return new BooleanType(...this._nullable())
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
optional() {
|
|
21
|
+
return new BooleanType(...this._optional())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
nullish() {
|
|
25
|
+
return new BooleanType(...this._nullish())
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type TAny, type TTransform, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
|
+
|
|
4
|
+
export class CustomType<
|
|
5
|
+
T,
|
|
6
|
+
N extends boolean = false,
|
|
7
|
+
O extends boolean = false,
|
|
8
|
+
> extends BaseType<TTransform<TAny, T>, N, O> {
|
|
9
|
+
constructor(
|
|
10
|
+
protected readonly decode: (value: any) => T,
|
|
11
|
+
protected readonly encode: (value: T) => any,
|
|
12
|
+
nullable: N = false as N,
|
|
13
|
+
optional: O = false as O,
|
|
14
|
+
) {
|
|
15
|
+
super(
|
|
16
|
+
Type.Optional(Type.Transform(Type.Any()).Decode(decode).Encode(encode)),
|
|
17
|
+
nullable,
|
|
18
|
+
optional,
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
nullable() {
|
|
23
|
+
const [_, ...args] = this._nullable()
|
|
24
|
+
return new CustomType(this.decode, this.encode, ...args)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
optional() {
|
|
28
|
+
const [_, ...args] = this._optional()
|
|
29
|
+
return new CustomType(this.decode, this.encode, ...args)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
nullish() {
|
|
33
|
+
const [_, ...args] = this._nullish()
|
|
34
|
+
return new CustomType(this.decode, this.encode, ...args)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type TString, type TTransform, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
|
+
|
|
4
|
+
export class DateType<
|
|
5
|
+
N extends boolean = false,
|
|
6
|
+
O extends boolean = false,
|
|
7
|
+
> extends BaseType<TTransform<TString, Date>, N, O> {
|
|
8
|
+
constructor(
|
|
9
|
+
schema: TTransform<TString, Date> = Type.Transform(
|
|
10
|
+
Type.String({ format: 'date' }),
|
|
11
|
+
)
|
|
12
|
+
.Decode((value) => new Date(value))
|
|
13
|
+
.Encode((value) => value.toJSON()),
|
|
14
|
+
nullable: N = false as N,
|
|
15
|
+
optional: O = false as O,
|
|
16
|
+
) {
|
|
17
|
+
super(schema, nullable, optional)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
nullable() {
|
|
21
|
+
return new DateType(...this._nullable())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
optional() {
|
|
25
|
+
return new DateType(...this._optional())
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
nullish() {
|
|
29
|
+
return new DateType(...this._nullish())
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class DateTimeType<
|
|
34
|
+
N extends boolean = false,
|
|
35
|
+
O extends boolean = false,
|
|
36
|
+
> extends BaseType<TTransform<TString, Date>, N, O> {
|
|
37
|
+
constructor(
|
|
38
|
+
schema: TTransform<TString, Date> = Type.Transform(
|
|
39
|
+
Type.String({ format: 'date-time' }),
|
|
40
|
+
)
|
|
41
|
+
.Decode((value) => new Date(value))
|
|
42
|
+
.Encode((value) => value.toJSON()),
|
|
43
|
+
nullable: N = false as N,
|
|
44
|
+
optional: O = false as O,
|
|
45
|
+
) {
|
|
46
|
+
super(schema, nullable, optional)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
nullable() {
|
|
50
|
+
return new DateTimeType(...this._nullable())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
optional() {
|
|
54
|
+
return new DateTimeType(...this._optional())
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
nullish() {
|
|
58
|
+
return new DateTimeType(...this._nullish())
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { TNativeEnum } from '../schemas/native-enum.ts'
|
|
2
|
+
import { NativeEnum } from '../schemas/native-enum.ts'
|
|
3
|
+
import { type TUnionEnum, UnionEnum } from '../schemas/union-enum.ts'
|
|
4
|
+
import { BaseType } from './base.ts'
|
|
5
|
+
|
|
6
|
+
export class NativeEnumType<
|
|
7
|
+
T extends { [K in string]: K } = { [K in string]: K },
|
|
8
|
+
N extends boolean = false,
|
|
9
|
+
O extends boolean = false,
|
|
10
|
+
> extends BaseType<TNativeEnum<T>, N, O> {
|
|
11
|
+
constructor(
|
|
12
|
+
readonly values: T,
|
|
13
|
+
nullable: N = false as N,
|
|
14
|
+
optional: O = false as O,
|
|
15
|
+
) {
|
|
16
|
+
super(NativeEnum(values), nullable, optional)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
nullable() {
|
|
20
|
+
const [_, ...args] = this._nullable()
|
|
21
|
+
return new NativeEnumType(this.values, ...args)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
optional() {
|
|
25
|
+
const [_, ...args] = this._optional()
|
|
26
|
+
return new NativeEnumType(this.values, ...args)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
nullish() {
|
|
30
|
+
const [_, ...args] = this._nullish()
|
|
31
|
+
return new NativeEnumType(this.values, ...args)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class EnumType<
|
|
36
|
+
T extends (string | number)[],
|
|
37
|
+
N extends boolean = false,
|
|
38
|
+
O extends boolean = false,
|
|
39
|
+
> extends BaseType<TUnionEnum<T>, N, O> {
|
|
40
|
+
constructor(
|
|
41
|
+
readonly values: [...T],
|
|
42
|
+
nullable: N = false as N,
|
|
43
|
+
optional: O = false as O,
|
|
44
|
+
) {
|
|
45
|
+
super(UnionEnum(values), nullable, optional)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
nullable() {
|
|
49
|
+
const [_, ...args] = this._nullable()
|
|
50
|
+
return new EnumType(this.values, ...args)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
optional() {
|
|
54
|
+
const [_, ...args] = this._optional()
|
|
55
|
+
return new EnumType(this.values, ...args)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
nullish() {
|
|
59
|
+
const [_, ...args] = this._nullish()
|
|
60
|
+
return new EnumType(this.values, ...args)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type TLiteral, type TLiteralValue, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
|
+
|
|
4
|
+
export class LiteralType<
|
|
5
|
+
T extends TLiteralValue = TLiteralValue,
|
|
6
|
+
N extends boolean = false,
|
|
7
|
+
O extends boolean = false,
|
|
8
|
+
> extends BaseType<TLiteral<T>, N, O> {
|
|
9
|
+
constructor(
|
|
10
|
+
readonly value: T,
|
|
11
|
+
nullable: N = false as N,
|
|
12
|
+
optional: O = false as O,
|
|
13
|
+
) {
|
|
14
|
+
super(Type.Literal(value), nullable, optional)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
nullable() {
|
|
18
|
+
const [_, ...args] = this._nullable()
|
|
19
|
+
return new LiteralType(this.value, ...args)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
optional() {
|
|
23
|
+
const [_, ...args] = this._optional()
|
|
24
|
+
return new LiteralType(this.value, ...args)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
nullish() {
|
|
28
|
+
const [_, ...args] = this._nullish()
|
|
29
|
+
return new LiteralType(this.value, ...args)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type TNever, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
|
+
|
|
4
|
+
export class NeverType<
|
|
5
|
+
N extends boolean = false,
|
|
6
|
+
O extends boolean = false,
|
|
7
|
+
> extends BaseType<TNever, N, O> {
|
|
8
|
+
constructor(
|
|
9
|
+
schema = Type.Never(),
|
|
10
|
+
nullable: N = false as N,
|
|
11
|
+
optional: O = false as O,
|
|
12
|
+
) {
|
|
13
|
+
super(schema, nullable, optional)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// @ts-expect-error
|
|
17
|
+
nullable() {
|
|
18
|
+
throw new Error('NeverType cannot be nullable')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// @ts-expect-error
|
|
22
|
+
optional() {
|
|
23
|
+
throw new Error('NeverType cannot be optional')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// @ts-expect-error
|
|
27
|
+
nullish() {
|
|
28
|
+
throw new Error('NeverType cannot be nullish')
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { type TInteger, type TNumber, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
|
+
|
|
4
|
+
export class NumberType<
|
|
5
|
+
N extends boolean = false,
|
|
6
|
+
O extends boolean = false,
|
|
7
|
+
> extends BaseType<TNumber, N, O> {
|
|
8
|
+
constructor(
|
|
9
|
+
schema: TNumber = Type.Number(),
|
|
10
|
+
nullable: N = false as N,
|
|
11
|
+
optional: O = false as O,
|
|
12
|
+
) {
|
|
13
|
+
super(schema, nullable, optional)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
positive() {
|
|
17
|
+
return new NumberType(
|
|
18
|
+
Type.Number({ ...this._schema, minimum: 0, exclusiveMinimum: 0 }),
|
|
19
|
+
...this._isNullableOptional,
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
negative() {
|
|
24
|
+
return new NumberType(
|
|
25
|
+
Type.Number({ ...this._schema, maximum: 0, exclusiveMaximum: 0 }),
|
|
26
|
+
...this._isNullableOptional,
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
max(value: number, exclusive?: true) {
|
|
31
|
+
return new NumberType(
|
|
32
|
+
Type.Number({
|
|
33
|
+
...this._schema,
|
|
34
|
+
maximum: value,
|
|
35
|
+
...(exclusive ? {} : { exclusiveMaximum: value }),
|
|
36
|
+
}),
|
|
37
|
+
...this._isNullableOptional,
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
min(value: number, exclusive?: true) {
|
|
42
|
+
return new NumberType(
|
|
43
|
+
Type.Number({
|
|
44
|
+
...this._schema,
|
|
45
|
+
minimum: value,
|
|
46
|
+
...(exclusive ? {} : { exclusiveMinimum: value }),
|
|
47
|
+
}),
|
|
48
|
+
...this._isNullableOptional,
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
nullable() {
|
|
53
|
+
return new NumberType(...this._nullable())
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
optional() {
|
|
57
|
+
return new NumberType(...this._optional())
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
nullish() {
|
|
61
|
+
return new NumberType(...this._nullish())
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class IntegerType<
|
|
66
|
+
N extends boolean = false,
|
|
67
|
+
O extends boolean = false,
|
|
68
|
+
> extends BaseType<TInteger, N, O> {
|
|
69
|
+
constructor(
|
|
70
|
+
schema: TInteger = Type.Integer(),
|
|
71
|
+
nullable: N = false as N,
|
|
72
|
+
optional: O = false as O,
|
|
73
|
+
) {
|
|
74
|
+
super(schema, nullable, optional)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
positive() {
|
|
78
|
+
return new IntegerType(
|
|
79
|
+
Type.Integer({ ...this._schema, minimum: 0, exclusiveMinimum: 0 }),
|
|
80
|
+
...this._isNullableOptional,
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
negative() {
|
|
85
|
+
return new IntegerType(
|
|
86
|
+
Type.Integer({ ...this._schema, maximum: 0, exclusiveMaximum: 0 }),
|
|
87
|
+
...this._isNullableOptional,
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
max(value: number, exclusive?: true) {
|
|
92
|
+
return new IntegerType(
|
|
93
|
+
Type.Integer({
|
|
94
|
+
...this._schema,
|
|
95
|
+
maximum: value,
|
|
96
|
+
...(exclusive ? {} : { exclusiveMaximum: value }),
|
|
97
|
+
}),
|
|
98
|
+
...this._isNullableOptional,
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
min(value: number, exclusive?: true) {
|
|
103
|
+
return new IntegerType(
|
|
104
|
+
Type.Integer({
|
|
105
|
+
...this._schema,
|
|
106
|
+
minimum: value,
|
|
107
|
+
...(exclusive ? {} : { exclusiveMinimum: value }),
|
|
108
|
+
}),
|
|
109
|
+
...this._isNullableOptional,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
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
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type TObject, Type } from '@sinclair/typebox'
|
|
2
|
+
import type { UnionToTupleString } from '../utils.ts'
|
|
3
|
+
import { BaseType, typeFinalSchema } from './base.ts'
|
|
4
|
+
import { EnumType } from './enum.ts'
|
|
5
|
+
|
|
6
|
+
export class ObjectType<
|
|
7
|
+
T extends Record<string, BaseType> = Record<string, BaseType>,
|
|
8
|
+
N extends boolean = false,
|
|
9
|
+
O extends boolean = false,
|
|
10
|
+
> extends BaseType<TObject<{ [K in keyof T]: T[K][typeFinalSchema] }>, N, O> {
|
|
11
|
+
constructor(
|
|
12
|
+
readonly properties: T = {} as T,
|
|
13
|
+
nullable: N = false as N,
|
|
14
|
+
optional: O = false as O,
|
|
15
|
+
) {
|
|
16
|
+
const schemaProperties = Object.fromEntries(
|
|
17
|
+
Object.entries(properties).map(([key, value]) => [
|
|
18
|
+
key,
|
|
19
|
+
value[typeFinalSchema],
|
|
20
|
+
]),
|
|
21
|
+
)
|
|
22
|
+
super(
|
|
23
|
+
Type.Object(
|
|
24
|
+
schemaProperties as { [K in keyof T]: T[K][typeFinalSchema] },
|
|
25
|
+
),
|
|
26
|
+
nullable,
|
|
27
|
+
optional,
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
nullable() {
|
|
32
|
+
const [_, ...args] = this._nullable()
|
|
33
|
+
return new ObjectType(this.properties, ...args)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
optional() {
|
|
37
|
+
const [_, ...args] = this._optional()
|
|
38
|
+
return new ObjectType(this.properties, ...args)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
nullish() {
|
|
42
|
+
const [_, ...args] = this._nullish()
|
|
43
|
+
return new ObjectType(this.properties, ...args)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
pick<P extends { [K in keyof T]?: true }>(pick: P) {
|
|
47
|
+
const properties = Object.fromEntries(
|
|
48
|
+
Object.entries(this.properties).filter(([key]) => pick[key]),
|
|
49
|
+
)
|
|
50
|
+
return new ObjectType(
|
|
51
|
+
properties as Pick<T, Extract<keyof P, keyof T>>,
|
|
52
|
+
...this._isNullableOptional,
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
omit<P extends { [K in keyof T]?: true }>(omit: P) {
|
|
57
|
+
const properties = Object.fromEntries(
|
|
58
|
+
Object.entries(this.properties).filter(([key]) => !omit[key]),
|
|
59
|
+
)
|
|
60
|
+
return new ObjectType(
|
|
61
|
+
properties as Omit<T, Extract<keyof P, keyof T>>,
|
|
62
|
+
...this._isNullableOptional,
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
extend<P extends Record<string, BaseType>>(properties: P) {
|
|
67
|
+
return new ObjectType(
|
|
68
|
+
{ ...this.properties, ...properties },
|
|
69
|
+
...this._isNullableOptional,
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
merge<T extends ObjectType>(object: T) {
|
|
74
|
+
return new ObjectType(
|
|
75
|
+
{ ...this.properties, ...object.properties },
|
|
76
|
+
...this._isNullableOptional,
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
keyof(): EnumType<UnionToTupleString<keyof T>> {
|
|
81
|
+
return new EnumType(Object.keys(this.properties) as any)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type TString, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
|
+
|
|
4
|
+
export class StringType<
|
|
5
|
+
N extends boolean = false,
|
|
6
|
+
O extends boolean = false,
|
|
7
|
+
> extends BaseType<TString, N, O> {
|
|
8
|
+
constructor(
|
|
9
|
+
schema = Type.String(),
|
|
10
|
+
nullable: N = false as N,
|
|
11
|
+
optional: O = false as O,
|
|
12
|
+
) {
|
|
13
|
+
super(schema, nullable, optional)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
nullable() {
|
|
17
|
+
return new StringType(...this._nullable())
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
optional() {
|
|
21
|
+
return new StringType(...this._optional())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
nullish() {
|
|
25
|
+
return new StringType(...this._nullish())
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
format(format: TString['format']) {
|
|
29
|
+
return new StringType(
|
|
30
|
+
{
|
|
31
|
+
...this._schema,
|
|
32
|
+
format,
|
|
33
|
+
},
|
|
34
|
+
...this._isNullableOptional,
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
max(value: number) {
|
|
39
|
+
return new StringType(
|
|
40
|
+
{
|
|
41
|
+
...this._schema,
|
|
42
|
+
maxLength: value,
|
|
43
|
+
},
|
|
44
|
+
...this._isNullableOptional,
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
min(value: number) {
|
|
49
|
+
return new StringType(
|
|
50
|
+
{
|
|
51
|
+
...this._schema,
|
|
52
|
+
minLength: value,
|
|
53
|
+
},
|
|
54
|
+
...this._isNullableOptional,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
pattern(pattern: string) {
|
|
59
|
+
return new StringType(
|
|
60
|
+
{
|
|
61
|
+
...this._schema,
|
|
62
|
+
pattern,
|
|
63
|
+
},
|
|
64
|
+
...this._isNullableOptional,
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
email() {
|
|
69
|
+
return this.format('email')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
url() {
|
|
73
|
+
return this.format('uri')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
ipv4() {
|
|
77
|
+
return this.format('ipv4')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
ipv6() {
|
|
81
|
+
return this.format('ipv6')
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
uuid() {
|
|
85
|
+
return this.format('uuid')
|
|
86
|
+
}
|
|
87
|
+
}
|