Revision 0.8.0 (#9)

- Rename to TypeMap
This commit is contained in:
sinclairzx81
2025-01-26 03:27:38 +09:00
committed by GitHub
parent d24876b2ac
commit 5ce19b2f4f
52 changed files with 3769 additions and 1228 deletions

View File

@@ -1,403 +0,0 @@
/*--------------------------------------------------------------------------
@sinclair/typebox-adapter
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
import * as tb from '@sinclair/typebox'
import * as z from 'zod'
// ------------------------------------------------------------------
// Options
// ------------------------------------------------------------------
function Options(type: z.ZodTypeAny): tb.SchemaOptions {
const description = tb.ValueGuard.IsUndefined(type.description) ? {} : { description: type.description }
return { ...description }
}
// ------------------------------------------------------------------
// Formats
// ------------------------------------------------------------------
const check = (type: z.ZodTypeAny, value: unknown) => type.safeParse(value).success
tb.FormatRegistry.Set('zod:base64', (value) => check(z.string().base64(), value))
tb.FormatRegistry.Set('zod:base64url', (value) => check(z.string().base64url(), value))
tb.FormatRegistry.Set('zod:cidrv4', (value) => check(z.string().cidr({ version: 'v4' }), value))
tb.FormatRegistry.Set('zod:cidrv6', (value) => check(z.string().cidr({ version: 'v6' }), value))
tb.FormatRegistry.Set('zod:cidr', (value) => check(z.string().cidr(), value))
tb.FormatRegistry.Set('zod:cuid', (value) => check(z.string().cuid(), value))
tb.FormatRegistry.Set('zod:cuid2', (value) => check(z.string().cuid2(), value))
tb.FormatRegistry.Set('zod:ulid', (value) => check(z.string().ulid(), value))
tb.FormatRegistry.Set('zod:email', (value) => check(z.string().email(), value))
tb.FormatRegistry.Set('zod:emoji', (value) => check(z.string().emoji(), value))
tb.FormatRegistry.Set('zod:ipv4', (value) => check(z.string().ip({ version: 'v4' }), value))
tb.FormatRegistry.Set('zod:ipv6', (value) => check(z.string().ip({ version: 'v6' }), value))
tb.FormatRegistry.Set('zod:ip', (value) => check(z.string().ip(), value))
tb.FormatRegistry.Set('zod:ipv6Cidr', (value) => check(z.string().cidr({ version: 'v6' }), value))
tb.FormatRegistry.Set('zod:nanoid', (value) => check(z.string().nanoid(), value))
tb.FormatRegistry.Set('zod:jwt', (value) => check(z.string().jwt(), value))
tb.FormatRegistry.Set('zod:date', (value) => check(z.string().date(), value))
tb.FormatRegistry.Set('zod:datetime', (value) => check(z.string().datetime(), value))
tb.FormatRegistry.Set('zod:duration', (value) => check(z.string().duration(), value))
tb.FormatRegistry.Set('zod:time', (value) => check(z.string().time(), value))
tb.FormatRegistry.Set('zod:url', (value) => check(z.string().url(), value))
tb.FormatRegistry.Set('zod:uuid', (value) => check(z.string().uuid(), value))
// ------------------------------------------------------------------
// Any
// ------------------------------------------------------------------
type TFromAny = tb.TAny
function FromAny<Def extends z.ZodAnyDef>(_def: Def) {
return tb.Any()
}
// ------------------------------------------------------------------
// Array
// ------------------------------------------------------------------
type TFromArray<Type extends z.ZodTypeAny> = tb.Ensure<tb.TArray<TFromType<Type>>>
function FromArray<Def extends z.ZodArrayDef>(def: Def): tb.TSchema {
const minItems = def.minLength === null ? {} : { minItems: def.minLength.value }
const maxItems = def.maxLength === null ? {} : { minItems: def.maxLength.value }
const options = { ...minItems, ...maxItems }
return tb.Array(FromType(def.type), options)
}
// ------------------------------------------------------------------
// BigInt
// ------------------------------------------------------------------
type TFromBigInt = tb.TBigInt
function FromBigInt<Def extends z.ZodBigIntDef>(def: Def) {
return tb.BigInt()
}
// ------------------------------------------------------------------
// Boolean
// ------------------------------------------------------------------
type TFromBoolean = tb.TBoolean
function FromBoolean<Def extends z.ZodBooleanDef>(def: Def) {
return tb.Boolean()
}
// ------------------------------------------------------------------
// Date
// ------------------------------------------------------------------
type TFromDate = tb.TDate
function FromDate<Def extends z.ZodDateDef>(def: Def) {
return tb.Date()
}
// ------------------------------------------------------------------
// Default
// ------------------------------------------------------------------
type TFromDefault<Type extends z.ZodType> = TFromType<Type>
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>>
function FromEffects<Type extends z.ZodEffects<z.ZodTypeAny, unknown>>(type: Type): tb.TSchema {
return tb
.Transform(FromType(type._def.schema))
.Decode((value) => type.parse(value))
.Encode((_) => {
throw Error('Encode not implemented for Zod types')
})
}
// ------------------------------------------------------------------
// Literal
// ------------------------------------------------------------------
type TFromLiteral<Value extends unknown> = tb.Ensure<Value extends tb.TLiteralValue ? tb.TLiteral<Value> : tb.TNever>
function FromLiteral<Def extends z.ZodLiteralDef>(def: Def) {
return tb.Literal(def.value as tb.TLiteralValue)
}
// ------------------------------------------------------------------
// Intersect
// ------------------------------------------------------------------
// prettier-ignore
type TFromIntersect<Types extends z.ZodTypeAny[], Result extends tb.TSchema[] = []> = (
Types extends [infer Left extends z.ZodTypeAny, ...infer Right extends z.ZodTypeAny[]]
? TFromIntersect<Right, [...Result, TFromType<Left>]>
: tb.Ensure<tb.TIntersect<Result>>
)
function FromIntersect<Type extends z.ZodIntersectionDef>(type: Type): tb.TSchema {
return tb.Intersect([FromType(type.left), FromType(type.right)])
}
// ------------------------------------------------------------------
// Object
// ------------------------------------------------------------------
type TFromObject<Properties extends z.ZodRawShape> = tb.Ensure<
tb.TObject<{
[Key in keyof Properties]: TFromType<Properties[Key]>
}>
>
function FromObject<Def extends z.ZodObjectDef<z.ZodRawShape>, Shape extends z.ZodRawShape>(def: Def, shape: Shape): tb.TSchema {
const additionalProperties = def.unknownKeys === 'strict' ? { additionalProperties: false } : {}
const options = { ...additionalProperties }
return tb.Object(
globalThis.Object.keys(shape).reduce((properties: any, key: any) => {
return { ...properties, [key]: FromType(shape[key]) }
}, {} as tb.TProperties) as never,
options,
)
}
// ------------------------------------------------------------------
// Optional
// ------------------------------------------------------------------
type TFromOptional<Type extends z.ZodTypeAny, Result extends tb.TSchema = tb.TOptional<TFromType<Type>>> = Result
function FromOptional<Def extends z.ZodOptionalDef>(def: Def): tb.TSchema {
return tb.Optional(FromType(def.innerType))
}
// ------------------------------------------------------------------
// Promise
// ------------------------------------------------------------------
type TFromPromise<Type extends z.ZodTypeAny> = tb.Ensure<tb.TPromise<TFromType<Type>>>
function FromPromise<Def extends z.ZodPromiseDef>(def: Def): tb.TSchema {
return tb.Promise(FromType(def.type))
}
// ------------------------------------------------------------------
// Nullable
// ------------------------------------------------------------------
type TFromNullable<Type extends z.ZodTypeAny> = tb.Ensure<tb.TUnion<[tb.TNull, TFromType<Type>]>>
function FromNullable<Def extends z.ZodNullableDef>(def: Def): tb.TSchema {
return tb.Union([tb.Null(), FromType(def.innerType)])
}
// ------------------------------------------------------------------
// Number
// ------------------------------------------------------------------
type TFromNumber = tb.TNumber
// prettier-ignore
function FromNumber<Def extends z.ZodNumberDef>(def: Def) {
const options = def.checks.reduce((options, check) => {
return { ...options, ... (
check.kind === 'int' ? { multipleOf: 1 } :
check.kind === 'max' ? check.inclusive ? { maximum: check.value } : { exclusiveMaximum: check.value } :
check.kind === 'min' ? check.inclusive ? { minimum: check.value } : { exclusiveMinimum: check.value } :
check.kind === 'multipleOf' ? { multipleOf: check.value } :
{}
)}
}, {})
return tb.Number(options)
}
// ------------------------------------------------------------------
// Never
// ------------------------------------------------------------------
type TFromNever = tb.TNever
function FromNever<Def extends z.ZodNeverDef>(def: Def) {
return tb.Never()
}
// ------------------------------------------------------------------
// Null
// ------------------------------------------------------------------
type TFromNull = tb.TNull
function FromNull<Def extends z.ZodNullDef>(def: Def) {
return tb.Null()
}
// ------------------------------------------------------------------
// Readonly
// ------------------------------------------------------------------
type TFromReadonly<Type extends z.ZodTypeAny, Result extends tb.TSchema = tb.TReadonly<TFromType<Type>>> = Result
function FromReadonly<Def extends z.ZodReadonlyDef>(def: Def): tb.TSchema {
return tb.Readonly(FromType(def.innerType))
}
// ------------------------------------------------------------------
// Record
// ------------------------------------------------------------------
type TFromRecord<Key extends z.ZodTypeAny, Value extends z.ZodTypeAny> = tb.Ensure<tb.TRecordOrObject<TFromType<Key>, TFromType<Value>>>
function FromRecord<Def extends z.ZodRecordDef>(def: Def): tb.TSchema {
return tb.Record(FromType(def.keyType), FromType(def.valueType))
}
// ------------------------------------------------------------------
// String
// ------------------------------------------------------------------
type TFromString = tb.TString
// prettier-ignore
function FromString<Def extends z.ZodStringDef>(def: Def) {
const options = def.checks.reduce((options, check) => {
return { ...options, ...(
check.kind === 'base64' ? { format: 'zod:base64' } :
check.kind === 'base64url' ? { format: 'zod:base64url' } :
check.kind === 'cidr' ? { format: check.version === 'v4' ? 'zod:cidrv4' : check.version === 'v6' ? 'zod:cidrv6' : 'zod:cidr' } :
check.kind === 'cuid' ? { format: 'zod:cuid' } :
check.kind === 'cuid2' ? { format: 'zod:cuid2' } :
check.kind === 'date' ? { format: 'zod:date' } :
check.kind === 'datetime' ? { format: 'zod:datetime' } :
check.kind === 'duration' ? { format: 'zod:duration' } :
check.kind === 'email' ? { format: 'zod:email' } :
check.kind === 'emoji' ? { format: 'zod:emoji' } :
check.kind === 'endsWith' ? { pattern: `${check.value}$` } :
check.kind === 'includes' ? { pattern: check.value } :
check.kind === 'ip' ? { format: check.version === 'v4' ? 'zod:ipv4' : check.version === 'v6' ? 'zod:ipv6' : 'zod:ip' } :
check.kind === 'jwt' ? { format: 'zod:jwt' } :
check.kind === 'length' ? { minLength: check.value, maxLength: check.value } :
check.kind === 'min' ? { minLength: check.value } :
check.kind === 'max' ? { maxLength: check.value } :
check.kind === 'nanoid' ? { format: 'zod:nanoid' } :
check.kind === 'regex' ? { pattern: check.regex.source } :
check.kind === 'startsWith' ? { pattern: `^${check.value}` } :
check.kind === 'time' ? { format: 'zod:time' } :
check.kind === 'ulid' ? { format: 'zod:ulid' } :
check.kind === 'url' ? { format: 'zod:url' } :
check.kind === 'uuid' ? { format: 'zod:uuid' } :
{}
)}
}, {})
return tb.String(options)
}
// ------------------------------------------------------------------
// Symbol
// ------------------------------------------------------------------
type TFromSymbol = tb.TSymbol
function FromSymbol<Def extends z.ZodSymbolDef>(def: Def) {
return tb.Symbol()
}
// ------------------------------------------------------------------
// Tuple
// ------------------------------------------------------------------
// prettier-ignore
type TFromTuple<Types extends z.ZodTypeAny[], Result extends tb.TSchema[] = []> = (
Types extends [infer Left extends z.ZodTypeAny, ...infer Right extends z.ZodTypeAny[]]
? TFromTuple<Right, [...Result, TFromType<Left>]>
: tb.TTuple<Result>
)
function FromTuple<Def extends z.ZodTupleDef>(def: Def): tb.TSchema {
return tb.Tuple(def.items.map((item) => FromType(item)))
}
// ------------------------------------------------------------------
// Undefined
// ------------------------------------------------------------------
type TFromUndefined = tb.TUndefined
function FromUndefined<Def extends z.ZodUndefinedDef>(def: Def) {
return tb.Undefined()
}
// ------------------------------------------------------------------
// Union
// ------------------------------------------------------------------
// prettier-ignore
type TFromUnion<Types extends z.ZodTypeAny[], Result extends tb.TSchema[] = []> = (
Types extends [infer Left extends z.ZodTypeAny, ...infer Right extends z.ZodTypeAny[]]
? TFromUnion<Right, [...Result, TFromType<Left>]>
: tb.TUnion<Result>
)
function FromUnion<Def extends z.ZodUnionDef>(def: Def): tb.TSchema {
return tb.Union(def.options.map((item) => FromType(item)))
}
// ------------------------------------------------------------------
// Unknown
// ------------------------------------------------------------------
type TFromUnknown = tb.TUnknown
function FromUnknown<Def extends z.ZodUnknownDef>(def: Def) {
return tb.Unknown()
}
// ------------------------------------------------------------------
// Void
// ------------------------------------------------------------------
type TFromVoid = tb.TVoid
function FromVoid<Def extends z.ZodVoidDef>(def: Def) {
return tb.Void()
}
// ------------------------------------------------------------------
// Type
// ------------------------------------------------------------------
// prettier-ignore
type TFromType<Type extends z.ZodType> = (
Type extends z.ZodAny ? TFromAny :
Type extends z.ZodArray<infer Type> ? TFromArray<Type> :
Type extends z.ZodBigInt ? TFromBigInt :
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> :
Type extends z.ZodObject<infer Properties> ? TFromObject<Properties> :
Type extends z.ZodOptional<infer Type> ? TFromOptional<Type> :
Type extends z.ZodPromise<infer Type> ? TFromPromise<Type> :
Type extends z.ZodRecord<infer Key, infer Value> ? TFromRecord<Key, Value> :
Type extends z.ZodReadonly<infer Type> ? TFromReadonly<Type> :
Type extends z.ZodNumber ? TFromNumber :
Type extends z.ZodNever ? TFromNever :
Type extends z.ZodNull ? TFromNull :
Type extends z.ZodString ? TFromString :
Type extends z.ZodSymbol ? TFromSymbol :
Type extends z.ZodTuple<infer Types> ? TFromTuple<tb.Assert<Types, z.ZodTypeAny[]>> :
Type extends z.ZodUndefined ? TFromUndefined :
Type extends z.ZodUnion<infer Types> ? TFromUnion<tb.Assert<Types, z.ZodTypeAny[]>> :
Type extends z.ZodUnknown ? TFromUnknown :
Type extends z.ZodVoid ? TFromVoid :
// Intersection (Ensure Last Due to Zod Differentiation Issue)
Type extends z.ZodIntersection<infer Left, infer Right> ? TFromIntersect<[Left, Right]> :
tb.TNever
)
// prettier-ignore
function FromType<Type extends z.ZodType>(type: Type): tb.TSchema {
const schema = (
type instanceof z.ZodAny ? FromAny(type._def) :
type instanceof z.ZodArray ? FromArray(type._def) :
type instanceof z.ZodBigInt ? FromBigInt(type._def) :
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) :
type instanceof z.ZodObject ? FromObject(type._def, type.shape) :
type instanceof z.ZodOptional ? FromOptional(type._def) :
type instanceof z.ZodPromise ? FromPromise(type._def) :
type instanceof z.ZodReadonly ? FromReadonly(type._def) :
type instanceof z.ZodRecord ? FromRecord(type._def) :
type instanceof z.ZodNever ? FromNever(type._def) :
type instanceof z.ZodNull ? FromNull(type._def) :
type instanceof z.ZodNumber ? FromNumber(type._def) :
type instanceof z.ZodString ? FromString(type._def) :
type instanceof z.ZodSymbol ? FromSymbol(type._def) :
type instanceof z.ZodTuple ? FromTuple(type._def) :
type instanceof z.ZodUndefined ? FromUndefined(type._def) :
type instanceof z.ZodUnion ? FromUnion(type._def) :
type instanceof z.ZodUnknown ? FromUnknown(type._def) :
type instanceof z.ZodVoid ? FromVoid(type._def) :
// Intersection (Ensure Last Due to Zod Differentiation Issue)
type instanceof z.ZodIntersection ? FromIntersect(type._def) :
tb.Never()
) as tb.TSchema
return tb.CreateType(schema, Options(type)) as tb.TSchema
}
// ------------------------------------------------------------------
// Box
// ------------------------------------------------------------------
/** Converts a Zod Type to a TypeBox Type */
export type TBox<Type extends unknown> = Type extends z.ZodType ? TFromType<Type> : undefined
/** Converts a Zod Type to a TypeBox Type */
export function Box<Type extends unknown, Result extends TBox<Type> = TBox<Type>>(type: Type): Result {
return (type instanceof z.ZodType ? FromType(type) : undefined) as never
}

View File

@@ -0,0 +1,45 @@
/*--------------------------------------------------------------------------
@sinclair/typemap
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
import { TTypeBoxFromSyntax, TypeBoxFromSyntax } from '../typebox/typebox-from-syntax'
import { ZodFromTypeBox, TZodFromTypeBox } from './zod-from-typebox'
import * as t from '@sinclair/typebox'
import * as z from 'zod'
// prettier-ignore
export type TZodFromSyntax<Type extends object | string,
Schema extends t.TSchema = TTypeBoxFromSyntax<Type>,
Result extends z.ZodTypeAny | z.ZodEffects<any> = TZodFromTypeBox<Schema>
> = Result
// prettier-ignore
export function ZodFromSyntax<Type extends string>(type: Type): TZodFromSyntax<Type> {
const schema = TypeBoxFromSyntax(type)
const result = ZodFromTypeBox(schema)
return result
}

389
src/zod/zod-from-typebox.ts Normal file
View File

@@ -0,0 +1,389 @@
/*--------------------------------------------------------------------------
@sinclair/typemap
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
import * as t from '@sinclair/typebox'
import * as z from 'zod'
// ------------------------------------------------------------------
// Constraint
// ------------------------------------------------------------------
type TConstraint<Input extends z.ZodTypeAny = z.ZodTypeAny, Output extends z.ZodTypeAny = Input> = (input: Input) => Output
// ------------------------------------------------------------------
// Any
// ------------------------------------------------------------------
type TFromAny<Result = z.ZodAny> = Result
function FromAny(_type: t.TAny): z.ZodTypeAny {
return z.any()
}
// ------------------------------------------------------------------
// Array
// ------------------------------------------------------------------
type TFromArray<Type extends t.TSchema, Result = z.ZodArray<TFromType<Type>>> = Result
function FromArray(type: t.TArray): z.ZodTypeAny {
const constraints: TConstraint<z.ZodArray<z.ZodTypeAny, any>>[] = []
const { minItems, maxItems /* minContains, maxContains, contains */ } = type
if (t.ValueGuard.IsNumber(minItems)) constraints.push((input) => input.min(minItems))
if (t.ValueGuard.IsNumber(maxItems)) constraints.push((input) => input.max(maxItems))
const mapped = z.array(FromType(type.items))
return constraints.reduce((type, constraint) => constraint(type), mapped)
}
// ------------------------------------------------------------------
// BigInt
// ------------------------------------------------------------------
type TFromBigInt<Result = z.ZodBigInt> = Result
function FromBigInt(_type: t.TBigInt): z.ZodTypeAny {
return z.bigint()
}
// ------------------------------------------------------------------
// Boolean
// ------------------------------------------------------------------
type TFromBoolean<Result = z.ZodBoolean> = Result
function FromBoolean(_type: t.TBoolean): z.ZodTypeAny {
return z.boolean()
}
// ------------------------------------------------------------------
// Date
// ------------------------------------------------------------------
type TFromDate<Result = z.ZodDate> = Result
function FromDate(_type: t.TDate): z.ZodTypeAny {
return z.date()
}
// ------------------------------------------------------------------
// Function
// ------------------------------------------------------------------
// prettier-ignore
type TFromFunction<Parameters extends t.TSchema[], ReturnType extends t.TSchema,
MappedParameters extends z.ZodTypeAny[] = TFromTypes<Parameters>
> = (
MappedParameters extends [z.ZodTypeAny, ...z.ZodTypeAny[]] | []
? z.ZodFunction<z.ZodTuple<MappedParameters>, TFromType<ReturnType>>
: z.ZodNever
)
function FromFunction(type: t.TFunction): z.ZodTypeAny {
const mappedParameters = FromTypes(type.parameters) as [] | [z.ZodTypeAny, ...z.ZodTypeAny[]]
return z.function(z.tuple(mappedParameters), FromType(type.returns))
}
// ------------------------------------------------------------------
// Integer
// ------------------------------------------------------------------
type TFromInteger<Result = z.ZodNumber> = Result
function FromInteger(type: t.TInteger): z.ZodTypeAny {
const { exclusiveMaximum, exclusiveMinimum, minimum, maximum, multipleOf } = type
const constraints: TConstraint<z.ZodNumber>[] = [(value) => value.int()]
if (t.ValueGuard.IsNumber(exclusiveMinimum)) constraints.push((input) => input.min(exclusiveMinimum + 1))
if (t.ValueGuard.IsNumber(exclusiveMaximum)) constraints.push((input) => input.min(exclusiveMaximum - 1))
if (t.ValueGuard.IsNumber(maximum)) constraints.push((input) => input.max(maximum))
if (t.ValueGuard.IsNumber(minimum)) constraints.push((input) => input.min(minimum))
if (t.ValueGuard.IsNumber(multipleOf)) constraints.push((input) => input.multipleOf(multipleOf))
return constraints.reduce((input, constraint) => constraint(input), z.number())
}
// ------------------------------------------------------------------
// Intersect
// ------------------------------------------------------------------
type TFromIntersect<Types extends t.TSchema[], Result extends z.ZodTypeAny = z.ZodUnknown> = Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]]
? TFromIntersect<Right, z.ZodIntersection<TFromType<Left>, Result>>
: Result
function FromIntersect(type: t.TIntersect): z.ZodTypeAny {
return type.allOf.reduce((result, left) => {
return z.intersection(FromType(left), result) as never
}, z.unknown()) as never
}
// ------------------------------------------------------------------
// Literal
// ------------------------------------------------------------------
type TFromLiteral<Value extends t.TLiteralValue, Result = z.ZodLiteral<Value>> = Result
function FromLiteral(type: t.TLiteral): z.ZodTypeAny {
return z.literal(type.const)
}
// ------------------------------------------------------------------
// Object
// ------------------------------------------------------------------
type TFromObject<
Properties extends t.TProperties,
Result = z.ZodObject<{
[Key in keyof Properties]: TFromType<Properties[Key]>
}>,
> = Result
// prettier-ignore
function FromObject(type: t.TObject): z.ZodTypeAny {
const constraints: TConstraint<z.ZodObject<any, any, any>>[] = []
const { additionalProperties } = type
if (additionalProperties === false) constraints.push((input) => input.strict())
if (t.KindGuard.IsSchema(additionalProperties)) constraints.push((input) => input.catchall(FromType(additionalProperties)))
const properties = globalThis.Object.getOwnPropertyNames(type.properties).reduce((result, key) => ({ ...result, [key]: FromType(type.properties[key]) }), {})
return constraints.reduce((type, constraint) => constraint(type), z.object(properties))
}
// ------------------------------------------------------------------
// Promise
// ------------------------------------------------------------------
type TFromPromise<Type extends t.TSchema, Result = z.ZodPromise<TFromType<Type>>> = Result
function FromPromise(type: t.TPromise): z.ZodTypeAny {
return z.promise(FromType(type.item))
}
// ------------------------------------------------------------------
// Record
// ------------------------------------------------------------------
type TFromRegExp<Result = z.ZodString> = Result
function FromRegExp(type: t.TRegExp): z.ZodTypeAny {
const constraints: TConstraint<z.ZodString>[] = [(input) => input.regex(new RegExp(type.source), type.flags)]
const { minLength, maxLength } = type
if (t.ValueGuard.IsNumber(maxLength)) constraints.push((input) => input.max(maxLength))
if (t.ValueGuard.IsNumber(minLength)) constraints.push((input) => input.min(minLength))
return constraints.reduce((type, constraint) => constraint(type), z.string())
}
// ------------------------------------------------------------------
// Record
// ------------------------------------------------------------------
type TFromRecord<Key extends t.TSchema, Value extends t.TSchema> = TFromType<Key> extends infer ZodKey extends z.KeySchema ? z.ZodRecord<ZodKey, TFromType<Value>> : z.ZodNever
// prettier-ignore
function FromRecord(type: t.TRecord): z.ZodTypeAny {
const pattern = globalThis.Object.getOwnPropertyNames(type.patternProperties)[0]
const value = FromType(type.patternProperties[pattern])
return (
pattern === t.PatternBooleanExact ? z.record(z.boolean(), value) :
pattern === t.PatternNumberExact ? z.record(z.number(), value) :
pattern === t.PatternStringExact ? z.record(z.string(), value) :
z.record(z.string().regex(new RegExp(pattern)), value)
)
}
// ------------------------------------------------------------------
// Optional
// ------------------------------------------------------------------
type TFromOptional<Type extends t.TSchema, Result = z.ZodOptional<TFromType<Type>>> = Result
function FromOptional(type: t.TOptional<t.TSchema>): z.ZodTypeAny {
return z.optional(FromType(t.Optional(type, false)))
}
// ------------------------------------------------------------------
// Readonly
// ------------------------------------------------------------------
type TFromReadonly<Type extends t.TSchema, Result = z.ZodReadonly<TFromType<Type>>> = Result
function FromReadonly(type: t.TReadonly<t.TSchema>): z.ZodTypeAny {
return FromType(t.Readonly(type, false))
}
// ------------------------------------------------------------------
// Never
// ------------------------------------------------------------------
type TFromNever<Result = z.ZodNever> = Result
function FromNever(type: t.TNever): z.ZodTypeAny {
return z.never()
}
// ------------------------------------------------------------------
// Never
// ------------------------------------------------------------------
type TFromNull<Result = z.ZodNull> = Result
function FromNull(_type: t.TNull): z.ZodTypeAny {
return z.null()
}
// ------------------------------------------------------------------
// Number
// ------------------------------------------------------------------
type TFromNumber<Result = z.ZodNumber> = Result
function FromNumber(type: t.TNumber): z.ZodTypeAny {
const { exclusiveMaximum, exclusiveMinimum, minimum, maximum, multipleOf } = type
const constraints: TConstraint<z.ZodNumber>[] = []
if (t.ValueGuard.IsNumber(exclusiveMinimum)) constraints.push((input) => input.min(exclusiveMinimum + 1))
if (t.ValueGuard.IsNumber(exclusiveMaximum)) constraints.push((input) => input.min(exclusiveMaximum - 1))
if (t.ValueGuard.IsNumber(maximum)) constraints.push((input) => input.max(maximum))
if (t.ValueGuard.IsNumber(minimum)) constraints.push((input) => input.min(minimum))
if (t.ValueGuard.IsNumber(multipleOf)) constraints.push((input) => input.multipleOf(multipleOf))
return constraints.reduce((input, constraint) => constraint(input), z.number())
}
// ------------------------------------------------------------------
// String
// ------------------------------------------------------------------
type TFromString<Result = z.ZodString> = Result
// prettier-ignore
function FromString(type: t.TString): z.ZodTypeAny {
const constraints: TConstraint<z.ZodString>[] = []
const { minLength, maxLength, pattern, format } = type
if (t.ValueGuard.IsNumber(maxLength)) constraints.push((input) => input.max(maxLength))
if (t.ValueGuard.IsNumber(minLength)) constraints.push((input) => input.min(minLength))
if (t.ValueGuard.IsString(pattern)) constraints.push((input) => input.regex(new RegExp(pattern)))
if (t.ValueGuard.IsString(format))
constraints.push((input) =>
format === 'base64' ? input.base64() :
format === 'base64url' ? input.base64url() :
format === 'cidrv4' ? input.cidr({ version: 'v4' }) :
format === 'cidrv6' ? input.cidr({ version: 'v6' }) :
format === 'cidr' ? input.cidr() :
format === 'cuid' ? input.cuid() :
format === 'cuid2' ? input.cuid2() :
format === 'date' ? input.date() :
format === 'datetime' ? input.datetime() :
format === 'duration' ? input.duration() :
format === 'email' ? input.email() :
format === 'emoji' ? input.emoji() :
format === 'ipv4' ? input.ip({ version: 'v4' }) :
format === 'ipv6' ? input.ip({ version: 'v6' }) :
format === 'ip' ? input.ip() :
format === 'jwt' ? input.jwt() :
format === 'nanoid' ? input.nanoid() :
format === 'time' ? input.time() :
format === 'ulid' ? input.ulid() :
format === 'url' ? input.url() :
format === 'uuid' ? input.uuid() :
input,
)
return constraints.reduce((type, constraint) => constraint(type), z.string())
}
// ------------------------------------------------------------------
// Symbol
// ------------------------------------------------------------------
type TFromSymbol<Result = z.ZodSymbol> = Result
function FromSymbol(_type: t.TSymbol): z.ZodTypeAny {
return z.symbol()
}
// ------------------------------------------------------------------
// Tuple
// ------------------------------------------------------------------
// prettier-ignore
type TFromTuple<Types extends t.TSchema[], Mapped extends z.ZodTypeAny[] = TFromTypes<Types>> = (
Mapped extends [z.ZodTypeAny, ...z.ZodTypeAny[]] | []
? z.ZodTuple<Mapped>
: z.ZodNever
)
function FromTuple(type: t.TTuple): z.ZodTypeAny {
const mapped = FromTypes(type.items || []) as [] | [z.ZodTypeAny, ...z.ZodTypeAny[]]
return z.tuple(mapped)
}
// ------------------------------------------------------------------
// Undefined
// ------------------------------------------------------------------
type TFromUndefined<Result = z.ZodUndefined> = Result
function FromUndefined(_type: t.TUndefined): z.ZodTypeAny {
return z.undefined()
}
// ------------------------------------------------------------------
// Union
// ------------------------------------------------------------------
// prettier-ignore
type TFromUnion<Types extends t.TSchema[], Mapped extends z.ZodTypeAny[] = TFromTypes<Types>> = (
Mapped extends z.ZodUnionOptions ? z.ZodUnion<Mapped> : z.ZodNever
)
function FromUnion(_type: t.TUnion): z.ZodTypeAny {
const mapped = FromTypes(_type.anyOf) as [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]]
return mapped.length >= 1 ? z.union(mapped) : z.never()
}
// ------------------------------------------------------------------
// TUnknown
// ------------------------------------------------------------------
type TFromUnknown<Result = z.ZodUnknown> = Result
function FromUnknown(_type: t.TUnknown): z.ZodTypeAny {
return z.unknown()
}
// ------------------------------------------------------------------
// Void
// ------------------------------------------------------------------
type TFromVoid<Result = z.ZodVoid> = Result
function FromVoid(_type: t.TVoid): z.ZodTypeAny {
return z.void()
}
// ------------------------------------------------------------------
// Types
// ------------------------------------------------------------------
type TFromTypes<Types extends t.TSchema[], Result extends z.ZodTypeAny[] = []> = Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]] ? TFromTypes<Right, [...Result, TFromType<Left>]> : Result
function FromTypes(types: t.TSchema[]): z.ZodTypeAny[] {
return types.map((type) => FromType(type))
}
// ------------------------------------------------------------------
// Type
// ------------------------------------------------------------------
// prettier-ignore
type TFromType<Type extends t.TSchema> = (
Type extends t.TReadonly<infer Type extends t.TSchema> ? TFromReadonly<Type> :
Type extends t.TOptional<infer Type extends t.TSchema> ? TFromOptional<Type> :
Type extends t.TAny ? TFromAny :
Type extends t.TArray<infer Type extends t.TSchema> ? TFromArray<Type> :
Type extends t.TBigInt ? TFromBigInt :
Type extends t.TBoolean ? TFromBoolean :
Type extends t.TDate ? TFromDate :
Type extends t.TFunction<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? TFromFunction<Parameters, ReturnType> :
Type extends t.TInteger ? TFromInteger :
Type extends t.TIntersect<infer Types extends t.TSchema[]> ? TFromIntersect<Types> :
Type extends t.TLiteral<infer Value extends t.TLiteralValue> ? TFromLiteral<Value> :
Type extends t.TNever ? TFromNever :
Type extends t.TNull ? TFromNull :
Type extends t.TNumber ? TFromNumber :
Type extends t.TObject<infer Properties extends t.TProperties> ? TFromObject<Properties> :
Type extends t.TPromise<infer Type extends t.TSchema> ? TFromPromise<Type> :
Type extends t.TRecord<infer Key extends t.TSchema, infer Value extends t.TSchema> ? TFromRecord<Key, Value> :
Type extends t.TRegExp ? TFromRegExp :
Type extends t.TString ? TFromString :
Type extends t.TSymbol ? TFromSymbol :
Type extends t.TTuple<infer Types extends t.TSchema[]> ? TFromTuple<Types> :
Type extends t.TUndefined ? TFromUndefined :
Type extends t.TUnion<infer Types extends t.TSchema[]> ? TFromUnion<Types> :
Type extends t.TUnknown ? TFromUnknown :
Type extends t.TVoid ? TFromVoid :
z.ZodNever
)
// prettier-ignore
function FromType(type: t.TSchema): z.ZodTypeAny {
const constraints: TConstraint<z.ZodTypeAny>[] = []
if(!t.ValueGuard.IsUndefined(type.description)) constraints.push(input => input.describe(type.description!))
if(!t.ValueGuard.IsUndefined(type.default)) constraints.push(input => input.default(type.default))
return constraints.reduce((type, constraint) => constraint(type), (
t.KindGuard.IsReadonly(type) ? FromReadonly(type) :
t.KindGuard.IsOptional(type) ? FromOptional(type) :
t.KindGuard.IsAny(type) ? FromAny(type) :
t.KindGuard.IsArray(type) ? FromArray(type) :
t.KindGuard.IsBigInt(type) ? FromBigInt(type) :
t.KindGuard.IsBoolean(type) ? FromBoolean(type) :
t.KindGuard.IsDate(type) ? FromDate(type) :
t.KindGuard.IsFunction(type) ? FromFunction(type) :
t.KindGuard.IsInteger(type) ? FromInteger(type) :
t.KindGuard.IsIntersect(type) ? FromIntersect(type) :
t.KindGuard.IsLiteral(type) ? FromLiteral(type) :
t.KindGuard.IsNever(type) ? FromNever(type) :
t.KindGuard.IsNull(type) ? FromNull(type) :
t.KindGuard.IsNumber(type) ? FromNumber(type) :
t.KindGuard.IsObject(type) ? FromObject(type) :
t.KindGuard.IsPromise(type) ? FromPromise(type) :
t.KindGuard.IsRegExp(type) ? FromRegExp(type) :
t.KindGuard.IsRecord(type) ? FromRecord(type) :
t.KindGuard.IsString(type) ? FromString(type) :
t.KindGuard.IsSymbol(type) ? FromSymbol(type) :
t.KindGuard.IsTuple(type) ? FromTuple(type) :
t.KindGuard.IsUndefined(type) ? FromUndefined(type) :
t.KindGuard.IsUnion(type) ? FromUnion(type) :
t.KindGuard.IsUnknown(type) ? FromUnknown(type) :
t.KindGuard.IsVoid(type) ? FromVoid(type) :
z.never()
))
}
// ------------------------------------------------------------------
// ZodFromTypeBox
// ------------------------------------------------------------------
// prettier-ignore
export type TZodFromTypeBox<Type extends object | string> = (
Type extends t.TSchema ? TFromType<Type> : z.ZodNever
)
export function ZodFromTypeBox<Type extends object | string>(type: Type): TZodFromTypeBox<Type> {
return (t.KindGuard.IsSchema(type) ? FromType(type) : z.never()) as never
}

