Revision 0.8.10 (#20)

* Use Inferred Type for Standard Schema Input / Output

* Version
This commit is contained in:
sinclairzx81
2025-02-02 00:03:36 +09:00
committed by GitHub
parent 81a60a96cf
commit 444d09aaa1
5 changed files with 57 additions and 29 deletions

View File

@@ -1,25 +1,37 @@
import { TypeBox } from '@sinclair/typemap'
import { TypeBox, Valibot, Zod, Syntax, Compile } from '@sinclair/typemap'
import * as z from 'zod'
// ------------------------------------------------------------------
// Syntax Types
// ------------------------------------------------------------------
const Z = z.object({ // const Z: z.ZodObject<{
x: z.number(), // x: z.ZodNumber;
y: z.number(), // y: z.ZodNumber;
z: z.number() // z: z.ZodNumber;
}).strict() // }, "strict", ...>
const S = `{
x: number,
y: number,
z: number
}`
// TypeBox represents types as Json Schema
// ------------------------------------------------------------------
// Runtime Types
// ------------------------------------------------------------------
const T = TypeBox(Z) // const T = {
// type: 'object',
// required: ['x', 'y', 'z'],
// additionalProperties: false,
// properties: {
// x: { type: 'number' },
// y: { type: 'number' },
// z: { type: 'number' }
// }
// }
const T = TypeBox(S) // const T: TObject<{ ... }>
const V = Valibot(S) // const V: ObjectSchema<{ ... }, ...>
console.log(T)
const Z = Zod(S) // const Z: ZodObject<{ ... }, ...>
// ------------------------------------------------------------------
// Reverse Syntax
// ------------------------------------------------------------------
const X = Syntax(Z) // const X: "{ x: number, y: number, z: number }"
// ------------------------------------------------------------------
// Compile
// ------------------------------------------------------------------
const C = Compile(X) // const C: Validator<TObject<{
// x: TNumber;
// y: TNumber;
// z: TNumber;
// }>>