Revision 0.8.6 (#15)
* Parameterized Compile Types * Documentation | Tests * Infer Validator via Static * Version
This commit is contained in:
@@ -26,79 +26,17 @@ THE SOFTWARE.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
import { TypeCompiler, TypeCheck, ValueErrorIterator } from '@sinclair/typebox/compiler'
|
||||
import { TypeCompiler, TypeCheck } from '@sinclair/typebox/compiler'
|
||||
import { Value } from '@sinclair/typebox/value'
|
||||
import { TypeBox, TTypeBox } from '../typebox/typebox'
|
||||
import { IsEvalSupported } from './environment'
|
||||
import { StandardSchemaV1 } from './standard'
|
||||
import * as t from '@sinclair/typebox'
|
||||
import { Validator } from './validator'
|
||||
|
||||
import { type TTypeBox, TypeBox } from '../typebox/typebox'
|
||||
import { type TSyntaxOptions } from '../options'
|
||||
import { type TParameter } from '../typebox/typebox'
|
||||
import * as t from '@sinclair/typebox'
|
||||
import * as g from '../guard'
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// StandardSchemaProps
|
||||
// ------------------------------------------------------------------
|
||||
export class StandardSchemaProps<Type extends t.TSchema> implements StandardSchemaV1.Props<Type, t.Static<Type>> {
|
||||
readonly #check: TypeCheck<Type>
|
||||
constructor(check: TypeCheck<Type>) {
|
||||
this.#check = check
|
||||
}
|
||||
// ----------------------------------------------------------------
|
||||
// StandardSchemaV1.Props<Type, t.Static<Type>>
|
||||
// ----------------------------------------------------------------
|
||||
public get vendor(): '@sinclair/typemap' {
|
||||
return '@sinclair/typemap'
|
||||
}
|
||||
public get version(): 1 {
|
||||
return 1
|
||||
}
|
||||
public get types(): { input: Type; output: t.Static<Type> } {
|
||||
return { input: this.#check.Schema(), output: null }
|
||||
}
|
||||
public validate(value: unknown): StandardSchemaV1.Result<t.Static<Type>> {
|
||||
return this.#check.Check(value) ? this.#createValue(value) : this.#createIssues(value)
|
||||
}
|
||||
// ----------------------------------------------------------------
|
||||
// Internal
|
||||
// ----------------------------------------------------------------
|
||||
#createIssues(value: unknown) {
|
||||
const errors = [...Value.Errors(this.#check.Schema(), value)]
|
||||
const issues: StandardSchemaV1.Issue[] = errors.map((error) => ({ ...error, path: [error.path] }))
|
||||
return { issues }
|
||||
}
|
||||
#createValue(value: unknown) {
|
||||
return { value }
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Validator<TSchema>
|
||||
// ------------------------------------------------------------------
|
||||
export class Validator<Type extends t.TSchema> implements StandardSchemaV1<Type, t.Static<Type>> {
|
||||
private readonly _standard: StandardSchemaProps<Type>
|
||||
private readonly _check: TypeCheck<Type>
|
||||
constructor(check: TypeCheck<Type>) {
|
||||
this._standard = new StandardSchemaProps<Type>(check)
|
||||
this._check = check
|
||||
}
|
||||
/** Standard Schema Interface */
|
||||
public get ['~standard'](): StandardSchemaProps<Type> {
|
||||
return this._standard
|
||||
}
|
||||
/** Returns the code used by this validator. */
|
||||
public Code(): string {
|
||||
return this._check.Code()
|
||||
}
|
||||
/** Parses this value. Do not use this function for high throughput validation */
|
||||
public Parse(value: unknown): t.StaticDecode<Type> {
|
||||
return Value.Parse(this._check.Schema(), value)
|
||||
}
|
||||
/** Checks if this value matches the type */
|
||||
public Check(value: unknown): value is t.Static<Type> {
|
||||
return this._check.Check(value)
|
||||
}
|
||||
/** Returns errors for this value */
|
||||
public Errors(value: unknown): ValueErrorIterator {
|
||||
return this._check.Errors(value)
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// CompileDynamic
|
||||
// ------------------------------------------------------------------
|
||||
@@ -107,17 +45,32 @@ function CompileDynamic<Type extends t.TSchema>(type: Type, references: t.TSchem
|
||||
return new TypeCheck(type, references, value => Value.Check(type, references, value), TypeCompiler.Code(type, references))
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// ResolveTypeCheck
|
||||
// ------------------------------------------------------------------
|
||||
function ResolveTypeCheck<Type extends t.TSchema>(type: Type): TypeCheck<Type> {
|
||||
return IsEvalSupported() ? TypeCompiler.Compile(type) : CompileDynamic(type)
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Compile
|
||||
// ------------------------------------------------------------------
|
||||
/** Compiles a type for high performance validation */
|
||||
// prettier-ignore
|
||||
type TCompile<Type extends object | string,
|
||||
Schema extends t.TSchema = TTypeBox<{}, Type>,
|
||||
> = Validator<Schema>
|
||||
type TCompile<Paramter extends TParameter, Type extends object | string,
|
||||
Schema extends t.TSchema = TTypeBox<Paramter, Type>,
|
||||
Result = Validator<Schema>
|
||||
> = Result
|
||||
|
||||
/** Compiles a type for high performance validation */
|
||||
export function Compile<Parameter extends TParameter, Type extends string>(parameter: Parameter, type: Type, options?: TSyntaxOptions): TCompile<Parameter, Type>
|
||||
/** Compiles a type for high performance validation */
|
||||
export function Compile<Type extends string>(type: Type, options?: TSyntaxOptions): TCompile<{}, Type>
|
||||
/** Compiles a type for high performance validation */
|
||||
export function Compile<Type extends object>(type: Type, options?: TSyntaxOptions): TCompile<{}, Type>
|
||||
/** Compiles a type for high performance validation */
|
||||
// prettier-ignore
|
||||
export function Compile<Type extends object | string>(type: Type): TCompile<Type> {
|
||||
const schema = TypeBox(type)
|
||||
const check = IsEvalSupported() ? TypeCompiler.Compile(schema) : CompileDynamic(schema)
|
||||
return new Validator(check)
|
||||
export function Compile(...args: any[]): never {
|
||||
const [parameter, type, options] = g.Signature(args)
|
||||
const schema = t.ValueGuard.IsString(type) ? TypeBox(parameter, type, options) : TypeBox(type)
|
||||
const check = ResolveTypeCheck(schema)
|
||||
return new Validator(check) as never
|
||||
}
|
||||
|
||||
99
src/compile/validator.ts
Normal file
99
src/compile/validator.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@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 { TypeCheck, ValueErrorIterator } from '@sinclair/typebox/compiler'
|
||||
import { Value } from '@sinclair/typebox/value'
|
||||
import { StandardSchemaV1 } from './standard'
|
||||
import * as t from '@sinclair/typebox'
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// StandardSchemaProps<Input, Output>
|
||||
// ------------------------------------------------------------------
|
||||
export class StandardSchemaProps<Type extends t.TSchema> implements StandardSchemaV1.Props<Type, t.Static<Type>> {
|
||||
private readonly _check: TypeCheck<Type>
|
||||
constructor(check: TypeCheck<Type>) {
|
||||
this._check = check
|
||||
}
|
||||
// ----------------------------------------------------------------
|
||||
// StandardSchemaV1.Props<Type, t.Static<Type>>
|
||||
// ----------------------------------------------------------------
|
||||
public get vendor(): '@sinclair/typemap' {
|
||||
return '@sinclair/typemap'
|
||||
}
|
||||
public get version(): 1 {
|
||||
return 1
|
||||
}
|
||||
public get types(): { input: Type; output: t.Static<Type> } {
|
||||
return { input: this._check.Schema(), output: null }
|
||||
}
|
||||
public validate(value: unknown): StandardSchemaV1.Result<t.Static<Type>> {
|
||||
return this._check.Check(value) ? this._createValue(value) : this._createIssues(value)
|
||||
}
|
||||
// ----------------------------------------------------------------
|
||||
// Internal
|
||||
// ----------------------------------------------------------------
|
||||
private _createIssues(value: unknown) {
|
||||
const errors = [...Value.Errors(this._check.Schema(), value)]
|
||||
const issues: StandardSchemaV1.Issue[] = errors.map((error) => ({ ...error, path: [error.path] }))
|
||||
return { issues }
|
||||
}
|
||||
private _createValue(value: unknown) {
|
||||
return { value }
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// Validator<TSchema>
|
||||
// ------------------------------------------------------------------
|
||||
export class Validator<Type extends t.TSchema> implements StandardSchemaV1<Type, t.Static<Type>> {
|
||||
private readonly _standard: StandardSchemaProps<Type>
|
||||
private readonly _check: TypeCheck<Type>
|
||||
constructor(check: TypeCheck<Type>) {
|
||||
this._standard = new StandardSchemaProps<Type>(check)
|
||||
this._check = check
|
||||
}
|
||||
/** Standard Schema Interface */
|
||||
public get ['~standard'](): StandardSchemaProps<Type> {
|
||||
return this._standard
|
||||
}
|
||||
/** Returns the code used by this validator. */
|
||||
public Code(): string {
|
||||
return this._check.Code()
|
||||
}
|
||||
/** Parses this value. Do not use this function for high throughput validation */
|
||||
public Parse(value: unknown): t.StaticDecode<Type> {
|
||||
return Value.Parse(this._check.Schema(), value)
|
||||
}
|
||||
/** Checks if this value matches the type */
|
||||
public Check(value: unknown): value is t.Static<Type> {
|
||||
return this._check.Check(value)
|
||||
}
|
||||
/** Returns errors for this value */
|
||||
public Errors(value: unknown): ValueErrorIterator {
|
||||
return this._check.Errors(value)
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ THE SOFTWARE.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
import { Validator } from './compile/validator'
|
||||
import * as s from '@sinclair/typebox/syntax'
|
||||
import * as t from '@sinclair/typebox'
|
||||
import * as v from 'valibot'
|
||||
@@ -37,6 +38,7 @@ type BaseSchema = v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>
|
||||
// prettier-ignore
|
||||
export type Static<Type extends object | string> = (
|
||||
Type extends string ? s.StaticParseAsType<{}, Type> :
|
||||
Type extends Validator<infer Type extends t.TSchema> ? t.Static<Type> :
|
||||
Type extends t.TSchema ? t.Static<Type> :
|
||||
Type extends BaseSchema ? v.InferInput<Type> :
|
||||
Type extends z.ZodTypeAny ? z.infer<Type> :
|
||||
|
||||
@@ -81,13 +81,13 @@ export function TypeBox<Type extends object | string>(type: Type, options?: TSyn
|
||||
// prettier-ignore
|
||||
export function TypeBox(...args: any[]): never {
|
||||
const [parameter, type, options] = g.Signature(args)
|
||||
return t.CloneType(
|
||||
return (
|
||||
t.ValueGuard.IsString(type) ? TypeBoxFromSyntax(ContextFromParameter(parameter), type, options) :
|
||||
g.IsTypeBox(type) ? TypeBoxFromTypeBox(type) :
|
||||
g.IsValibot(type) ? TypeBoxFromValibot(type) :
|
||||
g.IsZod(type) ? TypeBoxFromZod(type) :
|
||||
t.Never(),
|
||||
options) as never
|
||||
t.Never()
|
||||
) as never
|
||||
}
|
||||
|
||||
/** Creates a TypeBox type from Syntax or another Type */
|
||||
|
||||
Reference in New Issue
Block a user