typedriver 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/compile.d.mts +11 -0
- package/build/compile.mjs +21 -0
- package/build/errors.d.mts +11 -0
- package/build/errors.mjs +17 -0
- package/build/guard.d.mts +5 -0
- package/build/guard.mjs +43 -0
- package/build/index.d.mts +3 -0
- package/build/index.mjs +4 -0
- package/build/json-schema/index.d.mts +1 -0
- package/build/json-schema/index.mjs +1 -0
- package/build/json-schema/validator.d.mts +14 -0
- package/build/json-schema/validator.mjs +44 -0
- package/build/standard-schema/index.d.mts +1 -0
- package/build/standard-schema/index.mjs +1 -0
- package/build/standard-schema/validator.d.mts +13 -0
- package/build/standard-schema/validator.mjs +50 -0
- package/build/static.d.mts +10 -0
- package/build/static.mjs +2 -0
- package/build/typescript/index.d.mts +1 -0
- package/build/typescript/index.mjs +1 -0
- package/build/typescript/validator.d.mts +15 -0
- package/build/typescript/validator.mjs +46 -0
- package/build/validator.d.mts +17 -0
- package/build/validator.mjs +4 -0
- package/license +23 -0
- package/package.json +57 -0
- package/readme.md +214 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import Type from 'typebox';
|
|
3
|
+
import { Validator } from './validator.mjs';
|
|
4
|
+
type TFromTypeScript<Input extends string, Schema extends Type.TSchema = Type.TScript<{}, Input>, Output extends unknown = Type.Static<Schema>, Result extends Validator = Validator<Input, Output>> = Result;
|
|
5
|
+
type TFromStandardSchema<Input extends StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferInput<Input>, Result extends Validator = Validator<Input, Output>> = Result;
|
|
6
|
+
type TFromJsonSchema<Input extends Type.TSchema, Output extends unknown = Type.Static<Input>, Result extends Validator = Validator<Input, Output>> = Result;
|
|
7
|
+
/** Compiles a schema into a typed Validator */
|
|
8
|
+
export type TCompile<Schema extends unknown> = (Schema extends string ? TFromTypeScript<Schema> : Schema extends StandardSchemaV1 ? TFromStandardSchema<Schema> : Schema extends Type.TSchema ? TFromJsonSchema<Schema> : TFromJsonSchema<{}>);
|
|
9
|
+
/** Compiles a schema into a typed Validator */
|
|
10
|
+
export declare function compile<const Schema extends unknown>(schema: Schema): TCompile<Schema>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { IsJsonSchema, IsStandardSchemaV1, IsTypeScript } from './guard.mjs';
|
|
3
|
+
import { JsonSchemaValidator } from './json-schema/validator.mjs';
|
|
4
|
+
import { StandardSchemaValidator } from './standard-schema/validator.mjs';
|
|
5
|
+
import { TypeScriptValidator } from './typescript/validator.mjs';
|
|
6
|
+
function FromTypeScript(script) {
|
|
7
|
+
return new TypeScriptValidator(script);
|
|
8
|
+
}
|
|
9
|
+
function FromStandardSchema(schema) {
|
|
10
|
+
return new StandardSchemaValidator(schema);
|
|
11
|
+
}
|
|
12
|
+
function FromJsonSchema(schema) {
|
|
13
|
+
return new JsonSchemaValidator(schema);
|
|
14
|
+
}
|
|
15
|
+
/** Compiles a schema into a typed Validator */
|
|
16
|
+
export function compile(schema) {
|
|
17
|
+
return (IsTypeScript(schema) ? FromTypeScript(schema) :
|
|
18
|
+
IsStandardSchemaV1(schema) ? FromStandardSchema(schema) :
|
|
19
|
+
IsJsonSchema(schema) ? FromJsonSchema(schema) :
|
|
20
|
+
FromJsonSchema({}));
|
|
21
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class AssertError extends globalThis.Error {
|
|
2
|
+
readonly errors: object[];
|
|
3
|
+
constructor(errors: object[]);
|
|
4
|
+
}
|
|
5
|
+
export declare class ParseError extends globalThis.Error {
|
|
6
|
+
readonly errors: object[];
|
|
7
|
+
constructor(errors: object[]);
|
|
8
|
+
}
|
|
9
|
+
export declare class UnknownError extends globalThis.Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
package/build/errors.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class AssertError extends globalThis.Error {
|
|
2
|
+
constructor(errors) {
|
|
3
|
+
super('AssertError');
|
|
4
|
+
this.errors = errors;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class ParseError extends globalThis.Error {
|
|
8
|
+
constructor(errors) {
|
|
9
|
+
super('ParseError');
|
|
10
|
+
this.errors = errors;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class UnknownError extends globalThis.Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(`UnknownError: ${message}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import Type from 'typebox';
|
|
3
|
+
export declare function IsStandardSchemaV1(value: unknown): value is StandardSchemaV1;
|
|
4
|
+
export declare function IsJsonSchema(value: unknown): value is Type.TSchema;
|
|
5
|
+
export declare function IsTypeScript(value: unknown): value is string;
|
package/build/guard.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import Guard from 'typebox/guard';
|
|
3
|
+
// ------------------------------------------------------------------
|
|
4
|
+
// IsStandardSchemaV1
|
|
5
|
+
// ------------------------------------------------------------------
|
|
6
|
+
function IsStandardSchemaV1Props(value) {
|
|
7
|
+
return Guard.IsObject(value) &&
|
|
8
|
+
Guard.HasPropertyKey(value, 'version') &&
|
|
9
|
+
Guard.HasPropertyKey(value, 'vendor') &&
|
|
10
|
+
Guard.HasPropertyKey(value, 'validate') &&
|
|
11
|
+
(Guard.IsEqual(value.version, '1') || // spec
|
|
12
|
+
Guard.IsEqual(value.version, 1) // arktype
|
|
13
|
+
) &&
|
|
14
|
+
Guard.IsString(value.vendor) &&
|
|
15
|
+
Guard.IsFunction(value.validate);
|
|
16
|
+
}
|
|
17
|
+
function IsTypicalStandardSchemaV1(value) {
|
|
18
|
+
return Guard.IsObject(value) &&
|
|
19
|
+
Guard.HasPropertyKey(value, '~standard') &&
|
|
20
|
+
IsStandardSchemaV1Props(value['~standard']);
|
|
21
|
+
}
|
|
22
|
+
// ArkType
|
|
23
|
+
function IsNonTypicalStandardSchemaV1(value) {
|
|
24
|
+
return Guard.IsFunction(value) &&
|
|
25
|
+
!Guard.IsUndefined(value['~standard']) &&
|
|
26
|
+
IsStandardSchemaV1Props(value['~standard']);
|
|
27
|
+
}
|
|
28
|
+
export function IsStandardSchemaV1(value) {
|
|
29
|
+
return IsTypicalStandardSchemaV1(value) ||
|
|
30
|
+
IsNonTypicalStandardSchemaV1(value);
|
|
31
|
+
}
|
|
32
|
+
// ------------------------------------------------------------------
|
|
33
|
+
// IsJsonSchema
|
|
34
|
+
// ------------------------------------------------------------------
|
|
35
|
+
export function IsJsonSchema(value) {
|
|
36
|
+
return !IsStandardSchemaV1(value) && Guard.IsObjectNotArray(value);
|
|
37
|
+
}
|
|
38
|
+
// ------------------------------------------------------------------
|
|
39
|
+
// IsTypeScript
|
|
40
|
+
// ------------------------------------------------------------------
|
|
41
|
+
export function IsTypeScript(value) {
|
|
42
|
+
return Guard.IsString(value);
|
|
43
|
+
}
|
package/build/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './validator.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './validator.mjs';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Validator } from '../validator.mjs';
|
|
2
|
+
import Type from 'typebox';
|
|
3
|
+
export declare class JsonSchemaValidator<Input extends Type.TSchema, Output extends unknown = Type.Static<Input>> extends Validator<Input, Output> {
|
|
4
|
+
private readonly input;
|
|
5
|
+
private readonly validator;
|
|
6
|
+
constructor(input: Input);
|
|
7
|
+
schema(): Input;
|
|
8
|
+
isJsonSchema(): boolean;
|
|
9
|
+
asJsonSchema(): unknown;
|
|
10
|
+
assert(value: unknown): asserts value is Output;
|
|
11
|
+
check(value: unknown): value is Output;
|
|
12
|
+
parse(value: unknown): Output;
|
|
13
|
+
errors(value: unknown): object[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { Validator as TBValidator } from 'typebox/compile';
|
|
3
|
+
import { AssertError, ParseError } from '../errors.mjs';
|
|
4
|
+
import { Validator } from '../validator.mjs';
|
|
5
|
+
export class JsonSchemaValidator extends Validator {
|
|
6
|
+
constructor(input) {
|
|
7
|
+
super();
|
|
8
|
+
this.input = input;
|
|
9
|
+
this.validator = new TBValidator({}, input);
|
|
10
|
+
}
|
|
11
|
+
// ----------------------------------------------------------------
|
|
12
|
+
// Schema
|
|
13
|
+
// ----------------------------------------------------------------
|
|
14
|
+
schema() {
|
|
15
|
+
return this.input;
|
|
16
|
+
}
|
|
17
|
+
// ----------------------------------------------------------------
|
|
18
|
+
// Json Schema
|
|
19
|
+
// ----------------------------------------------------------------
|
|
20
|
+
isJsonSchema() {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
asJsonSchema() {
|
|
24
|
+
return this.input;
|
|
25
|
+
}
|
|
26
|
+
// ----------------------------------------------------------------
|
|
27
|
+
// Validation
|
|
28
|
+
// ----------------------------------------------------------------
|
|
29
|
+
assert(value) {
|
|
30
|
+
if (!this.validator.Check(value))
|
|
31
|
+
throw new AssertError(this.errors(value));
|
|
32
|
+
}
|
|
33
|
+
check(value) {
|
|
34
|
+
return this.validator.Check(value);
|
|
35
|
+
}
|
|
36
|
+
parse(value) {
|
|
37
|
+
if (!this.validator.Check(value))
|
|
38
|
+
throw new ParseError(this.errors(value));
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
errors(value) {
|
|
42
|
+
return this.validator.Errors(value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './validator.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './validator.mjs';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { Validator } from '../validator.mjs';
|
|
3
|
+
export declare class StandardSchemaValidator<Input extends StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferOutput<Input>> extends Validator<Input, Output> {
|
|
4
|
+
private readonly input;
|
|
5
|
+
constructor(input: Input);
|
|
6
|
+
schema(): Input;
|
|
7
|
+
isJsonSchema(): boolean;
|
|
8
|
+
asJsonSchema(): unknown;
|
|
9
|
+
assert(value: unknown): asserts value is Output;
|
|
10
|
+
check(value: unknown): value is Output;
|
|
11
|
+
parse(value: unknown): Output;
|
|
12
|
+
errors(value: unknown): object[];
|
|
13
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { AssertError, ParseError, UnknownError } from '../errors.mjs';
|
|
3
|
+
import { Validator } from '../validator.mjs';
|
|
4
|
+
export class StandardSchemaValidator extends Validator {
|
|
5
|
+
constructor(input) {
|
|
6
|
+
super();
|
|
7
|
+
this.input = input;
|
|
8
|
+
}
|
|
9
|
+
// ----------------------------------------------------------------
|
|
10
|
+
// Schema
|
|
11
|
+
// ----------------------------------------------------------------
|
|
12
|
+
schema() {
|
|
13
|
+
return this.input;
|
|
14
|
+
}
|
|
15
|
+
// ----------------------------------------------------------------
|
|
16
|
+
// Json Schema
|
|
17
|
+
// ----------------------------------------------------------------
|
|
18
|
+
isJsonSchema() {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
asJsonSchema() {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
// ----------------------------------------------------------------
|
|
25
|
+
// Validation
|
|
26
|
+
// ----------------------------------------------------------------
|
|
27
|
+
assert(value) {
|
|
28
|
+
const result = this.input['~standard'].validate(value);
|
|
29
|
+
if ('issues' in result)
|
|
30
|
+
throw new AssertError(result.issues || []);
|
|
31
|
+
}
|
|
32
|
+
check(value) {
|
|
33
|
+
const result = this.input['~standard'].validate(value);
|
|
34
|
+
return !('issues' in result);
|
|
35
|
+
}
|
|
36
|
+
parse(value) {
|
|
37
|
+
const result = this.input['~standard'].validate(value);
|
|
38
|
+
if ('issues' in result)
|
|
39
|
+
throw new ParseError(result.issues || []);
|
|
40
|
+
if ('value' in result)
|
|
41
|
+
return result.value;
|
|
42
|
+
throw new UnknownError('Invalid result');
|
|
43
|
+
}
|
|
44
|
+
errors(value) {
|
|
45
|
+
const result = this.input['~standard'].validate(value);
|
|
46
|
+
if ('issues' in result)
|
|
47
|
+
return result.issues || [];
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { type Validator } from './validator.mjs';
|
|
3
|
+
import Type from 'typebox';
|
|
4
|
+
type StaticValidator<_Input extends unknown, Output extends unknown> = (Output);
|
|
5
|
+
type StaticTypeScript<Input extends string, Schema extends Type.TSchema = Type.TScript<{}, Input>, Output extends unknown = Type.Static<Schema>> = Output;
|
|
6
|
+
type StaticStandardSchema<Input extends StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferOutput<Input>> = Output;
|
|
7
|
+
type StaticJsonSchema<Input extends Type.TSchema, Output extends unknown = Type.Static<Input>> = Output;
|
|
8
|
+
type StaticInput<Input extends unknown> = (Input extends Validator<infer Input extends unknown, infer Output extends unknown> ? StaticValidator<Input, Output> : Input extends string ? StaticTypeScript<Input> : Input extends StandardSchemaV1 ? StaticStandardSchema<Input> : Input extends Type.TSchema ? StaticJsonSchema<Input> : unknown);
|
|
9
|
+
export type Static<Input, Output = StaticInput<Input>> = Output;
|
|
10
|
+
export {};
|
package/build/static.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './validator.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './validator.mjs';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Validator } from '../validator.mjs';
|
|
2
|
+
import Type from 'typebox';
|
|
3
|
+
export declare class TypeScriptValidator<Input extends string, Schema extends Type.TSchema = Type.TScript<{}, Input>, Output extends unknown = Type.Static<Schema>> extends Validator<Input, Output> {
|
|
4
|
+
private readonly validator;
|
|
5
|
+
private readonly script;
|
|
6
|
+
private readonly jsonschema;
|
|
7
|
+
constructor(script: Input);
|
|
8
|
+
schema(): Input;
|
|
9
|
+
isJsonSchema(): boolean;
|
|
10
|
+
asJsonSchema(): unknown;
|
|
11
|
+
check(value: unknown): value is Output;
|
|
12
|
+
assert(value: unknown): asserts value is Output;
|
|
13
|
+
parse(value: unknown): Output;
|
|
14
|
+
errors(value: unknown): object[];
|
|
15
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { Validator as TBValidator } from 'typebox/compile';
|
|
3
|
+
import { Validator } from '../validator.mjs';
|
|
4
|
+
import { AssertError, ParseError } from '../errors.mjs';
|
|
5
|
+
import Type from 'typebox';
|
|
6
|
+
export class TypeScriptValidator extends Validator {
|
|
7
|
+
constructor(script) {
|
|
8
|
+
super();
|
|
9
|
+
this.script = script;
|
|
10
|
+
this.jsonschema = Type.Script(this.script);
|
|
11
|
+
this.validator = new TBValidator({}, this.jsonschema);
|
|
12
|
+
}
|
|
13
|
+
// ----------------------------------------------------------------
|
|
14
|
+
// Schema
|
|
15
|
+
// ----------------------------------------------------------------
|
|
16
|
+
schema() {
|
|
17
|
+
return this.script;
|
|
18
|
+
}
|
|
19
|
+
// ----------------------------------------------------------------
|
|
20
|
+
// Json Schema
|
|
21
|
+
// ----------------------------------------------------------------
|
|
22
|
+
isJsonSchema() {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
asJsonSchema() {
|
|
26
|
+
return this.jsonschema;
|
|
27
|
+
}
|
|
28
|
+
// ----------------------------------------------------------------
|
|
29
|
+
// Validation
|
|
30
|
+
// ----------------------------------------------------------------
|
|
31
|
+
check(value) {
|
|
32
|
+
return this.validator.Check(value);
|
|
33
|
+
}
|
|
34
|
+
assert(value) {
|
|
35
|
+
if (!this.validator.Check(value))
|
|
36
|
+
throw new AssertError(this.errors(value));
|
|
37
|
+
}
|
|
38
|
+
parse(value) {
|
|
39
|
+
if (!this.validator.Check(value))
|
|
40
|
+
throw new ParseError(this.errors(value));
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
errors(value) {
|
|
44
|
+
return this.validator.Errors(value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Abstract Base for all Validator types. */
|
|
2
|
+
export declare abstract class Validator<Input extends unknown = unknown, Output extends unknown = unknown> {
|
|
3
|
+
/** Returns the internal schema used by this validator */
|
|
4
|
+
abstract schema(): Input;
|
|
5
|
+
/** Asserts a value matches the given schema */
|
|
6
|
+
abstract assert(value: unknown): asserts value is Output;
|
|
7
|
+
/** Checks a value matches the given schema */
|
|
8
|
+
abstract check(value: unknown): value is Output;
|
|
9
|
+
/** Parses a value and throws if invalid */
|
|
10
|
+
abstract parse(value: unknown): Output;
|
|
11
|
+
/** Returns errors for the given value */
|
|
12
|
+
abstract errors(value: unknown): object[];
|
|
13
|
+
/** True if the the validator supports Json Schema translation */
|
|
14
|
+
abstract isJsonSchema(): boolean;
|
|
15
|
+
/** Returns this validator as Json Schema. */
|
|
16
|
+
abstract asJsonSchema(): unknown;
|
|
17
|
+
}
|
package/license
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
TypeDriver
|
|
2
|
+
|
|
3
|
+
The MIT License (MIT)
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2025 Haydn Paterson
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
|
15
|
+
all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
|
+
THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "typedriver",
|
|
3
|
+
"description": "Unified Runtime Validation and Inference Driver for TypeScript",
|
|
4
|
+
"version": "0.8.0",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"json-schema",
|
|
8
|
+
"standard-schema"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "sinclairzx81",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/sinclairzx81/typedriver"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@standard-schema/spec": "^1.0.0",
|
|
18
|
+
"typebox": "^1.0.53"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"types": "./build/index.d.mts",
|
|
22
|
+
"module": "./build/index.mjs",
|
|
23
|
+
"exports": {
|
|
24
|
+
"./standard-schema": {
|
|
25
|
+
"import": "./build/standard-schema/index.mjs",
|
|
26
|
+
"default": "./build/standard-schema/index.mjs"
|
|
27
|
+
},
|
|
28
|
+
"./json-schema": {
|
|
29
|
+
"import": "./build/json-schema/index.mjs",
|
|
30
|
+
"default": "./build/json-schema/index.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./typescript": {
|
|
33
|
+
"import": "./build/typescript/index.mjs",
|
|
34
|
+
"default": "./build/typescript/index.mjs"
|
|
35
|
+
},
|
|
36
|
+
".": {
|
|
37
|
+
"import": "./build/index.mjs",
|
|
38
|
+
"default": "./build/index.mjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"typesVersions": {
|
|
42
|
+
"*": {
|
|
43
|
+
"standard-schema": [
|
|
44
|
+
"./build/standard-schema/index.d.mts"
|
|
45
|
+
],
|
|
46
|
+
"json-schema": [
|
|
47
|
+
"./build/json-schema/index.d.mts"
|
|
48
|
+
],
|
|
49
|
+
"typescript": [
|
|
50
|
+
"./build/typescript/index.d.mts"
|
|
51
|
+
],
|
|
52
|
+
".": [
|
|
53
|
+
"./build/index.d.mts"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
<div align='center'>
|
|
2
|
+
|
|
3
|
+
<h1>TypeDriver</h1>
|
|
4
|
+
|
|
5
|
+
<p>Unified Runtime Validation and Inference Driver for TypeScript</p>
|
|
6
|
+
|
|
7
|
+
<img src="typedriver.png" />
|
|
8
|
+
|
|
9
|
+
<br />
|
|
10
|
+
<br />
|
|
11
|
+
|
|
12
|
+
[](https://opensource.org/licenses/MIT) [](https://github.com/sinclairzx81/typedriver/actions/workflows/build.yml)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
Compile, Infer and Parse
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { compile, type Static } from 'typedriver'
|
|
23
|
+
|
|
24
|
+
// Compile
|
|
25
|
+
|
|
26
|
+
const Vector3 = compile(`{
|
|
27
|
+
x: number
|
|
28
|
+
y: number
|
|
29
|
+
z: number
|
|
30
|
+
}`) // const Vector3: Validator<... {
|
|
31
|
+
// x: number,
|
|
32
|
+
// y: number,
|
|
33
|
+
// z: number
|
|
34
|
+
// }>
|
|
35
|
+
|
|
36
|
+
// Infer
|
|
37
|
+
|
|
38
|
+
type Vector3 = Static<typeof Vector3> // type Vector3 = {
|
|
39
|
+
// x: number,
|
|
40
|
+
// y: number,
|
|
41
|
+
// z: number
|
|
42
|
+
// }
|
|
43
|
+
|
|
44
|
+
// Parse
|
|
45
|
+
|
|
46
|
+
const position = Vector3.parse({ // const position: {
|
|
47
|
+
x: 0, // x: number,
|
|
48
|
+
y: 1, // y: number,
|
|
49
|
+
z: 0 // z: number
|
|
50
|
+
}) // } = ...
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Overview
|
|
54
|
+
|
|
55
|
+
TypeDriver is a unified runtime validation and inference system for Json Schema, Standard Schema, and runtime TypeScript. It is designed for framework integration and offers simple Json Schema and Standard Schema integration using a common validation interface.
|
|
56
|
+
|
|
57
|
+
License MIT
|
|
58
|
+
|
|
59
|
+
## Contents
|
|
60
|
+
|
|
61
|
+
- [Overview](#Overview)
|
|
62
|
+
- [Compile](#Compile)
|
|
63
|
+
- [Validator](#Validator)
|
|
64
|
+
- [Assert](#Assert)
|
|
65
|
+
- [Check](#Check)
|
|
66
|
+
- [Parse](#Parse)
|
|
67
|
+
- [Errors](#VaErrorslidator)
|
|
68
|
+
- [Static](#Static)
|
|
69
|
+
- [Schema](#Schema)
|
|
70
|
+
- [Contribute](#Contribute)
|
|
71
|
+
|
|
72
|
+
## Compile
|
|
73
|
+
|
|
74
|
+
TypeDriver provides a single compile(...) function that accepts Json Schema, Standard Schema or TypeScript definition (expressed as a string). The function returns a Validator instance that can be used to check, parse and assert values.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { compile } from 'typedriver'
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Use the compile function can compile TypeScript types ...
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const Vector3 = compile(`{
|
|
84
|
+
x: number
|
|
85
|
+
y: number
|
|
86
|
+
z: number
|
|
87
|
+
}`) // const Vector3: Validator<..., {
|
|
88
|
+
// x: number,
|
|
89
|
+
// y: number,
|
|
90
|
+
// z: number,
|
|
91
|
+
// }>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
... or Json Schema schematics
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const Vector3 = compile({ // const Vector3: Validator<..., {
|
|
98
|
+
type: 'object', // x: number,
|
|
99
|
+
required: ['x', 'y', 'z'], // y: number,
|
|
100
|
+
properties: { // z: number
|
|
101
|
+
x: { type: 'number' }, // }>
|
|
102
|
+
y: { type: 'number' },
|
|
103
|
+
z: { type: 'number' }
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
... or libraries that implement Standard Schema.
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import * z from 'zod'
|
|
112
|
+
|
|
113
|
+
const Vector3 = compile(z.object({ // const Vector3: Validator<..., {
|
|
114
|
+
x: z.number(), // x: number,
|
|
115
|
+
y: z.number(), // y: number,
|
|
116
|
+
z: z.number(), // z: number
|
|
117
|
+
})) // }>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Validator
|
|
121
|
+
|
|
122
|
+
Validators contain functions to assert, parse and check JavaScript values.
|
|
123
|
+
|
|
124
|
+
### Assert
|
|
125
|
+
|
|
126
|
+
The assert(...) function will throw if the value is invalid.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
// Vector3.assert(value: unknown): asserts value is Vector3
|
|
130
|
+
|
|
131
|
+
Vector3.assert(value)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Check
|
|
135
|
+
|
|
136
|
+
The check(...) returns a safe boolean result.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
// Vector3.check(value: unknown): value is Vector3
|
|
140
|
+
|
|
141
|
+
if(Vector3.check(value)) {
|
|
142
|
+
|
|
143
|
+
const { x, y, z } = value // safe
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Parse
|
|
148
|
+
|
|
149
|
+
The parse(...) function checks and returns a value and throws on error.
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
// Vector3.parse(value: unknown): Vector3
|
|
153
|
+
|
|
154
|
+
const { x, y, z } = Vector3.parse(value)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Errors
|
|
158
|
+
|
|
159
|
+
The errors(...) function returns validation errors for a value.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
// Vector3.errors(value: unknown): object[]
|
|
163
|
+
|
|
164
|
+
const errors = Vector3.errors(value)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Static
|
|
168
|
+
|
|
169
|
+
TypeDriver provides a unified inference type called `Static<T>` that can infer types from Validator instances as well as Json Schema, Standard Schema or TypeScript syntax. The following infers a Validator.
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { compile, type Static } from 'typedriver'
|
|
173
|
+
|
|
174
|
+
const Vector = compile(`{
|
|
175
|
+
x: number,
|
|
176
|
+
y: number,
|
|
177
|
+
z: number
|
|
178
|
+
}`)
|
|
179
|
+
|
|
180
|
+
type Vector = Static<typeof Vector> // type Vector = {
|
|
181
|
+
// x: number
|
|
182
|
+
// y: number
|
|
183
|
+
// z: number
|
|
184
|
+
// }
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Schema
|
|
188
|
+
|
|
189
|
+
Validator instances provide access to internal schematics if required. They also provide support for Validator to Json Schema translation in some cases, with fallbacks for manual Json Schema transformation if required.
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { compile, type Static } from 'typedriver'
|
|
193
|
+
|
|
194
|
+
const validator = compile(...)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
validator.isJsonSchema() // Returns true if the validator can be converted to
|
|
198
|
+
// Json Schema. This is true when the validator was
|
|
199
|
+
// compiled with Json Schema or TypeScript, but false
|
|
200
|
+
// if it was compiled with Standard Schema.
|
|
201
|
+
|
|
202
|
+
validator.asJsonSchema() // Returns the Json Schema for the validator. If the
|
|
203
|
+
// validator was compiled with Standard Schema, an
|
|
204
|
+
// empty {} is returned to indicate an unknown
|
|
205
|
+
// runtime schema.
|
|
206
|
+
|
|
207
|
+
validator.schema() // Returns the schema that was passed to compile. This
|
|
208
|
+
// can be used to manually transform the schema if
|
|
209
|
+
// isJsonSchema() returns false.
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Contribute
|
|
213
|
+
|
|
214
|
+
TypeDriver is open to community contribution. Please ensure you submit an issue before submitting a pull request. This project prefers open community discussion before accepting new features.
|