Revision 0.8.2

This commit is contained in:
sinclair
2025-01-09 03:40:44 +09:00
parent c4ff05bd55
commit 81275b2a51
4 changed files with 32 additions and 3 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@sinclair/typebox-adapter",
"version": "0.8.1",
"version": "0.8.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@sinclair/typebox-adapter",
"version": "0.8.1",
"version": "0.8.2",
"license": "MIT",
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.2",

View File

@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox-adapter",
"version": "0.8.1",
"version": "0.8.2",
"description": "Integrate Valibot and Zod with TypeBox",
"author": "sinclairzx81",
"license": "MIT",

View File

@@ -108,6 +108,19 @@ function FromDefault<Def extends z.ZodDefaultDef>(def: Def): tb.TSchema {
return tb.CloneType(FromType(def.innerType), { default: def.defaultValue() })
}
// ------------------------------------------------------------------
// DiscriminatedUnion
// ------------------------------------------------------------------
// prettier-ignore
type TFromDiscriminatedUnion<Discriminator extends string, Types extends readonly z.ZodObject<any>[], Result extends tb.TSchema[] = []> = (
Types extends [infer Left extends z.ZodObject<any>, ...infer Right extends z.ZodObject<any>[]]
? TFromDiscriminatedUnion<Discriminator, Right, [...Result, TFromType<Left>]>
: tb.TUnion<Result>
)
function FromDiscriminatedUnion<Def extends z.ZodDiscriminatedUnionDef<string, z.ZodDiscriminatedUnionOption<string>[]>>(def: Def): tb.TSchema {
const types = def.options.map((type) => FromType(type))
return tb.Union(types, { discriminator: def.discriminator })
}
// ------------------------------------------------------------------
// Effects
// ------------------------------------------------------------------
type TFromEffects<Input extends z.ZodTypeAny, Output extends unknown> = tb.Ensure<tb.TTransform<TFromType<Input>, Output>>
@@ -322,6 +335,7 @@ type TFromType<Type extends z.ZodType> = (
Type extends z.ZodBoolean ? TFromBoolean :
Type extends z.ZodDate ? TFromDate :
Type extends z.ZodDefault<infer Type> ? TFromDefault<Type> :
Type extends z.ZodDiscriminatedUnion<infer Discriminator, infer Types> ? TFromDiscriminatedUnion<Discriminator, Types> :
Type extends z.ZodEffects<infer Input, infer Output> ? TFromEffects<Input, Output> :
Type extends z.ZodLiteral<infer Value> ? TFromLiteral<Value> :
Type extends z.ZodNullable<infer Type> ? TFromNullable<Type> :
@@ -353,6 +367,7 @@ function FromType<Type extends z.ZodType>(type: Type): tb.TSchema {
type instanceof z.ZodBoolean ? FromBoolean(type._def) :
type instanceof z.ZodDate ? FromDate(type._def) :
type instanceof z.ZodDefault ? FromDefault(type._def) :
type instanceof z.ZodDiscriminatedUnion ? FromDiscriminatedUnion(type._def) :
type instanceof z.ZodEffects ? FromEffects(type) :
type instanceof z.ZodLiteral ? FromLiteral(type._def) :
type instanceof z.ZodNullable ? FromNullable(type._def) :

View File

@@ -46,6 +46,20 @@ describe('Zod', () => {
Assert.IsTrue(TypeGuard.IsDate(T))
})
// ----------------------------------------------------------------
// DiscriminatedUnion
// ----------------------------------------------------------------
it('Should map DiscriminatedUnion', () => {
const A = z.object({ type: z.literal('A') })
const B = z.object({ type: z.literal('B') })
const C = z.object({ type: z.literal('C') })
const T = Box(z.discriminatedUnion('type', [A, B, C]))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsEqual(T.discriminator, 'type')
Assert.IsTrue(T.anyOf[0].properties.type.const === 'A')
Assert.IsTrue(T.anyOf[1].properties.type.const === 'B')
Assert.IsTrue(T.anyOf[2].properties.type.const === 'C')
})
// ----------------------------------------------------------------
// Effects
// ----------------------------------------------------------------
it('Should map Effects (Transform)', () => {