Revision 0.8.8 (#18)
* Reverse Syntax Mapping * Minor Optimizations * Documentation
This commit is contained in:
33
src/syntax/syntax-from-syntax.ts
Normal file
33
src/syntax/syntax-from-syntax.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typemap
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2024-2025 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.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
export type TSyntaxFromSyntax<Type extends string> = Type
|
||||
|
||||
export function SyntaxFromSyntax<Type extends string>(type: Type): TSyntaxFromSyntax<Type> {
|
||||
return type
|
||||
}
|
||||
365
src/syntax/syntax-from-typebox.ts
Normal file
365
src/syntax/syntax-from-typebox.ts
Normal file
@@ -0,0 +1,365 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typemap
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2024-2025 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'
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Characters
|
||||
// ------------------------------------------------------------------
|
||||
type TEmptyString = ''
|
||||
|
||||
type TAmpersand = '&'
|
||||
type TComma = ','
|
||||
type TPipe = '|'
|
||||
|
||||
const Ampersand = '&'
|
||||
const Comma = ','
|
||||
const Pipe = '|'
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Delmited
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromDelimited<Values extends string[], Delimiter extends string, Result extends string = TEmptyString> = (
|
||||
Values extends [infer Left extends string, ...infer Right extends string[]]
|
||||
? Result extends TEmptyString
|
||||
? TFromDelimited<Right, Delimiter, Left>
|
||||
: TFromDelimited<Right, Delimiter, `${Result}${Delimiter} ${Left}`>
|
||||
: Result
|
||||
)
|
||||
function FromDelimited(values: string[], delimiter: string): string {
|
||||
return values.join(delimiter)
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Any
|
||||
// ------------------------------------------------------------------
|
||||
type TFromAny<_Type extends t.TAny> = 'any'
|
||||
function FromAny(_type: t.TSchema): string {
|
||||
return 'any'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Array
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromArray<Type extends t.TSchema,
|
||||
Result extends string = `${TFromType<Type>}[]`
|
||||
> = Result
|
||||
function FromArray(type: t.TSchema): string {
|
||||
return `${type}[]`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// BigInt
|
||||
// ------------------------------------------------------------------
|
||||
type TFromBigInt<_Type extends t.TBigInt> = 'bigint'
|
||||
function FromBigInt(_type: t.TSchema): string {
|
||||
return 'bigint'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Boolean
|
||||
// ------------------------------------------------------------------
|
||||
type TFromBoolean<_Type extends t.TBoolean> = 'boolean'
|
||||
function FromBoolean(_type: t.TSchema): string {
|
||||
return 'boolean'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Constructor
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromConstructor<Parameters extends t.TSchema[], InstanceType extends t.TSchema,
|
||||
MappedParameters extends string = TFromParameters<Parameters>,
|
||||
MappedInstanceType extends string = TFromType<InstanceType>
|
||||
> = `new ${MappedParameters} => ${MappedInstanceType}`
|
||||
// prettier-ignore
|
||||
function FromConstructor(parameters: t.TSchema[], instanceType: t.TSchema): string {
|
||||
const mappedParameters = FromParameters(parameters)
|
||||
const mappedInstanceType = FromType(instanceType)
|
||||
return `new ${mappedParameters} => ${mappedInstanceType}`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Function
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromFunction<Parameters extends t.TSchema[], ReturnType extends t.TSchema,
|
||||
MappedParameters extends string = TFromParameters<Parameters>,
|
||||
MappedReturnType extends string = TFromType<ReturnType>
|
||||
> = `${MappedParameters} => ${MappedReturnType}`
|
||||
// prettier-ignore
|
||||
function FromFunction(parameters: t.TSchema[], returnType: t.TSchema): string {
|
||||
const mappedParameters = FromParameters(parameters)
|
||||
const mappedReturnType = FromType(returnType)
|
||||
return `${mappedParameters} => ${mappedReturnType}`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Integer
|
||||
// ------------------------------------------------------------------
|
||||
type TFromInteger<_Type extends t.TInteger> = 'integer'
|
||||
function FromInteger(_type: t.TSchema): string {
|
||||
return 'integer'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Intersect
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromIntersect<Types extends t.TSchema[], Result extends string[] = []> = (
|
||||
Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]]
|
||||
? TFromIntersect<Right, [...Result, TFromType<Left>]>
|
||||
: `(${TFromDelimited<Result, ` ${TAmpersand}`>})`
|
||||
)
|
||||
function FromIntersect(types: t.TSchema[]): string {
|
||||
const result = types.map((type) => FromType(type))
|
||||
return `(${FromDelimited(result, ` ${Ampersand} `)})`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Literal
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromLiteral<Value extends t.TLiteralValue,
|
||||
Result extends string = Value extends string ? `"${Value}"` : `${Value}`
|
||||
> = Result
|
||||
// prettier-ignore
|
||||
function FromLiteral(value: t.TLiteralValue): string {
|
||||
return t.ValueGuard.IsString(value) ? `"${value}"` : `${value}`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Number
|
||||
// ------------------------------------------------------------------
|
||||
type TFromNumber<_Type extends t.TNumber> = 'number'
|
||||
function FromNumber(_type: t.TSchema): string {
|
||||
return 'number'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Null
|
||||
// ------------------------------------------------------------------
|
||||
type TFromNull<_Type extends t.TNull> = 'null'
|
||||
function FromNull(_type: t.TSchema): string {
|
||||
return 'null'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Object
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromObject<Properties extends t.TProperties,
|
||||
PropertyKeys extends PropertyKey[] = t.UnionToTuple<keyof Properties>,
|
||||
Delimited extends string[] = TFromProperties<PropertyKeys, Properties>,
|
||||
Result extends string = TFromDelimited<Delimited, TComma>
|
||||
> = `{ ${Result} }`
|
||||
function FromObject(properties: t.TProperties): string {
|
||||
const propertyKeys = globalThis.Object.getOwnPropertyNames(properties)
|
||||
const delimited = FromProperties(propertyKeys, properties)
|
||||
const result = FromDelimited(delimited, Comma)
|
||||
return `{ ${result} }`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Parameters
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromParameters<Parameters extends t.TSchema[], Index extends string = '0', Result extends string[] = []> = (
|
||||
Parameters extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]]
|
||||
? TFromParameters<Right, t.TIncrement<Index>, [...Result, `arg${Index}: ${TFromType<Left>}`]>
|
||||
: `(${TFromDelimited<Result, TComma>})`
|
||||
)
|
||||
function FromParameters(parameters: t.TSchema[]): string {
|
||||
const result = parameters.map((parameter, index) => `arg${index}: ${FromType(parameter)}`)
|
||||
return `(${FromDelimited(result, Comma)})`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Property
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromProperty<Key extends string, Type extends t.TSchema,
|
||||
IsOptional extends boolean = Type extends t.TOptional<t.TSchema> ? true : false,
|
||||
IsReadonly extends boolean = Type extends t.TReadonly<t.TSchema> ? true : false,
|
||||
Mapped extends string = TFromType<Type>,
|
||||
Result = (
|
||||
[IsReadonly, IsOptional] extends [true, true] ? `readonly ${Key}?: ${Mapped}` :
|
||||
[IsReadonly, IsOptional] extends [false, true] ? `${Key}?: ${Mapped}` :
|
||||
[IsReadonly, IsOptional] extends [true, false] ? `readonly ${Key}: ${Mapped}` :
|
||||
`${Key}: ${Mapped}`
|
||||
)
|
||||
> = Result
|
||||
// prettier-ignore
|
||||
function FromProperty(key: string, type: t.TSchema): string {
|
||||
const isOptional = t.KindGuard.IsOptional(type)
|
||||
const isReadonly = t.KindGuard.IsReadonly(type)
|
||||
const mapped = FromType(type)
|
||||
return (
|
||||
isReadonly && isOptional ? `readonly ${key}?: ${mapped}` :
|
||||
!isReadonly && isOptional ? `${key}?: ${mapped}` :
|
||||
isReadonly && !isOptional ? `readonly ${key}: ${mapped}` :
|
||||
`${key}: ${mapped}`
|
||||
)
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Properties
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromProperties<PropertyKeys extends PropertyKey[], Properties extends t.TProperties, Result extends string[] = []> = (
|
||||
PropertyKeys extends [infer Left extends PropertyKey, ...infer Right extends PropertyKey[]]
|
||||
? (Left extends infer Key extends string
|
||||
? (Key extends keyof Properties
|
||||
? TFromProperties<Right, Properties, [...Result, TFromProperty<Key, Properties[Key]>]>
|
||||
: TFromProperties<Right, Properties, Result>
|
||||
) : TFromProperties<Right, Properties, Result>
|
||||
) : Result
|
||||
)
|
||||
// prettier-ignore
|
||||
function FromProperties(propertyKeys: PropertyKey[], properties: t.TProperties): string[] {
|
||||
return propertyKeys.reduce((result, left) => {
|
||||
const key = t.ValueGuard.IsString(left) || t.ValueGuard.IsNumber(left) || t.ValueGuard.IsBoolean(left) ? `${left}` : undefined
|
||||
return (
|
||||
t.ValueGuard.IsString(key)
|
||||
? (key in properties
|
||||
? [...result, FromProperty(key, properties[key])]
|
||||
: result
|
||||
): result
|
||||
)
|
||||
}, [] as string[])
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// String
|
||||
// ------------------------------------------------------------------
|
||||
type TFromString<_Type extends t.TString> = 'string'
|
||||
function FromString(_type: t.TSchema): string {
|
||||
return 'string'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Symbol
|
||||
// ------------------------------------------------------------------
|
||||
type TFromSymbol<_Type extends t.TSymbol> = 'symbol'
|
||||
function FromSymbol(_type: t.TSchema): string {
|
||||
return 'symbol'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Union
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromTuple<Types extends t.TSchema[], Result extends string[] = []> = (
|
||||
Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]]
|
||||
? TFromTuple<Right, [...Result, TFromType<Left>]>
|
||||
: `[${TFromDelimited<Result, TComma>}]`
|
||||
)
|
||||
function FromTuple(types: t.TSchema[]): string {
|
||||
const result = types.map((type) => FromType(type))
|
||||
return `[${FromDelimited(result, Comma)}]`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Undefined
|
||||
// ------------------------------------------------------------------
|
||||
type TFromUndefined<_Type extends t.TUndefined> = 'undefined'
|
||||
function FromUndefined(type: t.TSchema): string {
|
||||
return 'undefined'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Union
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromUnion<Types extends t.TSchema[], Result extends string[] = []> = (
|
||||
Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]]
|
||||
? TFromUnion<Right, [...Result, TFromType<Left>]>
|
||||
: `(${TFromDelimited<Result, ` ${TPipe}`>})`
|
||||
)
|
||||
function FromUnion(types: t.TSchema[]): string {
|
||||
const result = types.map((type) => FromType(type))
|
||||
return `(${FromDelimited(result, ` ${Pipe} `)})`
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Unknown
|
||||
// ------------------------------------------------------------------
|
||||
type TFromUnknown<Type extends t.TSchema> = 'unknown'
|
||||
function FromUnknown(type: t.TSchema): string {
|
||||
return 'unknown'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Void
|
||||
// ------------------------------------------------------------------
|
||||
type TFromVoid<Type extends t.TSchema> = 'void'
|
||||
function FromVoid(type: t.TSchema): string {
|
||||
return 'void'
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// FromType
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type TFromType<Type extends t.TSchema> = (
|
||||
Type extends t.TAny ? TFromAny<Type> :
|
||||
Type extends t.TArray<infer Type extends t.TSchema> ? TFromArray<Type> :
|
||||
Type extends t.TBigInt ? TFromBigInt<Type> :
|
||||
Type extends t.TBoolean ? TFromBoolean<Type> :
|
||||
Type extends t.TConstructor<infer Parameters extends t.TSchema[], infer InstanceType extends t.TSchema> ? TFromConstructor<Parameters, InstanceType> :
|
||||
Type extends t.TFunction<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? TFromFunction<Parameters, ReturnType> :
|
||||
Type extends t.TInteger ? TFromInteger<Type> :
|
||||
Type extends t.Intersect<infer Types extends t.TSchema[]> ? TFromIntersect<Types> :
|
||||
Type extends t.TLiteral<infer Value extends t.TLiteralValue> ? TFromLiteral<Value> :
|
||||
Type extends t.TNumber ? TFromNumber<Type> :
|
||||
Type extends t.TNull ? TFromNull<Type> :
|
||||
Type extends t.TObject<infer Properties extends t.TProperties> ? TFromObject<Properties> :
|
||||
Type extends t.TString ? TFromString<Type> :
|
||||
Type extends t.TSymbol ? TFromSymbol<Type> :
|
||||
Type extends t.TTuple<infer Types extends t.TSchema[]> ? TFromTuple<Types> :
|
||||
Type extends t.TUndefined ? TFromUndefined<Type> :
|
||||
Type extends t.TUnion<infer Types extends t.TSchema[]> ? TFromUnion<Types> :
|
||||
Type extends t.TUnknown ? TFromUnknown<Type> :
|
||||
Type extends t.TVoid ? TFromVoid<Type> :
|
||||
'never'
|
||||
)
|
||||
// prettier-ignore
|
||||
function FromType<Type extends t.TSchema>(type: Type): TFromType<Type> {
|
||||
return (
|
||||
t.KindGuard.IsAny(type) ? FromAny(type) :
|
||||
t.KindGuard.IsArray(type) ? FromArray(type.items) :
|
||||
t.KindGuard.IsBigInt(type) ? FromBigInt(type) :
|
||||
t.KindGuard.IsBoolean(type) ? FromBoolean(type) :
|
||||
t.KindGuard.IsConstructor(type) ? FromConstructor(type.parameters, type.returns) :
|
||||
t.KindGuard.IsFunction(type) ? FromFunction(type.parameters, type.returns) :
|
||||
t.KindGuard.IsInteger(type) ? FromInteger(type) :
|
||||
t.KindGuard.IsIntersect(type) ? FromIntersect(type.allOf) :
|
||||
t.KindGuard.IsLiteral(type) ? FromLiteral(type.const) :
|
||||
t.KindGuard.IsNumber(type) ? FromNumber(type) :
|
||||
t.KindGuard.IsNull(type) ? FromNull(type) :
|
||||
t.KindGuard.IsObject(type) ? FromObject(type.properties) :
|
||||
t.KindGuard.IsString(type) ? FromString(type) :
|
||||
t.KindGuard.IsSymbol(type) ? FromSymbol(type) :
|
||||
t.KindGuard.IsTuple(type) ? FromTuple(type.items || []) :
|
||||
t.KindGuard.IsUndefined(type) ? FromUndefined(type) :
|
||||
t.KindGuard.IsUnion(type) ? FromUnion(type.anyOf) :
|
||||
t.KindGuard.IsUnknown(type) ? FromUnknown(type) :
|
||||
t.KindGuard.IsVoid(type) ? FromVoid(type) :
|
||||
'never'
|
||||
) as never
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// SyntaxFromTypeBox
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
export type TSyntaxFromTypeBox<Type extends t.TSchema,
|
||||
Result extends string = TFromType<Type>
|
||||
> = Result
|
||||
export function SyntaxFromTypeBox<Type extends t.TSchema>(type: t.TSchema): TSyntaxFromTypeBox<Type> {
|
||||
return FromType(type) as never
|
||||
}
|
||||
48
src/syntax/syntax-from-valibot.ts
Normal file
48
src/syntax/syntax-from-valibot.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typemap
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2024-2025 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 TSyntaxFromTypeBox, SyntaxFromTypeBox } from './syntax-from-typebox'
|
||||
|
||||
import * as t from '@sinclair/typebox'
|
||||
import * as v from 'valibot'
|
||||
|
||||
/** Creates Syntax from Valibot */
|
||||
// prettier-ignore
|
||||
export type TSyntaxFromValibot<Type extends v.BaseSchema<any, any, any>,
|
||||
TypeBox extends t.TSchema = TTypeBoxFromValibot<Type>,
|
||||
Result extends string = TSyntaxFromTypeBox<TypeBox>
|
||||
> = Result
|
||||
|
||||
/** Creates Syntax from Valibot */
|
||||
// prettier-ignore
|
||||
export function SyntaxFromValibot<Type extends v.BaseSchema<any, any, any>>(type: Type): TSyntaxFromValibot<Type> {
|
||||
const typebox = TypeBoxFromValibot(type)
|
||||
const result = SyntaxFromTypeBox(typebox)
|
||||
return result as never
|
||||
}
|
||||
48
src/syntax/syntax-from-zod.ts
Normal file
48
src/syntax/syntax-from-zod.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typemap
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2024-2025 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 TTypeBoxFromZod, TypeBoxFromZod } from '../typebox/typebox-from-zod'
|
||||
import { type TSyntaxFromTypeBox, SyntaxFromTypeBox } from './syntax-from-typebox'
|
||||
|
||||
import * as t from '@sinclair/typebox'
|
||||
import * as z from 'zod'
|
||||
|
||||
/** Creates Syntax from Zod */
|
||||
// prettier-ignore
|
||||
export type TSyntaxFromZod<Type extends z.ZodTypeAny | z.ZodEffects<any>,
|
||||
TypeBox extends t.TSchema = TTypeBoxFromZod<Type>,
|
||||
Result extends string = TSyntaxFromTypeBox<TypeBox>
|
||||
> = Result
|
||||
|
||||
/** Creates Syntax from Zod */
|
||||
// prettier-ignore
|
||||
export function SyntaxFromZod<Type extends z.ZodTypeAny | z.ZodEffects<any>>(type: Type): TSyntaxFromZod<Type> {
|
||||
const typebox = TypeBoxFromZod(type)
|
||||
const result = SyntaxFromTypeBox(typebox)
|
||||
return result as never
|
||||
}
|
||||
69
src/syntax/syntax.ts
Normal file
69
src/syntax/syntax.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typemap
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2024-2025 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 TSyntaxFromSyntax, SyntaxFromSyntax } from './syntax-from-syntax'
|
||||
import { type TSyntaxFromTypeBox, SyntaxFromTypeBox } from './syntax-from-typebox'
|
||||
import { type TSyntaxFromValibot, SyntaxFromValibot } from './syntax-from-valibot'
|
||||
import { type TSyntaxFromZod, SyntaxFromZod } from './syntax-from-zod'
|
||||
import { type TSyntaxOptions } from '../options'
|
||||
import { type TParameter } from '../typebox/typebox'
|
||||
|
||||
import * as g from '../guard'
|
||||
import * as z from 'zod'
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Zod
|
||||
// ------------------------------------------------------------------
|
||||
/** Creates Syntax by mapping from a remote Type */
|
||||
// prettier-ignore
|
||||
export type TSyntax<_Parameter extends TParameter, Type extends object | string, Result extends string = (
|
||||
Type extends g.SyntaxType ? TSyntaxFromSyntax<Type> :
|
||||
Type extends g.TypeBoxType ? TSyntaxFromTypeBox<Type> :
|
||||
Type extends g.ValibotType ? TSyntaxFromValibot<Type> :
|
||||
Type extends g.ZodType ? TSyntaxFromZod<Type> :
|
||||
'never'
|
||||
)> = Result
|
||||
|
||||
/** Creates Syntax by mapping from a remote Type */
|
||||
export function Syntax<Parameter extends TParameter, Type extends string>(parameter: Parameter, type: Type, options?: TSyntaxOptions): TSyntax<Parameter, Type>
|
||||
/** Creates Syntax by mapping from a remote Type */
|
||||
export function Syntax<Type extends string>(type: Type, options?: TSyntaxOptions): TSyntax<{}, Type>
|
||||
/** Creates Syntax by mapping from a remote Type */
|
||||
export function Syntax<Type extends object>(type: Type, options?: TSyntaxOptions): TSyntax<{}, Type>
|
||||
/** Creates Syntax by mapping from a remote Type */
|
||||
// prettier-ignore
|
||||
export function Syntax(...args: any[]): never {
|
||||
const [_parameter, type, _options] = g.Signature(args)
|
||||
return (
|
||||
g.IsSyntax(type) ? SyntaxFromSyntax(type) :
|
||||
g.IsTypeBox(type) ? SyntaxFromTypeBox(type) :
|
||||
g.IsValibot(type) ? SyntaxFromValibot(type) :
|
||||
g.IsZod(type) ? SyntaxFromZod(type) :
|
||||
z.never()
|
||||
) as never
|
||||
}
|
||||
Reference in New Issue
Block a user