View File

@@ -0,0 +1,45 @@
/*--------------------------------------------------------------------------
@sinclair/typemap
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
import { type TTypeBoxFromValibot, TypeBoxFromValibot } from '../typebox/typebox-from-valibot'
import { type TZodFromTypeBox, ZodFromTypeBox } from './zod-from-typebox'
import * as t from '@sinclair/typebox'
import * as z from 'zod'
// prettier-ignore
export type TZodFromValibot<Type extends object | string,
Schema extends t.TSchema = TTypeBoxFromValibot<Type>,
Result extends z.ZodTypeAny | z.ZodEffects<any> = TZodFromTypeBox<Schema>
> = Result
// prettier-ignore
export function ZodFromValibot<Type extends object | string>(type: Type): TZodFromValibot<Type> {
const schema = TypeBoxFromValibot(type)
const result = ZodFromTypeBox(schema)
return result
}

View File

@@ -1,6 +1,6 @@
/*--------------------------------------------------------------------------
@sinclair/typebox-adapter
@sinclair/typemap
The MIT License (MIT)
@@ -26,4 +26,21 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/
export * from './box'
import * as Guard from '../guard'
import * as z from 'zod'
type BaseType = z.ZodTypeAny | z.ZodEffects<any>
// prettier-ignore
export type TZodFromZod<Type extends object | string,
Result extends BaseType = (
Type extends BaseType
? Type
: z.ZodNever
)
> = Result
// prettier-ignore
export function ZodFromZod<Type extends object | string>(type: Type): TZodFromZod<Type> {
return (Guard.IsZod(type) ? type : z.never()) as never
}

56
src/zod/zod.ts Normal file
View File

@@ -0,0 +1,56 @@
/*--------------------------------------------------------------------------
@sinclair/typemap
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
import { type TZodFromSyntax, ZodFromSyntax } from './zod-from-syntax'
import { type TZodFromTypeBox, ZodFromTypeBox } from './zod-from-typebox'
import { type TZodFromValibot, ZodFromValibot } from './zod-from-valibot'
import { type TZodFromZod, ZodFromZod } from './zod-from-zod'
import * as Guard from '../guard'
import * as z from 'zod'
/** Creates a Zod type from Syntax or another Type */
// prettier-ignore
export type TZod<Type extends object | string> = (
Guard.TIsSyntax<Type> extends true ? TZodFromSyntax<Type> :
Guard.TIsTypeBox<Type> extends true ? TZodFromTypeBox<Type> :
Guard.TIsValibot<Type> extends true ? TZodFromValibot<Type> :
Guard.TIsZod<Type> extends true ? TZodFromZod<Type> :
z.ZodNever
)
/** Creates a Zod type from Syntax or another Type */
// prettier-ignore
export function Zod<Type extends object | string, Result = TZod<Type>>(type: Type): Result {
return (
Guard.IsSyntax(type) ? ZodFromSyntax(type) :
Guard.IsTypeBox(type) ? ZodFromTypeBox(type) :
Guard.IsValibot(type) ? ZodFromValibot(type) :
Guard.IsZod(type) ? ZodFromZod(type) :
z.never()
) as never
}