diff --git a/example/index.ts b/example/index.ts index 3b8d36e..188fa94 100644 --- a/example/index.ts +++ b/example/index.ts @@ -34,4 +34,14 @@ const C = Compile(X) // const C: Validator> \ No newline at end of file + // }>> + +// ------------------------------------------------------------------ +// Validate | Standard +// ------------------------------------------------------------------ + +const R = C['~standard'].validate(null) // const R: StandardSchemaV1.Result<{ + // x: number; + // y: number; + // z: number; + // }> \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f463608..29fffb2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typemap", - "version": "0.8.10", + "version": "0.8.11", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typemap", - "version": "0.8.10", + "version": "0.8.11", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.17.2", diff --git a/package.json b/package.json index ea58843..8b58a7e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typemap", - "version": "0.8.10", + "version": "0.8.11", "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 d200324..c4c19f8 100644 --- a/src/compile/validator.ts +++ b/src/compile/validator.ts @@ -28,19 +28,23 @@ 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' +import * as s from './standard' // ------------------------------------------------------------------ // StandardSchemaProps // ------------------------------------------------------------------ -export class StandardSchemaProps implements StandardSchemaV1.Props, t.Static> { - private readonly _check: TypeCheck +// prettier-ignore +export class StandardSchemaProps, + Input extends Static = Static, + Output extends Static = Static +> implements s.StandardSchemaV1.Props { + private readonly __check: TypeCheck constructor(check: TypeCheck) { - this._check = check + this.__check = check } // ---------------------------------------------------------------- - // StandardSchemaV1.Props> + // StandardSchemaV1.Props // ---------------------------------------------------------------- public get vendor(): '@sinclair/typemap' { return '@sinclair/typemap' @@ -48,52 +52,54 @@ export class StandardSchemaProps implements StandardSche public get version(): 1 { return 1 } - public get types(): { input: t.Static; output: t.Static } { + public get types(): { input: Input; output: Output } { 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)) as never + public validate(value: unknown): s.StandardSchemaV1.Result { + return ( + this.__check.Check(value) ? this.__createValue(value) : this.__createIssues(value) + ) as never } // ---------------------------------------------------------------- // Internal // ---------------------------------------------------------------- - private _createIssues(value: unknown) { - const errors = [...Value.Errors(this._check.Schema(), value)] - const issues: StandardSchemaV1.Issue[] = errors.map((error) => ({ ...error, path: [error.path] })) + private __createIssues(value: unknown) { + const errors = [...Value.Errors(this.__check.Schema(), value)] + const issues: s.StandardSchemaV1.Issue[] = errors.map((error) => ({ ...error, path: [error.path] })) return { issues } } - private _createValue(value: unknown) { + private __createValue(value: unknown) { return { value } } } // ------------------------------------------------------------------ // Validator // ------------------------------------------------------------------ -export class Validator implements StandardSchemaV1, t.Static> { - private readonly _standard: StandardSchemaProps - private readonly _check: TypeCheck +export class Validator implements s.StandardSchemaV1, t.Static> { + private readonly __standard: StandardSchemaProps + private readonly __check: TypeCheck constructor(check: TypeCheck) { - this._standard = new StandardSchemaProps(check) - this._check = check + this.__standard = new StandardSchemaProps(check) + this.__check = check } /** Standard Schema Interface */ public get ['~standard'](): StandardSchemaProps { - return this._standard + return this.__standard as never } /** Returns the code used by this validator. */ public Code(): string { - return this._check.Code() + return this.__check.Code() } /** Parses this value. Do not use this function for high throughput validation */ public Parse(value: unknown): t.StaticDecode { - return Value.Parse(this._check.Schema(), value) + return Value.Parse(this.__check.Schema(), value) } /** Checks if this value matches the type */ public Check(value: unknown): value is t.Static { - return this._check.Check(value) + return this.__check.Check(value) } /** Returns errors for this value */ public Errors(value: unknown): ValueErrorIterator { - return this._check.Errors(value) + return this.__check.Errors(value) } }