diff --git a/example/index.ts b/example/index.ts index 60b14a6..3b8d36e 100644 --- a/example/index.ts +++ b/example/index.ts @@ -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) \ No newline at end of file +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> \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d0b15f8..f463608 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typemap", - "version": "0.8.9", + "version": "0.8.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typemap", - "version": "0.8.9", + "version": "0.8.10", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.17.2", diff --git a/package.json b/package.json index 6f702ce..ea58843 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typemap", - "version": "0.8.9", + "version": "0.8.10", "description": "Syntax, Compiler and Translation System for Runtime Types", "author": "sinclairzx81", "license": "MIT", diff --git a/src/compile/validator.ts b/src/compile/validator.ts index 6269960..d200324 100644 --- a/src/compile/validator.ts +++ b/src/compile/validator.ts @@ -34,7 +34,7 @@ import * as t from '@sinclair/typebox' // ------------------------------------------------------------------ // StandardSchemaProps // ------------------------------------------------------------------ -export class StandardSchemaProps implements StandardSchemaV1.Props> { +export class StandardSchemaProps implements StandardSchemaV1.Props, t.Static> { private readonly _check: TypeCheck constructor(check: TypeCheck) { this._check = check @@ -48,11 +48,11 @@ export class StandardSchemaProps implements StandardSche public get version(): 1 { return 1 } - public get types(): { input: Type; output: t.Static } { - return { input: this._check.Schema(), output: null } + public get types(): { input: t.Static; output: t.Static } { + throw Error('types is a phantom property used for inference only.') } public validate(value: unknown): StandardSchemaV1.Result> { - return this._check.Check(value) ? this._createValue(value) : this._createIssues(value) + return (this._check.Check(value) ? this._createValue(value) : this._createIssues(value)) as never } // ---------------------------------------------------------------- // Internal @@ -69,7 +69,7 @@ export class StandardSchemaProps implements StandardSche // ------------------------------------------------------------------ // Validator // ------------------------------------------------------------------ -export class Validator implements StandardSchemaV1> { +export class Validator implements StandardSchemaV1, t.Static> { private readonly _standard: StandardSchemaProps private readonly _check: TypeCheck constructor(check: TypeCheck) { diff --git a/test/compile.ts b/test/compile.ts index e4ba0c8..4183abb 100644 --- a/test/compile.ts +++ b/test/compile.ts @@ -2,6 +2,9 @@ import { Assert } from './assert' import { TypeBox, Valibot, Zod, Compile } from '@sinclair/typemap' describe('Compile', () => { + // ---------------------------------------------------------------- + // Validator + // ---------------------------------------------------------------- it('Should compile Syntax', () => { const C = Compile('123') Assert.IsTrue(C.Check(123)) @@ -18,7 +21,6 @@ describe('Compile', () => { const C = Compile({ T: TypeBox('123') }, 'T') Assert.IsTrue(C.Check(123)) }) - it('Should compile Valibot', () => { const C = Compile(Valibot('123')) Assert.IsTrue(C.Check(123)) @@ -27,7 +29,6 @@ describe('Compile', () => { const C = Compile({ T: Valibot('123') }, 'T') Assert.IsTrue(C.Check(123)) }) - it('Should compile Zod', () => { const C = Compile(Zod('123')) Assert.IsTrue(C.Check(123)) @@ -36,4 +37,19 @@ describe('Compile', () => { const C = Compile({ T: Zod('123') }, 'T') Assert.IsTrue(C.Check(123)) }) + // ---------------------------------------------------------------- + // Standard Schema + // ---------------------------------------------------------------- + it('Should validate via Standard Schema interface (Success)', () => { + const C = Compile('string') + const R = C['~standard'].validate('hello') + // @ts-ignore + Assert.IsEqual(R.value, 'hello') // Reference spec interface is broken here, It's not for me to fix. + }) + it('Should validate via Standard Schema interface (Failure)', () => { + const C = Compile('string') + const R = C['~standard'].validate(12345) + Assert.IsTrue('issues' in R) + Assert.IsTrue(R.issues!.length > 0) + }) })