@nmtjs/type 0.2.1 → 0.3.1
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/dist/types/union.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/union.ts"],"sourcesContent":["import { type TIntersect
|
|
1
|
+
{"version":3,"sources":["../../../src/types/union.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TIntersect,\n type TUnion,\n Type,\n // type UnionToTuple,\n} from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport type { UnionToTuple } from '../utils.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\n\nexport class UnionType<\n T extends [BaseType, BaseType, ...BaseType[]] = [\n BaseType,\n BaseType,\n ...BaseType[],\n ],\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n //@ts-expect-error\n TUnion<UnionToTuple<T[number][typeStatic]['schema']>>,\n N,\n O,\n D\n> {\n constructor(\n readonly types: T,\n options: SchemaOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(\n options: SchemaOptions,\n //@ts-expect-error\n ): TUnion<UnionToTuple<T[number][typeStatic]['schema']>> {\n return Type.Union(this.types.map(getTypeSchema), options) as any\n }\n\n nullable() {\n return new UnionType(this.types, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new UnionType(this.types, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new UnionType(\n this.types,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new UnionType(\n this.types,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new UnionType(\n this.types,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new UnionType(this.types, ...this._with({ options: { examples } }))\n }\n}\n\nexport class IntersactionType<\n T extends [BaseType, BaseType, ...BaseType[]] = [\n BaseType,\n BaseType,\n ...BaseType[],\n ],\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n // @ts-expect-error\n TIntersect<UnionToTuple<T[number][typeStatic]['schema']>>,\n N,\n O,\n D\n> {\n constructor(\n readonly types: T,\n options: SchemaOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(\n options: SchemaOptions,\n // @ts-expect-error\n ): TIntersect<UnionToTuple<T[number][typeStatic]['schema']>> {\n return Type.Intersect(this.types.map(getTypeSchema), options) as any\n }\n\n nullable() {\n return new IntersactionType(this.types, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new IntersactionType(this.types, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new IntersactionType(\n this.types,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new IntersactionType(\n this.types,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new IntersactionType(\n this.types,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...values: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new IntersactionType(\n this.types,\n ...this._with({ options: { examples: values } }),\n )\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","UnionType","constructor","types","options","isNullable","isOptional","hasDefault","_constructSchema","Union","map","nullable","_with","optional","nullish","default","value","description","examples","IntersactionType","Intersect","values"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"@sinclair/typebox": "^0.33.7",
|
|
23
23
|
"temporal-polyfill": "^0.2.5",
|
|
24
|
-
"@nmtjs/common": "0.
|
|
24
|
+
"@nmtjs/common": "0.3.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@sinclair/typebox": "^0.33.7",
|
|
28
28
|
"temporal-polyfill": "^0.2.5",
|
|
29
|
-
"@nmtjs/common": "0.
|
|
29
|
+
"@nmtjs/common": "0.3.1"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"src",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"LICENSE.md",
|
|
36
36
|
"README.md"
|
|
37
37
|
],
|
|
38
|
-
"version": "0.
|
|
38
|
+
"version": "0.3.1",
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "neemata-build -p neutral --root=./src './**/*.ts'",
|
|
41
41
|
"type-check": "tsc --noEmit"
|
package/src/compiler.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
type ValueErrorIterator,
|
|
4
4
|
} from '@sinclair/typebox/compiler'
|
|
5
5
|
import { Value } from '@sinclair/typebox/value'
|
|
6
|
-
import { type BaseType,
|
|
6
|
+
import { type BaseType, getTypeSchema } from './types/base.ts'
|
|
7
7
|
|
|
8
8
|
export type Compiled = {
|
|
9
9
|
check: (val: unknown) => boolean
|
|
@@ -19,7 +19,7 @@ export type Compiled = {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
const compileType = (type: BaseType) => {
|
|
22
|
-
const schema = type
|
|
22
|
+
const schema = getTypeSchema(type)
|
|
23
23
|
const compiled = TypeCompiler.Compile(schema)
|
|
24
24
|
const prepare = (value: any) => {
|
|
25
25
|
for (const fn of [Value.Clean, Value.Default]) {
|
package/src/constants.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { TLiteralValue } from '@sinclair/typebox'
|
|
2
2
|
import { ArrayType } from './types/array.ts'
|
|
3
|
-
import type { BaseType
|
|
3
|
+
import type { BaseType } from './types/base.ts'
|
|
4
4
|
import { BooleanType } from './types/boolean.ts'
|
|
5
5
|
import { CustomType } from './types/custom.ts'
|
|
6
|
-
import {
|
|
6
|
+
import { DateType } from './types/datetime.ts'
|
|
7
7
|
import { EnumType, NativeEnumType } from './types/enum.ts'
|
|
8
8
|
import { LiteralType } from './types/literal.ts'
|
|
9
9
|
import { IntegerType, NumberType } from './types/number.ts'
|
|
@@ -11,6 +11,7 @@ import { ObjectType } from './types/object.ts'
|
|
|
11
11
|
import { StringType } from './types/string.ts'
|
|
12
12
|
import { IntersactionType, UnionType } from './types/union.ts'
|
|
13
13
|
|
|
14
|
+
import type { typeStatic } from './constants.ts'
|
|
14
15
|
// register ajv formats
|
|
15
16
|
import { register } from './formats.ts'
|
|
16
17
|
import { AnyType } from './types/any.ts'
|
|
@@ -29,7 +30,6 @@ export {
|
|
|
29
30
|
ArrayType,
|
|
30
31
|
BooleanType,
|
|
31
32
|
CustomType,
|
|
32
|
-
DateTimeType,
|
|
33
33
|
DateType,
|
|
34
34
|
EnumType,
|
|
35
35
|
LiteralType,
|
|
@@ -45,8 +45,8 @@ export {
|
|
|
45
45
|
|
|
46
46
|
export namespace t {
|
|
47
47
|
export namespace infer {
|
|
48
|
-
export type decoded<T extends BaseType> = T[
|
|
49
|
-
export type encoded<T extends BaseType> = T[
|
|
48
|
+
export type decoded<T extends BaseType> = T[typeStatic]['decoded']
|
|
49
|
+
export type encoded<T extends BaseType> = T[typeStatic]['encoded']
|
|
50
50
|
}
|
|
51
51
|
export const never = () => new NeverType()
|
|
52
52
|
export const boolean = () => new BooleanType()
|
|
@@ -60,7 +60,6 @@ export namespace t {
|
|
|
60
60
|
export const arrayEnum = <T extends (string | number)[]>(enumLike: [...T]) =>
|
|
61
61
|
new EnumType(enumLike)
|
|
62
62
|
export const date = () => new DateType()
|
|
63
|
-
export const datetime = () => new DateTimeType()
|
|
64
63
|
export const array = <T extends BaseType>(element: T) =>
|
|
65
64
|
new ArrayType(element)
|
|
66
65
|
export const object = <T extends Record<string, BaseType>>(properties: T) =>
|
package/src/temporal.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { t as baseT } from './index.ts'
|
|
2
2
|
import {
|
|
3
3
|
DurationType,
|
|
4
4
|
PlainDateTimeType,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
ZonedDateTimeType,
|
|
10
10
|
} from './types/temporal.ts'
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
function extend<T extends typeof baseT>(value: T) {
|
|
13
13
|
return Object.assign({}, value, {
|
|
14
14
|
temporal: {
|
|
15
15
|
plainDate: () => new PlainDateType(),
|
|
@@ -23,6 +23,8 @@ export function extend<T extends typeof t>(value: T) {
|
|
|
23
23
|
})
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
export const t = extend(baseT)
|
|
27
|
+
|
|
26
28
|
export {
|
|
27
29
|
DurationType,
|
|
28
30
|
PlainDateTimeType,
|
package/src/types/any.ts
CHANGED
|
@@ -1,27 +1,50 @@
|
|
|
1
|
-
import { type TAny, Type } from '@sinclair/typebox'
|
|
1
|
+
import { type SchemaOptions, type TAny, Type } from '@sinclair/typebox'
|
|
2
2
|
import { BaseType } from './base.ts'
|
|
3
3
|
|
|
4
4
|
export class AnyType<
|
|
5
5
|
N extends boolean = false,
|
|
6
6
|
O extends boolean = false,
|
|
7
|
-
|
|
7
|
+
D extends boolean = false,
|
|
8
|
+
> extends BaseType<TAny, N, O, D, SchemaOptions> {
|
|
8
9
|
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
options: SchemaOptions = {},
|
|
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: SchemaOptions): TAny {
|
|
19
|
+
return Type.Any(options)
|
|
14
20
|
}
|
|
15
21
|
|
|
16
22
|
nullable() {
|
|
17
|
-
return new AnyType(...this.
|
|
23
|
+
return new AnyType(...this._with({ isNullable: true }))
|
|
18
24
|
}
|
|
19
25
|
|
|
20
26
|
optional() {
|
|
21
|
-
return new AnyType(...this.
|
|
27
|
+
return new AnyType(...this._with({ isOptional: true }))
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
nullish() {
|
|
25
|
-
return new AnyType(...this.
|
|
31
|
+
return new AnyType(...this._with({ isNullable: true, isOptional: true }))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
default(value: any) {
|
|
35
|
+
return new AnyType(
|
|
36
|
+
...this._with({
|
|
37
|
+
options: { default: value },
|
|
38
|
+
hasDefault: true,
|
|
39
|
+
}),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
description(description: string) {
|
|
44
|
+
return new AnyType(...this._with({ options: { description } }))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
examples(...examples: [any, ...any[]]) {
|
|
48
|
+
return new AnyType(...this._with({ options: { examples } }))
|
|
26
49
|
}
|
|
27
50
|
}
|
package/src/types/array.ts
CHANGED
|
@@ -1,66 +1,97 @@
|
|
|
1
1
|
import { type ArrayOptions, type TArray, Type } from '@sinclair/typebox'
|
|
2
|
-
import {
|
|
2
|
+
import type { typeStatic } from '../constants.ts'
|
|
3
|
+
import { BaseType, getTypeSchema } from './base.ts'
|
|
3
4
|
|
|
4
5
|
export class ArrayType<
|
|
5
6
|
T extends BaseType = BaseType,
|
|
6
7
|
N extends boolean = false,
|
|
7
8
|
O extends boolean = false,
|
|
8
|
-
|
|
9
|
+
D extends boolean = false,
|
|
10
|
+
> extends BaseType<TArray<T[typeStatic]['schema']>, N, O, D, ArrayOptions> {
|
|
9
11
|
constructor(
|
|
10
12
|
readonly element: T,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
options: ArrayOptions = {},
|
|
14
|
+
isNullable: N = false as N,
|
|
15
|
+
isOptional: O = false as O,
|
|
16
|
+
hasDefault: D = false as D,
|
|
14
17
|
) {
|
|
15
|
-
super(
|
|
18
|
+
super(options, isNullable, isOptional, hasDefault, element)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected _constructSchema(
|
|
22
|
+
options: ArrayOptions,
|
|
23
|
+
element: T,
|
|
24
|
+
): TArray<T[typeStatic]['schema']> {
|
|
25
|
+
return Type.Array(getTypeSchema(element), options)
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
nullable() {
|
|
19
|
-
|
|
20
|
-
return new ArrayType(this.element, this.options, ...args)
|
|
29
|
+
return new ArrayType(this.element, ...this._with({ isNullable: true }))
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
optional() {
|
|
24
|
-
|
|
25
|
-
return new ArrayType(this.element, this.options, ...args)
|
|
33
|
+
return new ArrayType(this.element, ...this._with({ isOptional: true }))
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
nullish() {
|
|
29
|
-
|
|
30
|
-
|
|
37
|
+
return new ArrayType(
|
|
38
|
+
this.element,
|
|
39
|
+
...this._with({ isNullable: true, isOptional: true }),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
default(value: this[typeStatic]['encoded']) {
|
|
44
|
+
return new ArrayType(
|
|
45
|
+
this.element,
|
|
46
|
+
...this._with({
|
|
47
|
+
options: { default: value },
|
|
48
|
+
hasDefault: true,
|
|
49
|
+
}),
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
description(description: string) {
|
|
54
|
+
return new ArrayType(
|
|
55
|
+
this.element,
|
|
56
|
+
...this._with({ options: { description } }),
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
examples(
|
|
61
|
+
...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
|
|
62
|
+
) {
|
|
63
|
+
return new ArrayType(
|
|
64
|
+
this.element,
|
|
65
|
+
...this._with({
|
|
66
|
+
options: { example: examples[0], examples },
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
31
69
|
}
|
|
32
70
|
|
|
33
71
|
min(value: number) {
|
|
34
72
|
return new ArrayType(
|
|
35
73
|
this.element,
|
|
36
|
-
{
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
},
|
|
40
|
-
...this._isNullableOptional,
|
|
74
|
+
...this._with({
|
|
75
|
+
options: { minItems: value },
|
|
76
|
+
}),
|
|
41
77
|
)
|
|
42
78
|
}
|
|
43
79
|
|
|
44
80
|
max(value: number) {
|
|
45
81
|
return new ArrayType(
|
|
46
82
|
this.element,
|
|
47
|
-
{
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
},
|
|
51
|
-
...this._isNullableOptional,
|
|
83
|
+
...this._with({
|
|
84
|
+
options: { maxItems: value },
|
|
85
|
+
}),
|
|
52
86
|
)
|
|
53
87
|
}
|
|
54
88
|
|
|
55
89
|
length(value: number) {
|
|
56
90
|
return new ArrayType(
|
|
57
91
|
this.element,
|
|
58
|
-
{
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
maxItems: value,
|
|
62
|
-
},
|
|
63
|
-
...this._isNullableOptional,
|
|
92
|
+
...this._with({
|
|
93
|
+
options: { minItems: value, maxItems: value },
|
|
94
|
+
}),
|
|
64
95
|
)
|
|
65
96
|
}
|
|
66
97
|
}
|
package/src/types/base.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type SchemaOptions,
|
|
2
3
|
type StaticDecode,
|
|
3
4
|
type StaticEncode,
|
|
4
5
|
type TAny,
|
|
@@ -6,32 +7,15 @@ import {
|
|
|
6
7
|
type TSchema,
|
|
7
8
|
Type,
|
|
8
9
|
} from '@sinclair/typebox'
|
|
10
|
+
import { typeSchema, typeStatic } from '../constants.ts'
|
|
9
11
|
import { Nullable, type TNullable } from '../schemas/nullable.ts'
|
|
10
12
|
|
|
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
13
|
type ResolveNullable<T extends TSchema, Is extends boolean> = Is extends true
|
|
30
|
-
?
|
|
14
|
+
? TNullable<T>
|
|
31
15
|
: T
|
|
32
16
|
|
|
33
17
|
type ResolveOptional<T extends TSchema, Is extends boolean> = Is extends true
|
|
34
|
-
?
|
|
18
|
+
? TOptional<T>
|
|
35
19
|
: T
|
|
36
20
|
|
|
37
21
|
type Resolve<
|
|
@@ -41,118 +25,81 @@ type Resolve<
|
|
|
41
25
|
> = ResolveOptional<ResolveNullable<Schema, IsNullable>, IsOptional>
|
|
42
26
|
|
|
43
27
|
export abstract class BaseType<
|
|
44
|
-
Schema extends TSchema =
|
|
28
|
+
Schema extends TSchema = TSchema,
|
|
45
29
|
IsNullable extends boolean = boolean,
|
|
46
30
|
IsOptional extends boolean = boolean,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
IsNullable,
|
|
50
|
-
IsOptional
|
|
51
|
-
>,
|
|
31
|
+
HasDefault extends boolean = boolean,
|
|
32
|
+
Options extends SchemaOptions = SchemaOptions,
|
|
52
33
|
> {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
protected abstract _constructSchema(
|
|
35
|
+
options: Options,
|
|
36
|
+
...constructArgs: any[]
|
|
37
|
+
): Schema
|
|
56
38
|
|
|
57
|
-
[
|
|
58
|
-
|
|
39
|
+
[typeStatic]!: {
|
|
40
|
+
schema: Resolve<Schema, IsNullable, IsOptional>
|
|
59
41
|
isOptional: IsOptional
|
|
60
42
|
isNullable: IsNullable
|
|
61
|
-
|
|
62
|
-
|
|
43
|
+
hasDefault: HasDefault
|
|
44
|
+
encoded: StaticEncode<Resolve<Schema, IsNullable, IsOptional>>
|
|
45
|
+
decoded: StaticDecode<
|
|
46
|
+
Resolve<Schema, IsNullable, HasDefault extends false ? false : IsOptional>
|
|
47
|
+
>
|
|
63
48
|
}
|
|
64
49
|
|
|
65
50
|
constructor(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
51
|
+
protected options: Options = {} as Options,
|
|
52
|
+
protected isNullable: IsNullable = false as IsNullable,
|
|
53
|
+
protected isOptional: IsOptional = false as IsOptional,
|
|
54
|
+
protected hasDefault: HasDefault = false as HasDefault,
|
|
55
|
+
...contstructArgs: any[]
|
|
69
56
|
) {
|
|
70
|
-
|
|
71
|
-
this
|
|
72
|
-
this[typeOptional] = optional
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
get [typeFinalSchema](): Final {
|
|
76
|
-
let schema: TSchema = this._schema
|
|
77
|
-
if (this._isNullable) {
|
|
57
|
+
let schema: TSchema = this._constructSchema(options, ...contstructArgs)
|
|
58
|
+
if (this.isNullable) {
|
|
78
59
|
schema = Nullable(schema)
|
|
79
60
|
}
|
|
80
|
-
if (this.
|
|
61
|
+
if (this.isOptional) {
|
|
81
62
|
schema = Type.Optional(schema)
|
|
82
63
|
}
|
|
83
|
-
|
|
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])
|
|
64
|
+
this[typeSchema] = schema as Schema
|
|
108
65
|
}
|
|
66
|
+
protected [typeSchema]: Schema
|
|
109
67
|
|
|
110
|
-
protected
|
|
111
|
-
return this.
|
|
68
|
+
protected get _args(): [IsNullable, IsOptional, HasDefault] {
|
|
69
|
+
return [this.isNullable, this.isOptional, this.hasDefault]
|
|
112
70
|
}
|
|
113
71
|
|
|
114
|
-
protected
|
|
115
|
-
|
|
72
|
+
protected _with<
|
|
73
|
+
_IsNullable extends boolean = IsNullable,
|
|
74
|
+
_IsOptional extends boolean = IsOptional,
|
|
75
|
+
_HasDefault extends boolean = HasDefault,
|
|
76
|
+
>({
|
|
77
|
+
options = this.options as Options,
|
|
78
|
+
isNullable = this.isNullable as unknown as _IsNullable,
|
|
79
|
+
isOptional = this.isOptional as unknown as _IsOptional,
|
|
80
|
+
hasDefault = this.hasDefault as unknown as _HasDefault,
|
|
81
|
+
}: {
|
|
82
|
+
options?: Options
|
|
83
|
+
isNullable?: _IsNullable
|
|
84
|
+
isOptional?: _IsOptional
|
|
85
|
+
hasDefault?: _HasDefault
|
|
86
|
+
} = {}): [Options, _IsNullable, _IsOptional, _HasDefault] {
|
|
87
|
+
return [{ ...this.options, ...options }, isNullable, isOptional, hasDefault]
|
|
116
88
|
}
|
|
117
89
|
|
|
118
|
-
abstract
|
|
119
|
-
abstract
|
|
120
|
-
abstract
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
}
|
|
90
|
+
abstract optional(): BaseType<Schema, IsNullable, true, HasDefault>
|
|
91
|
+
abstract nullish(): BaseType<Schema, true, true, HasDefault>
|
|
92
|
+
abstract default(value: any): BaseType<Schema, IsNullable, IsOptional, true>
|
|
93
|
+
abstract description(
|
|
94
|
+
value: string,
|
|
95
|
+
): BaseType<Schema, IsNullable, IsOptional, HasDefault>
|
|
96
|
+
abstract examples(
|
|
97
|
+
...values: any[]
|
|
98
|
+
): BaseType<Schema, IsNullable, IsOptional, HasDefault>
|
|
154
99
|
}
|
|
155
100
|
|
|
156
|
-
export function getTypeSchema<T extends BaseType>(
|
|
157
|
-
|
|
101
|
+
export function getTypeSchema<T extends BaseType>(
|
|
102
|
+
type: T,
|
|
103
|
+
): T[typeStatic]['schema'] {
|
|
104
|
+
return type[typeSchema]
|
|
158
105
|
}
|
package/src/types/boolean.ts
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
import { type TBoolean, Type } from '@sinclair/typebox'
|
|
1
|
+
import { type SchemaOptions, type TBoolean, Type } from '@sinclair/typebox'
|
|
2
2
|
import { BaseType } from './base.ts'
|
|
3
3
|
|
|
4
4
|
export class BooleanType<
|
|
5
5
|
N extends boolean = false,
|
|
6
6
|
O extends boolean = false,
|
|
7
|
-
|
|
7
|
+
D extends boolean = false,
|
|
8
|
+
> extends BaseType<TBoolean, N, O, D> {
|
|
8
9
|
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
options: SchemaOptions = {},
|
|
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: SchemaOptions): TBoolean {
|
|
19
|
+
return Type.Boolean(options)
|
|
14
20
|
}
|
|
15
21
|
|
|
16
22
|
nullable() {
|
|
17
|
-
return new BooleanType(...this.
|
|
23
|
+
return new BooleanType(...this._with({ isNullable: true }))
|
|
18
24
|
}
|
|
19
25
|
|
|
20
26
|
optional() {
|
|
21
|
-
return new BooleanType(...this.
|
|
27
|
+
return new BooleanType(...this._with({ isOptional: true }))
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
nullish() {
|
|
25
|
-
return new BooleanType(
|
|
31
|
+
return new BooleanType(
|
|
32
|
+
...this._with({ isNullable: true, isOptional: true }),
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
default(value: boolean) {
|
|
37
|
+
return new BooleanType(
|
|
38
|
+
...this._with({ options: { default: value }, hasDefault: true }),
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
description(description: string) {
|
|
43
|
+
return new BooleanType(...this._with({ options: { description } }))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
examples(...examples: [boolean, ...boolean[]]) {
|
|
47
|
+
return new BooleanType(...this._with({ options: { examples } }))
|
|
26
48
|
}
|
|
27
49
|
}
|