@nmtjs/type
Advanced tools
Comparing version 0.3.10 to 0.3.11
@@ -30,3 +30,5 @@ import { TypeCompiler } from '@sinclair/typebox/compiler'; | ||
...compiled, | ||
decode: (val)=>{ | ||
decode: (val)=>compiled.decode(compiled.prepare(val)), | ||
encode: (val)=>compiled.encode(compiled.prepare(val)), | ||
decodeSafe: (val)=>{ | ||
try { | ||
@@ -44,3 +46,3 @@ return { | ||
}, | ||
encode: (val)=>{ | ||
encodeSafe: (val)=>{ | ||
try { | ||
@@ -47,0 +49,0 @@ return { |
import { Type } from '@sinclair/typebox'; | ||
import { BaseType, getTypeSchema } from "./base.js"; | ||
import { EnumType } from "./enum.js"; | ||
import { EnumType, ObjectEnumType } from "./enum.js"; | ||
export class ObjectType extends BaseType { | ||
@@ -100,3 +100,11 @@ properties; | ||
_constructSchema(options, key, element) { | ||
return Type.Record(getTypeSchema(key), getTypeSchema(element), options); | ||
let keySchema; | ||
if (key instanceof EnumType) { | ||
keySchema = Type.Union(key.values.map((value)=>Type.Literal(value))); | ||
} else if (key instanceof ObjectEnumType) { | ||
keySchema = Type.Union(Object.values(key.values).map((value)=>Type.Literal(value))); | ||
} else { | ||
keySchema = getTypeSchema(key); | ||
} | ||
return Type.Record(keySchema, getTypeSchema(element), options); | ||
} | ||
@@ -103,0 +111,0 @@ nullable() { |
@@ -24,3 +24,3 @@ { | ||
"temporal-polyfill": "^0.2.5", | ||
"@nmtjs/common": "0.3.10" | ||
"@nmtjs/common": "0.3.11" | ||
}, | ||
@@ -30,3 +30,3 @@ "devDependencies": { | ||
"temporal-polyfill": "^0.2.5", | ||
"@nmtjs/common": "0.3.10" | ||
"@nmtjs/common": "0.3.11" | ||
}, | ||
@@ -40,3 +40,3 @@ "files": [ | ||
], | ||
"version": "0.3.10", | ||
"version": "0.3.11", | ||
"scripts": { | ||
@@ -43,0 +43,0 @@ "build": "neemata-build -p neutral --root=./src './**/*.ts'", |
@@ -13,6 +13,8 @@ import { | ||
convert: (val: unknown) => unknown | ||
decode: ( | ||
decode: (val: unknown) => unknown | ||
encode: (val: unknown) => unknown | ||
decodeSafe: ( | ||
val: unknown, | ||
) => { success: true; value: unknown } | { success: false; error: any } | ||
encode: ( | ||
encodeSafe: ( | ||
val: unknown, | ||
@@ -48,3 +50,5 @@ ) => { success: true; value: unknown } | { success: false; error: any } | ||
...compiled, | ||
decode: (val) => { | ||
decode: (val) => compiled.decode(compiled.prepare(val)), | ||
encode: (val) => compiled.encode(compiled.prepare(val)), | ||
decodeSafe: (val) => { | ||
try { | ||
@@ -59,3 +63,3 @@ return { | ||
}, | ||
encode: (val) => { | ||
encodeSafe: (val) => { | ||
try { | ||
@@ -62,0 +66,0 @@ return { |
@@ -16,3 +16,3 @@ import type { SchemaOptions } from '@sinclair/typebox' | ||
constructor( | ||
protected readonly values: T, | ||
readonly values: T, | ||
options: SchemaOptions = {}, | ||
@@ -78,3 +78,3 @@ isNullable: N = false as N, | ||
constructor( | ||
protected readonly values: [...T], | ||
readonly values: [...T], | ||
options: SchemaOptions = {}, | ||
@@ -81,0 +81,0 @@ isNullable: N = false as N, |
@@ -5,2 +5,3 @@ import { | ||
type TRecord, | ||
type TSchema, | ||
Type, | ||
@@ -11,3 +12,8 @@ } from '@sinclair/typebox' | ||
import { BaseType, getTypeSchema } from './base.ts' | ||
import { type AnyEnumType, type AnyObjectEnumType, EnumType } from './enum.ts' | ||
import { | ||
type AnyEnumType, | ||
type AnyObjectEnumType, | ||
EnumType, | ||
ObjectEnumType, | ||
} from './enum.ts' | ||
import type { AnyLiteralType } from './literal.ts' | ||
@@ -175,7 +181,19 @@ import type { AnyStringType } from './string.ts' | ||
protected _constructSchema(options: ObjectOptions, key: K, element: E) { | ||
let keySchema: TSchema | ||
if (key instanceof EnumType) { | ||
keySchema = Type.Union(key.values.map((value) => Type.Literal(value))) | ||
} else if (key instanceof ObjectEnumType) { | ||
keySchema = Type.Union( | ||
Object.values(key.values).map((value) => Type.Literal(value as string)), | ||
) | ||
} else { | ||
keySchema = getTypeSchema(key) | ||
} | ||
return Type.Record( | ||
getTypeSchema(key), | ||
keySchema, | ||
getTypeSchema(element), | ||
options, | ||
) as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']> | ||
) as unknown as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']> | ||
} | ||
@@ -182,0 +200,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234492
3871