@nmtjs/type 0.4.1 → 0.4.3
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/constants.js +2 -2
- package/dist/constants.js.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/schemas/nullable.js +13 -2
- package/dist/schemas/nullable.js.map +1 -1
- package/dist/types/enum.js +6 -4
- package/dist/types/enum.js.map +1 -1
- package/dist/types/object.js +2 -10
- package/dist/types/object.js.map +1 -1
- package/package.json +3 -3
- package/src/constants.ts +2 -2
- package/src/index.ts +1 -3
- package/src/schemas/nullable.ts +20 -1
- package/src/types/enum.ts +10 -16
- package/src/types/object.ts +2 -20
- package/dist/schemas/native-enum.js +0 -14
- package/dist/schemas/native-enum.js.map +0 -1
- package/dist/schemas/union-enum.js +0 -13
- package/dist/schemas/union-enum.js.map +0 -1
- package/src/schemas/native-enum.ts +0 -29
- package/src/schemas/union-enum.ts +0 -43
package/dist/constants.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const typeStatic = Symbol();
|
|
2
|
-
export const typeSchema = Symbol();
|
|
1
|
+
export const typeStatic = Symbol.for('neemataType:typeStatic');
|
|
2
|
+
export const typeSchema = Symbol.for('neemataType:typeSchema');
|
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/constants.ts"],"sourcesContent":["export const typeStatic: unique symbol = Symbol()\nexport type typeStatic = typeof typeStatic\n\nexport const typeSchema: unique symbol = Symbol()\nexport type typeSchema = typeof typeSchema\n"],"names":["typeStatic","Symbol","typeSchema"],"mappings":"AAAA,OAAO,MAAMA,aAA4BC,
|
|
1
|
+
{"version":3,"sources":["../../src/constants.ts"],"sourcesContent":["export const typeStatic: unique symbol = Symbol.for('neemataType:typeStatic')\nexport type typeStatic = typeof typeStatic\n\nexport const typeSchema: unique symbol = Symbol.for('neemataType:typeSchema')\nexport type typeSchema = typeof typeSchema\n"],"names":["typeStatic","Symbol","for","typeSchema"],"mappings":"AAAA,OAAO,MAAMA,aAA4BC,OAAOC,GAAG,CAAC,0BAAyB;AAG7E,OAAO,MAAMC,aAA4BF,OAAOC,GAAG,CAAC,0BAAyB"}
|
package/dist/index.js
CHANGED
|
@@ -12,8 +12,6 @@ import { AnyType } from "./types/any.js";
|
|
|
12
12
|
import { NeverType } from "./types/never.js";
|
|
13
13
|
import { register } from "./formats.js";
|
|
14
14
|
register();
|
|
15
|
-
export * from "./schemas/native-enum.js";
|
|
16
|
-
export * from "./schemas/union-enum.js";
|
|
17
15
|
export * from "./schemas/nullable.js";
|
|
18
16
|
export { BaseType, getTypeSchema } from "./types/base.js";
|
|
19
17
|
export { ArrayType, BooleanType, CustomType, DateType, EnumType, LiteralType, IntegerType, NumberType, ObjectType, StringType, IntersactionType, UnionType, AnyType, NeverType, };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type { TLiteralValue } from '@sinclair/typebox'\nimport { ArrayType } from './types/array.ts'\nimport type { BaseType } from './types/base.ts'\nimport { BooleanType } from './types/boolean.ts'\nimport { CustomType } from './types/custom.ts'\nimport { DateType } from './types/datetime.ts'\nimport {\n type AnyEnumType,\n type AnyObjectEnumType,\n EnumType,\n ObjectEnumType,\n} from './types/enum.ts'\nimport { type AnyLiteralType, LiteralType } from './types/literal.ts'\nimport { BigIntType, IntegerType, NumberType } from './types/number.ts'\nimport { ObjectType, RecordType } from './types/object.ts'\nimport { type AnyStringType, StringType } from './types/string.ts'\nimport {\n type AnyUnionType,\n IntersactionType,\n UnionType,\n} from './types/union.ts'\n\nimport type { typeStatic } from './constants.ts'\nimport { AnyType } from './types/any.ts'\nimport { NeverType } from './types/never.ts'\n\n// register ajv formats\nimport { register } from './formats.ts'\nregister()\n\nexport * from './schemas/
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type { TLiteralValue } from '@sinclair/typebox/type'\nimport { ArrayType } from './types/array.ts'\nimport type { BaseType } from './types/base.ts'\nimport { BooleanType } from './types/boolean.ts'\nimport { CustomType } from './types/custom.ts'\nimport { DateType } from './types/datetime.ts'\nimport {\n type AnyEnumType,\n type AnyObjectEnumType,\n EnumType,\n ObjectEnumType,\n} from './types/enum.ts'\nimport { type AnyLiteralType, LiteralType } from './types/literal.ts'\nimport { BigIntType, IntegerType, NumberType } from './types/number.ts'\nimport { ObjectType, RecordType } from './types/object.ts'\nimport { type AnyStringType, StringType } from './types/string.ts'\nimport {\n type AnyUnionType,\n IntersactionType,\n UnionType,\n} from './types/union.ts'\n\nimport type { typeStatic } from './constants.ts'\nimport { AnyType } from './types/any.ts'\nimport { NeverType } from './types/never.ts'\n\n// register ajv formats\nimport { register } from './formats.ts'\nregister()\n\nexport * from './schemas/nullable.ts'\nexport {\n BaseType,\n getTypeSchema,\n} from './types/base.ts'\nexport { type TSchema } from '@sinclair/typebox'\nexport {\n ArrayType,\n BooleanType,\n CustomType,\n DateType,\n EnumType,\n LiteralType,\n IntegerType,\n NumberType,\n ObjectType,\n StringType,\n IntersactionType,\n UnionType,\n AnyType,\n NeverType,\n}\n\nexport namespace t {\n export namespace infer {\n export type staticType<T extends BaseType> = T[typeStatic]\n export type decoded<T extends BaseType> = staticType<T>['decoded']\n export type encoded<T extends BaseType> = staticType<T>['encoded']\n }\n export const never = () => new NeverType()\n export const boolean = () => new BooleanType()\n export const string = () => new StringType()\n export const number = () => new NumberType()\n export const integer = () => new IntegerType()\n export const bitint = () => new BigIntType()\n export const literal = <T extends TLiteralValue>(value: T) =>\n new LiteralType(value)\n export const objectEnum = <T extends { [K in string]: K }>(enumLike: T) =>\n new ObjectEnumType(enumLike)\n export const arrayEnum = <T extends (string | number)[]>(enumLike: [...T]) =>\n new EnumType(enumLike)\n export const date = () => new DateType()\n export const array = <T extends BaseType>(element: T) =>\n new ArrayType(element)\n export const object = <T extends Record<string, BaseType>>(properties: T) =>\n new ObjectType(properties)\n export const record = <\n K extends\n | AnyLiteralType\n | AnyEnumType\n | AnyObjectEnumType\n | AnyStringType\n | AnyUnionType,\n E extends BaseType,\n >(\n key: K,\n value: E,\n ) => new RecordType(key, value)\n export const any = () => new AnyType()\n export const or = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new UnionType(types)\n export const and = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new IntersactionType(types)\n export const custom = <T>(\n decode: (value: any) => T,\n encode: (value: T) => any,\n ) => new CustomType<T>(decode, encode)\n}\n"],"names":["ArrayType","BooleanType","CustomType","DateType","EnumType","ObjectEnumType","LiteralType","BigIntType","IntegerType","NumberType","ObjectType","RecordType","StringType","IntersactionType","UnionType","AnyType","NeverType","register","BaseType","getTypeSchema","t","never","boolean","string","number","integer","bitint","literal","value","objectEnum","enumLike","arrayEnum","date","array","element","object","properties","record","key","any","or","types","and","custom","decode","encode"],"mappings":"AACA,SAASA,SAAS,QAAQ,mBAAkB;AAE5C,SAASC,WAAW,QAAQ,qBAAoB;AAChD,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,QAAQ,QAAQ,sBAAqB;AAC9C,SAGEC,QAAQ,EACRC,cAAc,QACT,kBAAiB;AACxB,SAA8BC,WAAW,QAAQ,qBAAoB;AACrE,SAASC,UAAU,EAAEC,WAAW,EAAEC,UAAU,QAAQ,oBAAmB;AACvE,SAASC,UAAU,EAAEC,UAAU,QAAQ,oBAAmB;AAC1D,SAA6BC,UAAU,QAAQ,oBAAmB;AAClE,SAEEC,gBAAgB,EAChBC,SAAS,QACJ,mBAAkB;AAGzB,SAASC,OAAO,QAAQ,iBAAgB;AACxC,SAASC,SAAS,QAAQ,mBAAkB;AAG5C,SAASC,QAAQ,QAAQ,eAAc;AACvCA;AAEA,cAAc,wBAAuB;AACrC,SACEC,QAAQ,EACRC,aAAa,QACR,kBAAiB;AAExB,SACEnB,SAAS,EACTC,WAAW,EACXC,UAAU,EACVC,QAAQ,EACRC,QAAQ,EACRE,WAAW,EACXE,WAAW,EACXC,UAAU,EACVC,UAAU,EACVE,UAAU,EACVC,gBAAgB,EAChBC,SAAS,EACTC,OAAO,EACPC,SAAS,KACV;;UAEgBI;MAMFC,QAAQ,IAAM,IAAIL;MAClBM,UAAU,IAAM,IAAIrB;MACpBsB,SAAS,IAAM,IAAIX;MACnBY,SAAS,IAAM,IAAIf;MACnBgB,UAAU,IAAM,IAAIjB;MACpBkB,SAAS,IAAM,IAAInB;MACnBoB,UAAU,CAA0BC,QAC/C,IAAItB,YAAYsB;MACLC,aAAa,CAAiCC,WACzD,IAAIzB,eAAeyB;MACRC,YAAY,CAAgCD,WACvD,IAAI1B,SAAS0B;MACFE,OAAO,IAAM,IAAI7B;MACjB8B,QAAQ,CAAqBC,UACxC,IAAIlC,UAAUkC;MACHC,SAAS,CAAqCC,aACzD,IAAI1B,WAAW0B;MACJC,SAAS,CASpBC,KACAV,QACG,IAAIjB,WAAW2B,KAAKV;MACZW,MAAM,IAAM,IAAIxB;MAChByB,KAAK,CAChB,GAAGC,QACA,IAAI3B,UAAU2B;MACNC,MAAM,CACjB,GAAGD,QACA,IAAI5B,iBAAiB4B;MACbE,SAAS,CACpBC,QACAC,SACG,IAAI3C,WAAc0C,QAAQC;AACjC,GA9CiBzB,MAAAA"}
|
package/dist/schemas/nullable.js
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox/type';
|
|
2
|
-
export const Nullable = (schema, options = {})=>
|
|
2
|
+
export const Nullable = (schema, options = {})=>{
|
|
3
|
+
const { default: _default, description, examples, readOnly, title, writeOnly } = schema;
|
|
4
|
+
return Type.Union([
|
|
3
5
|
schema,
|
|
4
6
|
Type.Null()
|
|
5
|
-
],
|
|
7
|
+
], {
|
|
8
|
+
default: _default,
|
|
9
|
+
description,
|
|
10
|
+
examples,
|
|
11
|
+
readOnly,
|
|
12
|
+
title,
|
|
13
|
+
writeOnly,
|
|
14
|
+
...options
|
|
15
|
+
});
|
|
16
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/nullable.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TNull,\n type TSchema,\n type TUnion,\n Type,\n} from '@sinclair/typebox/type'\n\nexport type TNullable<T extends TSchema> = TUnion<[T, TNull]>\nexport const Nullable = <T extends TSchema>(\n schema: T,\n options: SchemaOptions = {},\n) => Type.Union([schema, Type.Null()], options)\n"],"names":["Type","Nullable","schema","options","Union","Null"],"mappings":"AAAA,SAKEA,IAAI,QACC,yBAAwB;AAG/B,OAAO,MAAMC,WAAW,CACtBC,QACAC,UAAyB,CAAC,CAAC,
|
|
1
|
+
{"version":3,"sources":["../../../src/schemas/nullable.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TNull,\n type TSchema,\n type TUnion,\n Type,\n} from '@sinclair/typebox/type'\n\nexport type TNullable<T extends TSchema> = TUnion<[T, TNull]>\nexport const Nullable = <T extends TSchema>(\n schema: T,\n options: SchemaOptions = {},\n) => {\n const {\n default: _default,\n description,\n examples,\n readOnly,\n title,\n writeOnly,\n } = schema\n\n return Type.Union([schema, Type.Null()], {\n default: _default,\n description,\n examples,\n readOnly,\n title,\n writeOnly,\n ...options,\n })\n}\n"],"names":["Type","Nullable","schema","options","default","_default","description","examples","readOnly","title","writeOnly","Union","Null"],"mappings":"AAAA,SAKEA,IAAI,QACC,yBAAwB;AAG/B,OAAO,MAAMC,WAAW,CACtBC,QACAC,UAAyB,CAAC,CAAC;IAE3B,MAAM,EACJC,SAASC,QAAQ,EACjBC,WAAW,EACXC,QAAQ,EACRC,QAAQ,EACRC,KAAK,EACLC,SAAS,EACV,GAAGR;IAEJ,OAAOF,KAAKW,KAAK,CAAC;QAACT;QAAQF,KAAKY,IAAI;KAAG,EAAE;QACvCR,SAASC;QACTC;QACAC;QACAC;QACAC;QACAC;QACA,GAAGP,OAAO;IACZ;AACF,EAAC"}
|
package/dist/types/enum.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { UnionEnum } from "../schemas/union-enum.js";
|
|
1
|
+
import { Enum } from '@sinclair/typebox';
|
|
3
2
|
import { BaseType } from "./base.js";
|
|
4
3
|
export class ObjectEnumType extends BaseType {
|
|
5
4
|
values;
|
|
@@ -8,7 +7,7 @@ export class ObjectEnumType extends BaseType {
|
|
|
8
7
|
this.values = values;
|
|
9
8
|
}
|
|
10
9
|
_constructSchema(options, values) {
|
|
11
|
-
return
|
|
10
|
+
return Enum(values, options);
|
|
12
11
|
}
|
|
13
12
|
nullable() {
|
|
14
13
|
return new ObjectEnumType(this.values, ...this._with({
|
|
@@ -56,7 +55,10 @@ export class EnumType extends BaseType {
|
|
|
56
55
|
this.values = values;
|
|
57
56
|
}
|
|
58
57
|
_constructSchema(options, values) {
|
|
59
|
-
return
|
|
58
|
+
return Enum(Object.fromEntries(values.map((k)=>[
|
|
59
|
+
k,
|
|
60
|
+
k
|
|
61
|
+
])), options);
|
|
60
62
|
}
|
|
61
63
|
nullable() {
|
|
62
64
|
return new EnumType(this.values, ...this._with({
|
package/dist/types/enum.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import { Enum, type SchemaOptions, type TEnum } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport type AnyObjectEnumType<T extends { [K in string]: K } = any> =\n ObjectEnumType<T, boolean, boolean, boolean>\nexport class ObjectEnumType<\n T extends { [K in string]: K },\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TEnum<T>, N, O, D> {\n constructor(\n private readonly values: 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, values)\n }\n\n protected _constructSchema(options: SchemaOptions, values: T): TEnum<T> {\n return Enum(values, options)\n }\n\n nullable() {\n return new ObjectEnumType(this.values, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ObjectEnumType(this.values, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ObjectEnumType(\n this.values,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: keyof T) {\n return new ObjectEnumType(\n this.values,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new ObjectEnumType(\n this.values,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: (keyof T)[]) {\n return new ObjectEnumType(\n this.values,\n ...this._with({ options: { examples } }),\n )\n }\n}\n\nexport type AnyEnumType = EnumType<any, boolean, boolean, boolean>\nexport class EnumType<\n T extends (string | number)[] = (string | number)[],\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TEnum<Record<string, T[number]>>, N, O, D> {\n constructor(\n private readonly values: [...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, values)\n }\n\n protected _constructSchema(\n options: SchemaOptions,\n values: [...T],\n ): TEnum<Record<string, T[number]>> {\n return Enum(Object.fromEntries(values.map((k) => [k, k])), options)\n }\n\n nullable() {\n return new EnumType(this.values, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new EnumType(this.values, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new EnumType(\n this.values,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: T[number]) {\n return new EnumType(\n this.values,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new EnumType(\n this.values,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: [T[number], ...T[number][]]) {\n return new EnumType(this.values, ...this._with({ options: { examples } }))\n }\n}\n"],"names":["Enum","BaseType","ObjectEnumType","constructor","values","options","isNullable","isOptional","hasDefault","_constructSchema","nullable","_with","optional","nullish","default","value","description","examples","EnumType","Object","fromEntries","map","k"],"mappings":"AAAA,SAASA,IAAI,QAAwC,oBAAmB;AACxE,SAASC,QAAQ,QAAQ,YAAW;AAIpC,OAAO,MAAMC,uBAKHD;;IACRE,YACE,AAAiBC,MAAS,EAC1BC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aANlCA,SAAAA;IAOnB;IAEUK,iBAAiBJ,OAAsB,EAAED,MAAS,EAAY;QACtE,OAAOJ,KAAKI,QAAQC;IACtB;IAEAK,WAAW;QACT,OAAO,IAAIR,eAAe,IAAI,CAACE,MAAM,KAAK,IAAI,CAACO,KAAK,CAAC;YAAEL,YAAY;QAAK;IAC1E;IAEAM,WAAW;QACT,OAAO,IAAIV,eAAe,IAAI,CAACE,MAAM,KAAK,IAAI,CAACO,KAAK,CAAC;YAAEJ,YAAY;QAAK;IAC1E;IAEAM,UAAU;QACR,OAAO,IAAIX,eACT,IAAI,CAACE,MAAM,KACR,IAAI,CAACO,KAAK,CAAC;YAAEL,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAO,QAAQC,KAAc,EAAE;QACtB,OAAO,IAAIb,eACT,IAAI,CAACE,MAAM,KACR,IAAI,CAACO,KAAK,CAAC;YAAEN,SAAS;gBAAES,SAASC;YAAM;YAAGP,YAAY;QAAK;IAElE;IAEAQ,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAId,eACT,IAAI,CAACE,MAAM,KACR,IAAI,CAACO,KAAK,CAAC;YAAEN,SAAS;gBAAEW;YAAY;QAAE;IAE7C;IAEAC,SAAS,GAAGA,QAAqB,EAAE;QACjC,OAAO,IAAIf,eACT,IAAI,CAACE,MAAM,KACR,IAAI,CAACO,KAAK,CAAC;YAAEN,SAAS;gBAAEY;YAAS;QAA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@@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType, getTypeSchema } from "./base.js";
|
|
3
|
-
import { EnumType
|
|
3
|
+
import { EnumType } from "./enum.js";
|
|
4
4
|
export class ObjectType extends BaseType {
|
|
5
5
|
properties;
|
|
6
6
|
constructor(properties = {}, options = {}, isNullable = false, isOptional = false, hasDefault = false){
|
|
@@ -97,15 +97,7 @@ export class RecordType extends BaseType {
|
|
|
97
97
|
this.element = element;
|
|
98
98
|
}
|
|
99
99
|
_constructSchema(options, key, element) {
|
|
100
|
-
|
|
101
|
-
if (key instanceof EnumType) {
|
|
102
|
-
keySchema = Type.Union(key.values.map((value)=>Type.Literal(value)));
|
|
103
|
-
} else if (key instanceof ObjectEnumType) {
|
|
104
|
-
keySchema = Type.Union(Object.values(key.values).map((value)=>Type.Literal(value)));
|
|
105
|
-
} else {
|
|
106
|
-
keySchema = getTypeSchema(key);
|
|
107
|
-
}
|
|
108
|
-
return Type.Record(keySchema, getTypeSchema(element), options);
|
|
100
|
+
return Type.Record(getTypeSchema(key), getTypeSchema(element), options);
|
|
109
101
|
}
|
|
110
102
|
nullable() {
|
|
111
103
|
return new RecordType(this.key, this.element, ...this._with({
|
package/dist/types/object.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/object.ts"],"sourcesContent":["import {\n type ObjectOptions,\n type TObject,\n type TRecord,\n type TSchema,\n Type,\n} from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport type { UnionToTupleString } from '../utils.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\nimport {\n type AnyEnumType,\n type AnyObjectEnumType,\n EnumType,\n ObjectEnumType,\n} from './enum.ts'\nimport type { AnyLiteralType } from './literal.ts'\nimport type { AnyStringType } from './string.ts'\nimport type { AnyUnionType } from './union.ts'\n\nexport class ObjectType<\n T extends Record<string, BaseType>,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,\n N,\n O,\n D\n> {\n constructor(\n protected readonly properties: T = {} as T,\n options: ObjectOptions = {},\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, properties)\n }\n\n protected _constructSchema(\n options: ObjectOptions,\n properties: T,\n ): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {\n const schemaProperties = {} as {\n [K in keyof T]: T[K][typeStatic]['schema']\n }\n\n for (const [key, value] of Object.entries(properties)) {\n // @ts-expect-error\n schemaProperties[key] = getTypeSchema(value)\n }\n return Type.Object(schemaProperties, options)\n }\n\n nullable() {\n return new ObjectType(this.properties, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ObjectType(this.properties, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ObjectType(\n this.properties,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { examples } }),\n )\n }\n\n pick<P extends { [K in keyof T]?: true }>(pick: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => pick[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Pick<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n omit<P extends { [K in keyof T]?: true }>(omit: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => !omit[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Omit<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n extend<P extends Record<string, BaseType>>(properties: P) {\n const [_, ...args] = this._with()\n return new ObjectType({ ...this.properties, ...properties }, {}, ...args)\n }\n\n merge<T extends ObjectType<Record<string, BaseType>>>(object: T) {\n const [_, ...args] = this._with()\n return new ObjectType(\n { ...this.properties, ...object.properties },\n {},\n ...args,\n )\n }\n\n partial() {\n const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =\n {} as any\n for (const [key, value] of Object.entries(this.properties)) {\n // @ts-expect-error\n properties[key] = value.optional()\n }\n const [_, ...args] = this._with()\n return new ObjectType(properties, {}, ...args)\n }\n\n keyof(): EnumType<UnionToTupleString<keyof T>> {\n return new EnumType(Object.keys(this.properties) as any)\n }\n}\n\nexport class RecordType<\n K extends\n | AnyLiteralType\n | AnyEnumType\n | AnyObjectEnumType\n | AnyStringType\n | AnyUnionType,\n E extends BaseType,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>,\n N,\n O,\n D,\n ObjectOptions\n> {\n constructor(\n protected readonly key: K,\n protected readonly element: E,\n options: ObjectOptions = {},\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, key, element)\n }\n\n protected _constructSchema(options: ObjectOptions, key: K, element: E) {\n let keySchema: TSchema\n\n if (key instanceof EnumType) {\n keySchema = Type.Union(key.values.map((value) => Type.Literal(value)))\n } else if (key instanceof ObjectEnumType) {\n keySchema = Type.Union(\n Object.values(key.values).map((value) => Type.Literal(value as string)),\n )\n } else {\n keySchema = getTypeSchema(key)\n }\n\n return Type.Record(\n keySchema,\n getTypeSchema(element),\n options,\n ) as unknown as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>\n }\n\n nullable() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true }),\n )\n }\n\n optional() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isOptional: true }),\n )\n }\n\n nullish() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { examples } }),\n )\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","EnumType","ObjectEnumType","ObjectType","constructor","properties","options","isNullable","isOptional","hasDefault","_constructSchema","schemaProperties","key","value","Object","entries","nullable","_with","optional","nullish","default","description","examples","pick","fromEntries","filter","_","args","omit","extend","merge","object","partial","keyof","keys","RecordType","element","keySchema","Union","values","map","Literal","Record"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAG1B,SAASC,QAAQ,EAAEC,aAAa,QAAQ,YAAW;AACnD,SAGEC,QAAQ,EACRC,cAAc,QACT,YAAW;AAKlB,OAAO,MAAMC,mBAKHJ;;IAMRK,YACE,AAAmBC,aAAgB,CAAC,CAAM,EAC1CC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aANhCA,aAAAA;IAOrB;IAEUK,iBACRJ,OAAsB,EACtBD,UAAa,EAC4C;QACzD,MAAMM,mBAAmB,CAAC;QAI1B,KAAK,MAAM,CAACC,KAAKC,MAAM,IAAIC,OAAOC,OAAO,CAACV,YAAa;YAErDM,gBAAgB,CAACC,IAAI,GAAGZ,cAAca;QACxC;QACA,OAAOf,KAAKgB,MAAM,CAACH,kBAAkBL;IACvC;IAEAU,WAAW;QACT,OAAO,IAAIb,WAAW,IAAI,CAACE,UAAU,KAAK,IAAI,CAACY,KAAK,CAAC;YAAEV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,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEX,SAAS;gBAAEgB;YAAS;QAAE;IAE1C;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/types/object.ts"],"sourcesContent":["import {\n type ObjectOptions,\n type TObject,\n type TRecord,\n Type,\n} from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport type { UnionToTupleString } from '../utils.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\nimport { type AnyEnumType, type AnyObjectEnumType, EnumType } from './enum.ts'\nimport type { AnyLiteralType } from './literal.ts'\nimport type { AnyStringType } from './string.ts'\nimport type { AnyUnionType } from './union.ts'\n\nexport class ObjectType<\n T extends Record<string, BaseType>,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,\n N,\n O,\n D\n> {\n constructor(\n protected readonly properties: T = {} as T,\n options: ObjectOptions = {},\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, properties)\n }\n\n protected _constructSchema(\n options: ObjectOptions,\n properties: T,\n ): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {\n const schemaProperties = {} as {\n [K in keyof T]: T[K][typeStatic]['schema']\n }\n\n for (const [key, value] of Object.entries(properties)) {\n // @ts-expect-error\n schemaProperties[key] = getTypeSchema(value)\n }\n return Type.Object(schemaProperties, options)\n }\n\n nullable() {\n return new ObjectType(this.properties, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ObjectType(this.properties, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ObjectType(\n this.properties,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { examples } }),\n )\n }\n\n pick<P extends { [K in keyof T]?: true }>(pick: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => pick[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Pick<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n omit<P extends { [K in keyof T]?: true }>(omit: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => !omit[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Omit<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n extend<P extends Record<string, BaseType>>(properties: P) {\n const [_, ...args] = this._with()\n return new ObjectType({ ...this.properties, ...properties }, {}, ...args)\n }\n\n merge<T extends ObjectType<Record<string, BaseType>>>(object: T) {\n const [_, ...args] = this._with()\n return new ObjectType(\n { ...this.properties, ...object.properties },\n {},\n ...args,\n )\n }\n\n partial() {\n const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =\n {} as any\n for (const [key, value] of Object.entries(this.properties)) {\n // @ts-expect-error\n properties[key] = value.optional()\n }\n const [_, ...args] = this._with()\n return new ObjectType(properties, {}, ...args)\n }\n\n keyof(): EnumType<UnionToTupleString<keyof T>> {\n return new EnumType(Object.keys(this.properties) as any)\n }\n}\n\nexport class RecordType<\n K extends\n | AnyLiteralType\n | AnyEnumType\n | AnyObjectEnumType\n | AnyStringType\n | AnyUnionType,\n E extends BaseType,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>,\n N,\n O,\n D,\n ObjectOptions\n> {\n constructor(\n protected readonly key: K,\n protected readonly element: E,\n options: ObjectOptions = {},\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, key, element)\n }\n\n protected _constructSchema(options: ObjectOptions, key: K, element: E) {\n return Type.Record(\n getTypeSchema(key),\n getTypeSchema(element),\n options,\n ) as unknown as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>\n }\n\n nullable() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true }),\n )\n }\n\n optional() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isOptional: true }),\n )\n }\n\n nullish() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { examples } }),\n )\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","EnumType","ObjectType","constructor","properties","options","isNullable","isOptional","hasDefault","_constructSchema","schemaProperties","key","value","Object","entries","nullable","_with","optional","nullish","default","description","examples","pick","fromEntries","filter","_","args","omit","extend","merge","object","partial","keyof","keys","RecordType","element","Record"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAG1B,SAASC,QAAQ,EAAEC,aAAa,QAAQ,YAAW;AACnD,SAAmDC,QAAQ,QAAQ,YAAW;AAK9E,OAAO,MAAMC,mBAKHH;;IAMRI,YACE,AAAmBC,aAAgB,CAAC,CAAM,EAC1CC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aANhCA,aAAAA;IAOrB;IAEUK,iBACRJ,OAAsB,EACtBD,UAAa,EAC4C;QACzD,MAA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AE1C;AACF"}
|
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.4.
|
|
24
|
+
"@nmtjs/common": "0.4.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@sinclair/typebox": "^0.33.7",
|
|
28
28
|
"temporal-polyfill": "^0.2.5",
|
|
29
|
-
"@nmtjs/common": "0.4.
|
|
29
|
+
"@nmtjs/common": "0.4.3"
|
|
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.4.
|
|
38
|
+
"version": "0.4.3",
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "neemata-build -p neutral --root=./src './**/*.ts'",
|
|
41
41
|
"type-check": "tsc --noEmit"
|
package/src/constants.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export const typeStatic: unique symbol = Symbol()
|
|
1
|
+
export const typeStatic: unique symbol = Symbol.for('neemataType:typeStatic')
|
|
2
2
|
export type typeStatic = typeof typeStatic
|
|
3
3
|
|
|
4
|
-
export const typeSchema: unique symbol = Symbol()
|
|
4
|
+
export const typeSchema: unique symbol = Symbol.for('neemataType:typeSchema')
|
|
5
5
|
export type typeSchema = typeof typeSchema
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { TLiteralValue } from '@sinclair/typebox'
|
|
1
|
+
import type { TLiteralValue } from '@sinclair/typebox/type'
|
|
2
2
|
import { ArrayType } from './types/array.ts'
|
|
3
3
|
import type { BaseType } from './types/base.ts'
|
|
4
4
|
import { BooleanType } from './types/boolean.ts'
|
|
@@ -28,8 +28,6 @@ import { NeverType } from './types/never.ts'
|
|
|
28
28
|
import { register } from './formats.ts'
|
|
29
29
|
register()
|
|
30
30
|
|
|
31
|
-
export * from './schemas/native-enum.ts'
|
|
32
|
-
export * from './schemas/union-enum.ts'
|
|
33
31
|
export * from './schemas/nullable.ts'
|
|
34
32
|
export {
|
|
35
33
|
BaseType,
|
package/src/schemas/nullable.ts
CHANGED
|
@@ -10,4 +10,23 @@ export type TNullable<T extends TSchema> = TUnion<[T, TNull]>
|
|
|
10
10
|
export const Nullable = <T extends TSchema>(
|
|
11
11
|
schema: T,
|
|
12
12
|
options: SchemaOptions = {},
|
|
13
|
-
) =>
|
|
13
|
+
) => {
|
|
14
|
+
const {
|
|
15
|
+
default: _default,
|
|
16
|
+
description,
|
|
17
|
+
examples,
|
|
18
|
+
readOnly,
|
|
19
|
+
title,
|
|
20
|
+
writeOnly,
|
|
21
|
+
} = schema
|
|
22
|
+
|
|
23
|
+
return Type.Union([schema, Type.Null()], {
|
|
24
|
+
default: _default,
|
|
25
|
+
description,
|
|
26
|
+
examples,
|
|
27
|
+
readOnly,
|
|
28
|
+
title,
|
|
29
|
+
writeOnly,
|
|
30
|
+
...options,
|
|
31
|
+
})
|
|
32
|
+
}
|
package/src/types/enum.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type { TNativeEnum } from '../schemas/native-enum.ts'
|
|
3
|
-
import { NativeEnum } from '../schemas/native-enum.ts'
|
|
4
|
-
import { type TUnionEnum, UnionEnum } from '../schemas/union-enum.ts'
|
|
1
|
+
import { Enum, type SchemaOptions, type TEnum } from '@sinclair/typebox'
|
|
5
2
|
import { BaseType } from './base.ts'
|
|
6
3
|
|
|
7
4
|
export type AnyObjectEnumType<T extends { [K in string]: K } = any> =
|
|
@@ -11,9 +8,9 @@ export class ObjectEnumType<
|
|
|
11
8
|
N extends boolean = false,
|
|
12
9
|
O extends boolean = false,
|
|
13
10
|
D extends boolean = false,
|
|
14
|
-
> extends BaseType<
|
|
11
|
+
> extends BaseType<TEnum<T>, N, O, D> {
|
|
15
12
|
constructor(
|
|
16
|
-
readonly values: T,
|
|
13
|
+
private readonly values: T,
|
|
17
14
|
options: SchemaOptions = {},
|
|
18
15
|
isNullable: N = false as N,
|
|
19
16
|
isOptional: O = false as O,
|
|
@@ -22,11 +19,8 @@ export class ObjectEnumType<
|
|
|
22
19
|
super(options, isNullable, isOptional, hasDefault, values)
|
|
23
20
|
}
|
|
24
21
|
|
|
25
|
-
protected _constructSchema(
|
|
26
|
-
|
|
27
|
-
values: T,
|
|
28
|
-
): TNativeEnum<T> {
|
|
29
|
-
return NativeEnum(values, options)
|
|
22
|
+
protected _constructSchema(options: SchemaOptions, values: T): TEnum<T> {
|
|
23
|
+
return Enum(values, options)
|
|
30
24
|
}
|
|
31
25
|
|
|
32
26
|
nullable() {
|
|
@@ -66,15 +60,15 @@ export class ObjectEnumType<
|
|
|
66
60
|
}
|
|
67
61
|
}
|
|
68
62
|
|
|
69
|
-
export type AnyEnumType = EnumType<any
|
|
63
|
+
export type AnyEnumType = EnumType<any, boolean, boolean, boolean>
|
|
70
64
|
export class EnumType<
|
|
71
65
|
T extends (string | number)[] = (string | number)[],
|
|
72
66
|
N extends boolean = false,
|
|
73
67
|
O extends boolean = false,
|
|
74
68
|
D extends boolean = false,
|
|
75
|
-
> extends BaseType<
|
|
69
|
+
> extends BaseType<TEnum<Record<string, T[number]>>, N, O, D> {
|
|
76
70
|
constructor(
|
|
77
|
-
readonly values: [...T],
|
|
71
|
+
private readonly values: [...T],
|
|
78
72
|
options: SchemaOptions = {},
|
|
79
73
|
isNullable: N = false as N,
|
|
80
74
|
isOptional: O = false as O,
|
|
@@ -86,8 +80,8 @@ export class EnumType<
|
|
|
86
80
|
protected _constructSchema(
|
|
87
81
|
options: SchemaOptions,
|
|
88
82
|
values: [...T],
|
|
89
|
-
):
|
|
90
|
-
return
|
|
83
|
+
): TEnum<Record<string, T[number]>> {
|
|
84
|
+
return Enum(Object.fromEntries(values.map((k) => [k, k])), options)
|
|
91
85
|
}
|
|
92
86
|
|
|
93
87
|
nullable() {
|
package/src/types/object.ts
CHANGED
|
@@ -2,18 +2,12 @@ import {
|
|
|
2
2
|
type ObjectOptions,
|
|
3
3
|
type TObject,
|
|
4
4
|
type TRecord,
|
|
5
|
-
type TSchema,
|
|
6
5
|
Type,
|
|
7
6
|
} from '@sinclair/typebox'
|
|
8
7
|
import type { typeStatic } from '../constants.ts'
|
|
9
8
|
import type { UnionToTupleString } from '../utils.ts'
|
|
10
9
|
import { BaseType, getTypeSchema } from './base.ts'
|
|
11
|
-
import {
|
|
12
|
-
type AnyEnumType,
|
|
13
|
-
type AnyObjectEnumType,
|
|
14
|
-
EnumType,
|
|
15
|
-
ObjectEnumType,
|
|
16
|
-
} from './enum.ts'
|
|
10
|
+
import { type AnyEnumType, type AnyObjectEnumType, EnumType } from './enum.ts'
|
|
17
11
|
import type { AnyLiteralType } from './literal.ts'
|
|
18
12
|
import type { AnyStringType } from './string.ts'
|
|
19
13
|
import type { AnyUnionType } from './union.ts'
|
|
@@ -176,20 +170,8 @@ export class RecordType<
|
|
|
176
170
|
}
|
|
177
171
|
|
|
178
172
|
protected _constructSchema(options: ObjectOptions, key: K, element: E) {
|
|
179
|
-
let keySchema: TSchema
|
|
180
|
-
|
|
181
|
-
if (key instanceof EnumType) {
|
|
182
|
-
keySchema = Type.Union(key.values.map((value) => Type.Literal(value)))
|
|
183
|
-
} else if (key instanceof ObjectEnumType) {
|
|
184
|
-
keySchema = Type.Union(
|
|
185
|
-
Object.values(key.values).map((value) => Type.Literal(value as string)),
|
|
186
|
-
)
|
|
187
|
-
} else {
|
|
188
|
-
keySchema = getTypeSchema(key)
|
|
189
|
-
}
|
|
190
|
-
|
|
191
173
|
return Type.Record(
|
|
192
|
-
|
|
174
|
+
getTypeSchema(key),
|
|
193
175
|
getTypeSchema(element),
|
|
194
176
|
options,
|
|
195
177
|
) as unknown as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Kind, TypeRegistry } from '@sinclair/typebox/type';
|
|
2
|
-
export const NativeEnumKind = 'NativeEnum';
|
|
3
|
-
export function NativeEnum(value, options = {}) {
|
|
4
|
-
const values = Object.values(value);
|
|
5
|
-
function NativeEnumCheck(schema, value) {
|
|
6
|
-
return typeof value === 'string' && schema.enum.includes(value);
|
|
7
|
-
}
|
|
8
|
-
if (!TypeRegistry.Has(NativeEnumKind)) TypeRegistry.Set(NativeEnumKind, NativeEnumCheck);
|
|
9
|
-
return {
|
|
10
|
-
...options,
|
|
11
|
-
[Kind]: NativeEnumKind,
|
|
12
|
-
enum: values
|
|
13
|
-
};
|
|
14
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/native-enum.ts"],"sourcesContent":["import {\n Kind,\n type SchemaOptions,\n type TSchema,\n TypeRegistry,\n} from '@sinclair/typebox/type'\n\nexport const NativeEnumKind = 'NativeEnum'\nexport interface TNativeEnum<T extends Record<string, string>> extends TSchema {\n [Kind]: typeof NativeEnumKind\n static: T[keyof T]\n enum: T[keyof T][]\n}\n\nexport function NativeEnum<T extends Record<string, string>>(\n value: T,\n options: SchemaOptions = {},\n) {\n const values = Object.values(value)\n\n function NativeEnumCheck(schema: TNativeEnum<T>, value: unknown) {\n return typeof value === 'string' && schema.enum.includes(value as any)\n }\n\n if (!TypeRegistry.Has(NativeEnumKind))\n TypeRegistry.Set(NativeEnumKind, NativeEnumCheck)\n\n return { ...options, [Kind]: NativeEnumKind, enum: values } as TNativeEnum<T>\n}\n"],"names":["Kind","TypeRegistry","NativeEnumKind","NativeEnum","value","options","values","Object","NativeEnumCheck","schema","enum","includes","Has","Set"],"mappings":"AAAA,SACEA,IAAI,EAGJC,YAAY,QACP,yBAAwB;AAE/B,OAAO,MAAMC,iBAAiB,aAAY;AAO1C,OAAO,SAASC,WACdC,KAAQ,EACRC,UAAyB,CAAC,CAAC;IAE3B,MAAMC,SAASC,OAAOD,MAAM,CAACF;IAE7B,SAASI,gBAAgBC,MAAsB,EAAEL,KAAc;QAC7D,OAAO,OAAOA,UAAU,YAAYK,OAAOC,IAAI,CAACC,QAAQ,CAACP;IAC3D;IAEA,IAAI,CAACH,aAAaW,GAAG,CAACV,iBACpBD,aAAaY,GAAG,CAACX,gBAAgBM;IAEnC,OAAO;QAAE,GAAGH,OAAO;QAAE,CAACL,KAAK,EAAEE;QAAgBQ,MAAMJ;IAAO;AAC5D"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Kind, TypeRegistry } from '@sinclair/typebox/type';
|
|
2
|
-
export const UnionEnumKind = 'UnionEnum';
|
|
3
|
-
export function UnionEnum(values, options = {}) {
|
|
4
|
-
function UnionEnumCheck(schema, value) {
|
|
5
|
-
return (typeof value === 'string' || typeof value === 'number') && schema.enum.includes(value);
|
|
6
|
-
}
|
|
7
|
-
if (!TypeRegistry.Has(UnionEnumKind)) TypeRegistry.Set(UnionEnumKind, UnionEnumCheck);
|
|
8
|
-
return {
|
|
9
|
-
...options,
|
|
10
|
-
[Kind]: UnionEnumKind,
|
|
11
|
-
enum: values
|
|
12
|
-
};
|
|
13
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/union-enum.ts"],"sourcesContent":["import {\n Kind,\n type SchemaOptions,\n type TSchema,\n TypeRegistry,\n} from '@sinclair/typebox/type'\n\nexport const UnionEnumKind = 'UnionEnum'\n\n// Ref: https://github.com/sinclairzx81/typebox/blob/master/example/prototypes/union-enum.ts\n\n// -------------------------------------------------------------------------------------\n// TUnionEnum\n// -------------------------------------------------------------------------------------\nexport interface TUnionEnum<T extends (string | number)[]> extends TSchema {\n [Kind]: typeof UnionEnumKind\n static: T[number]\n enum: T\n}\n\n// -------------------------------------------------------------------------------------\n// UnionEnum\n// -------------------------------------------------------------------------------------\n/** `[Experimental]` Creates a Union type with a `enum` schema representation */\nexport function UnionEnum<T extends (string | number)[]>(\n values: [...T],\n options: SchemaOptions = {},\n) {\n function UnionEnumCheck(\n schema: TUnionEnum<(string | number)[]>,\n value: unknown,\n ) {\n return (\n (typeof value === 'string' || typeof value === 'number') &&\n schema.enum.includes(value)\n )\n }\n\n if (!TypeRegistry.Has(UnionEnumKind))\n TypeRegistry.Set(UnionEnumKind, UnionEnumCheck)\n\n return { ...options, [Kind]: UnionEnumKind, enum: values } as TUnionEnum<T>\n}\n"],"names":["Kind","TypeRegistry","UnionEnumKind","UnionEnum","values","options","UnionEnumCheck","schema","value","enum","includes","Has","Set"],"mappings":"AAAA,SACEA,IAAI,EAGJC,YAAY,QACP,yBAAwB;AAE/B,OAAO,MAAMC,gBAAgB,YAAW;AAiBxC,OAAO,SAASC,UACdC,MAAc,EACdC,UAAyB,CAAC,CAAC;IAE3B,SAASC,eACPC,MAAuC,EACvCC,KAAc;QAEd,OACE,AAAC,CAAA,OAAOA,UAAU,YAAY,OAAOA,UAAU,QAAO,KACtDD,OAAOE,IAAI,CAACC,QAAQ,CAACF;IAEzB;IAEA,IAAI,CAACP,aAAaU,GAAG,CAACT,gBACpBD,aAAaW,GAAG,CAACV,eAAeI;IAElC,OAAO;QAAE,GAAGD,OAAO;QAAE,CAACL,KAAK,EAAEE;QAAeO,MAAML;IAAO;AAC3D"}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Kind,
|
|
3
|
-
type SchemaOptions,
|
|
4
|
-
type TSchema,
|
|
5
|
-
TypeRegistry,
|
|
6
|
-
} from '@sinclair/typebox/type'
|
|
7
|
-
|
|
8
|
-
export const NativeEnumKind = 'NativeEnum'
|
|
9
|
-
export interface TNativeEnum<T extends Record<string, string>> extends TSchema {
|
|
10
|
-
[Kind]: typeof NativeEnumKind
|
|
11
|
-
static: T[keyof T]
|
|
12
|
-
enum: T[keyof T][]
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function NativeEnum<T extends Record<string, string>>(
|
|
16
|
-
value: T,
|
|
17
|
-
options: SchemaOptions = {},
|
|
18
|
-
) {
|
|
19
|
-
const values = Object.values(value)
|
|
20
|
-
|
|
21
|
-
function NativeEnumCheck(schema: TNativeEnum<T>, value: unknown) {
|
|
22
|
-
return typeof value === 'string' && schema.enum.includes(value as any)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (!TypeRegistry.Has(NativeEnumKind))
|
|
26
|
-
TypeRegistry.Set(NativeEnumKind, NativeEnumCheck)
|
|
27
|
-
|
|
28
|
-
return { ...options, [Kind]: NativeEnumKind, enum: values } as TNativeEnum<T>
|
|
29
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Kind,
|
|
3
|
-
type SchemaOptions,
|
|
4
|
-
type TSchema,
|
|
5
|
-
TypeRegistry,
|
|
6
|
-
} from '@sinclair/typebox/type'
|
|
7
|
-
|
|
8
|
-
export const UnionEnumKind = 'UnionEnum'
|
|
9
|
-
|
|
10
|
-
// Ref: https://github.com/sinclairzx81/typebox/blob/master/example/prototypes/union-enum.ts
|
|
11
|
-
|
|
12
|
-
// -------------------------------------------------------------------------------------
|
|
13
|
-
// TUnionEnum
|
|
14
|
-
// -------------------------------------------------------------------------------------
|
|
15
|
-
export interface TUnionEnum<T extends (string | number)[]> extends TSchema {
|
|
16
|
-
[Kind]: typeof UnionEnumKind
|
|
17
|
-
static: T[number]
|
|
18
|
-
enum: T
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// -------------------------------------------------------------------------------------
|
|
22
|
-
// UnionEnum
|
|
23
|
-
// -------------------------------------------------------------------------------------
|
|
24
|
-
/** `[Experimental]` Creates a Union type with a `enum` schema representation */
|
|
25
|
-
export function UnionEnum<T extends (string | number)[]>(
|
|
26
|
-
values: [...T],
|
|
27
|
-
options: SchemaOptions = {},
|
|
28
|
-
) {
|
|
29
|
-
function UnionEnumCheck(
|
|
30
|
-
schema: TUnionEnum<(string | number)[]>,
|
|
31
|
-
value: unknown,
|
|
32
|
-
) {
|
|
33
|
-
return (
|
|
34
|
-
(typeof value === 'string' || typeof value === 'number') &&
|
|
35
|
-
schema.enum.includes(value)
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (!TypeRegistry.Has(UnionEnumKind))
|
|
40
|
-
TypeRegistry.Set(UnionEnumKind, UnionEnumCheck)
|
|
41
|
-
|
|
42
|
-
return { ...options, [Kind]: UnionEnumKind, enum: values } as TUnionEnum<T>
|
|
43
|
-
}
|