typedriver 0.8.4 → 0.8.6
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.mjs +4 -4
- package/build/errors/error-to-issue.d.mts +3 -0
- package/build/errors/error-to-issue.mjs +14 -0
- package/build/{errors.d.mts → errors/exceptions.d.mts} +6 -1
- package/build/{errors.mjs → errors/exceptions.mjs} +8 -1
- package/build/errors/index.d.mts +3 -0
- package/build/errors/index.mjs +4 -0
- package/build/errors/issue-to-error.d.mts +3 -0
- package/build/errors/issue-to-error.mjs +20 -0
- package/build/index.d.mts +3 -4
- package/build/index.mjs +8 -4
- package/build/validator.d.mts +32 -1
- package/build/validator.mjs +17 -0
- package/build/validators/json-schema/validator.d.mts +14 -0
- package/build/{json-schema → validators/json-schema}/validator.mjs +12 -23
- package/build/{standard-json-schema → validators/standard-json-schema}/resolve.d.mts +1 -1
- package/build/{standard-json-schema → validators/standard-json-schema}/resolve.mjs +1 -0
- package/build/validators/standard-json-schema/validator.d.mts +14 -0
- package/build/{standard-json-schema → validators/standard-json-schema}/validator.mjs +12 -18
- package/build/{standard-schema → validators/standard-schema}/validator.d.mts +3 -3
- package/build/{standard-schema → validators/standard-schema}/validator.mjs +10 -8
- package/build/{typescript → validators/typescript}/validator.d.mts +2 -2
- package/build/{typescript → validators/typescript}/validator.mjs +12 -6
- package/package.json +7 -28
- package/readme.md +191 -64
- package/build/json-schema/index.d.mts +0 -1
- package/build/json-schema/index.mjs +0 -1
- package/build/json-schema/validator.d.mts +0 -31
- package/build/locale.d.mts +0 -6
- package/build/locale.mjs +0 -7
- package/build/standard-json-schema/index.d.mts +0 -1
- package/build/standard-json-schema/index.mjs +0 -1
- package/build/standard-json-schema/validator.d.mts +0 -26
- package/build/standard-schema/index.d.mts +0 -1
- package/build/standard-schema/index.mjs +0 -1
- package/build/typescript/index.d.mts +0 -1
- package/build/typescript/index.mjs +0 -1
package/build/compile.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { IsJsonSchema, IsStandardSchemaV1, IsStandardJsonSchemaV1, IsTypeScript } from './guard/index.mjs';
|
|
3
|
-
import { JsonSchemaValidator } from './json-schema/validator.mjs';
|
|
4
|
-
import { StandardJsonSchemaValidator } from './standard-json-schema/validator.mjs';
|
|
5
|
-
import { StandardSchemaValidator } from './standard-schema/validator.mjs';
|
|
6
|
-
import { TypeScriptValidator } from './typescript/validator.mjs';
|
|
3
|
+
import { JsonSchemaValidator } from './validators/json-schema/validator.mjs';
|
|
4
|
+
import { StandardJsonSchemaValidator } from './validators/standard-json-schema/validator.mjs';
|
|
5
|
+
import { StandardSchemaValidator } from './validators/standard-schema/validator.mjs';
|
|
6
|
+
import { TypeScriptValidator } from './validators/typescript/validator.mjs';
|
|
7
7
|
function FromStandardSchema(schema) {
|
|
8
8
|
return new StandardSchemaValidator(schema);
|
|
9
9
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from 'typebox/guard';
|
|
3
|
+
// ------------------------------------------------------------------
|
|
4
|
+
// Issues
|
|
5
|
+
// ------------------------------------------------------------------
|
|
6
|
+
function pathSegments(pointer) {
|
|
7
|
+
if (Guard.IsEqual(pointer.length, 0))
|
|
8
|
+
return [];
|
|
9
|
+
return pointer.slice(1).split('/').map((segment) => segment.replace(/~1/g, '/').replace(/~0/g, '~'));
|
|
10
|
+
}
|
|
11
|
+
export function errorToIssue(error) {
|
|
12
|
+
const path = pathSegments(error.instancePath);
|
|
13
|
+
return { path, message: error.message };
|
|
14
|
+
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
export declare class ParseError extends globalThis.Error {
|
|
2
|
+
value: unknown;
|
|
2
3
|
readonly errors: object[];
|
|
3
|
-
|
|
4
|
+
readonly cause: {
|
|
5
|
+
errors: object[];
|
|
6
|
+
value: unknown;
|
|
7
|
+
};
|
|
8
|
+
constructor(value: unknown, errors: object[]);
|
|
4
9
|
}
|
|
5
10
|
export declare class UnknownError extends globalThis.Error {
|
|
6
11
|
constructor(message: string);
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
export class ParseError extends globalThis.Error {
|
|
3
|
-
constructor(errors) {
|
|
3
|
+
constructor(value, errors) {
|
|
4
4
|
super('ParseError');
|
|
5
|
+
this.value = value;
|
|
5
6
|
this.errors = errors;
|
|
7
|
+
Object.defineProperty(this, 'cause', {
|
|
8
|
+
value: { errors, value },
|
|
9
|
+
writable: false,
|
|
10
|
+
configurable: false,
|
|
11
|
+
enumerable: false
|
|
12
|
+
});
|
|
6
13
|
}
|
|
7
14
|
}
|
|
8
15
|
export class UnknownError extends globalThis.Error {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from 'typebox/guard';
|
|
3
|
+
// ------------------------------------------------------------------
|
|
4
|
+
// Issues
|
|
5
|
+
// ------------------------------------------------------------------
|
|
6
|
+
function jsonPointer(segments) {
|
|
7
|
+
return `#${segments.join('/')}`;
|
|
8
|
+
}
|
|
9
|
+
export function issueToError(issue) {
|
|
10
|
+
const instancePath = Guard.HasPropertyKey(issue, 'path') && Guard.IsArray(issue.path)
|
|
11
|
+
? jsonPointer(issue.path)
|
|
12
|
+
: jsonPointer([]);
|
|
13
|
+
return {
|
|
14
|
+
instancePath,
|
|
15
|
+
schemaPath: '#',
|
|
16
|
+
keyword: 'type',
|
|
17
|
+
message: issue.message,
|
|
18
|
+
params: { type: 'standard-schema' }
|
|
19
|
+
};
|
|
20
|
+
}
|
package/build/index.d.mts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export * from './static.mjs';
|
|
1
|
+
export { type TCompile, compile } from './compile.mjs';
|
|
2
|
+
export { type Static } from './static.mjs';
|
|
3
|
+
export { Validator, type TErrorFormat, type TErrorLocale, type TErrorOptions, type TErrorResult } from './validator.mjs';
|
package/build/index.mjs
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
2
|
+
// ------------------------------------------------------------------
|
|
3
|
+
// Compile
|
|
4
|
+
// ------------------------------------------------------------------
|
|
5
|
+
export { compile } from './compile.mjs';
|
|
6
|
+
// ------------------------------------------------------------------
|
|
7
|
+
// Validator
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
export { Validator } from './validator.mjs';
|
package/build/validator.d.mts
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
+
import { System } from 'typebox/system';
|
|
2
|
+
import { TLocalizedValidationError } from 'typebox/error';
|
|
3
|
+
import { StandardSchemaV1 } from './_standard/standard-schema.mjs';
|
|
4
|
+
export type TErrorFormat = 'json-schema' | 'standard-schema';
|
|
5
|
+
declare const Locale: typeof System.Locale;
|
|
6
|
+
export type TErrorLocale = (Exclude<keyof typeof Locale, 'Get' | 'Set' | 'Reset'> & ({} & string));
|
|
7
|
+
/** Internal */
|
|
8
|
+
export declare function resolveErrorOptions(options?: Partial<TErrorOptions>): TErrorOptions;
|
|
9
|
+
export interface TErrorOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Specifies the error message generation format.
|
|
12
|
+
*
|
|
13
|
+
* Supported values are `json-schema` and `standard-schema`.
|
|
14
|
+
*
|
|
15
|
+
* Default: `json-schema`
|
|
16
|
+
*/
|
|
17
|
+
format: TErrorFormat;
|
|
18
|
+
/**
|
|
19
|
+
* Specifies the locale used when generating error messages.
|
|
20
|
+
*
|
|
21
|
+
* This setting applies only when used with TypeScript, JSON Schema,
|
|
22
|
+
* or implementations that follow the Standard JSON Schema specification.
|
|
23
|
+
* When the compiled type is Standard Schema only, the error message
|
|
24
|
+
* will be whatever is returned from that library.
|
|
25
|
+
*
|
|
26
|
+
* Default: `en_US`
|
|
27
|
+
*/
|
|
28
|
+
locale: TErrorLocale;
|
|
29
|
+
}
|
|
30
|
+
export type TErrorResult<Options extends Partial<TErrorOptions>> = (Options['format'] extends 'standard-schema' ? StandardSchemaV1.Issue[] : TLocalizedValidationError[]);
|
|
1
31
|
/** Abstract Base for all Validator types. */
|
|
2
32
|
export declare abstract class Validator<Input extends unknown = unknown, Output extends unknown = unknown> {
|
|
3
33
|
/** Returns the schema used to construct this validator */
|
|
@@ -7,7 +37,7 @@ export declare abstract class Validator<Input extends unknown = unknown, Output
|
|
|
7
37
|
/** Parses a value and throws if invalid */
|
|
8
38
|
abstract parse(value: unknown): Output;
|
|
9
39
|
/** Returns errors for the given value */
|
|
10
|
-
abstract errors(value: unknown):
|
|
40
|
+
abstract errors<Options extends Partial<TErrorOptions>>(value: unknown, options?: Options): TErrorResult<Options>;
|
|
11
41
|
/** True if the validator has a Json Schema representation */
|
|
12
42
|
abstract isJsonSchema(): boolean;
|
|
13
43
|
/** Return the validator Json Schema representation. */
|
|
@@ -15,3 +45,4 @@ export declare abstract class Validator<Input extends unknown = unknown, Output
|
|
|
15
45
|
/** Returns true if this validator is JIT accelerated */
|
|
16
46
|
abstract isAccelerated(): boolean;
|
|
17
47
|
}
|
|
48
|
+
export {};
|
package/build/validator.mjs
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { System } from 'typebox/system';
|
|
3
|
+
// ------------------------------------------------------------------
|
|
4
|
+
// TErrorLocale
|
|
5
|
+
// ------------------------------------------------------------------
|
|
6
|
+
const Locale = System.Locale;
|
|
7
|
+
// ------------------------------------------------------------------
|
|
8
|
+
// TErrorOptions
|
|
9
|
+
// ------------------------------------------------------------------
|
|
10
|
+
/** Internal */
|
|
11
|
+
export function resolveErrorOptions(options) {
|
|
12
|
+
const defaults = { format: 'json-schema', locale: 'en_US' };
|
|
13
|
+
const resolved = options ?? {};
|
|
14
|
+
return { ...defaults, ...resolved };
|
|
15
|
+
}
|
|
16
|
+
// ------------------------------------------------------------------
|
|
17
|
+
// Validator
|
|
18
|
+
// ------------------------------------------------------------------
|
|
2
19
|
/** Abstract Base for all Validator types. */
|
|
3
20
|
export class Validator {
|
|
4
21
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Validator, type TErrorOptions, type TErrorResult } 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
|
+
toJsonSchema(): unknown;
|
|
10
|
+
isAccelerated(): boolean;
|
|
11
|
+
check(value: unknown): value is Output;
|
|
12
|
+
parse(value: unknown): Output;
|
|
13
|
+
errors<Options extends Partial<TErrorOptions>>(value: unknown, options?: Options): TErrorResult<Options>;
|
|
14
|
+
}
|
|
@@ -1,24 +1,8 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { System } from 'typebox/system';
|
|
2
3
|
import { Validator as TBValidator } from 'typebox/compile';
|
|
3
|
-
import { ParseError } from '
|
|
4
|
-
import { Validator } from '
|
|
5
|
-
/**
|
|
6
|
-
* High-performance Json Schema validator that uses library-specific
|
|
7
|
-
* inference mechanisms. The validator assumes the source library
|
|
8
|
-
* produces accurate schematics that encode the runtime
|
|
9
|
-
* representations of its types.
|
|
10
|
-
*
|
|
11
|
-
* In TypeBox terminology, this falls under the TUnsafe<T> category.
|
|
12
|
-
* Preferably, TypeScript types "should" be derived from the
|
|
13
|
-
* schematics rather than assumed.
|
|
14
|
-
*
|
|
15
|
-
* Note:
|
|
16
|
-
*
|
|
17
|
-
* Standard JSON Schema does not advertise which Draft versions it
|
|
18
|
-
* supports, and the resolver is using try/catch resolution. This
|
|
19
|
-
* should be brought up in RFC feedback, not by me of course, I don't
|
|
20
|
-
* know anything about Json Schema.
|
|
21
|
-
*/
|
|
4
|
+
import { ParseError, errorToIssue } from '../../errors/index.mjs';
|
|
5
|
+
import { Validator, resolveErrorOptions } from '../../validator.mjs';
|
|
22
6
|
export class JsonSchemaValidator extends Validator {
|
|
23
7
|
constructor(input) {
|
|
24
8
|
super();
|
|
@@ -41,7 +25,7 @@ export class JsonSchemaValidator extends Validator {
|
|
|
41
25
|
return this.input;
|
|
42
26
|
}
|
|
43
27
|
// ----------------------------------------------------------------
|
|
44
|
-
//
|
|
28
|
+
// Acceleration
|
|
45
29
|
// ----------------------------------------------------------------
|
|
46
30
|
isAccelerated() {
|
|
47
31
|
return this.validator.IsEvaluated();
|
|
@@ -54,10 +38,15 @@ export class JsonSchemaValidator extends Validator {
|
|
|
54
38
|
}
|
|
55
39
|
parse(value) {
|
|
56
40
|
if (!this.validator.Check(value))
|
|
57
|
-
throw new ParseError(this.errors(value));
|
|
41
|
+
throw new ParseError(value, this.errors(value));
|
|
58
42
|
return value;
|
|
59
43
|
}
|
|
60
|
-
errors(value) {
|
|
61
|
-
|
|
44
|
+
errors(value, options) {
|
|
45
|
+
const config = resolveErrorOptions(options);
|
|
46
|
+
System.Locale.Set(System.Locale[config.locale]);
|
|
47
|
+
const errors = this.validator.Errors(value);
|
|
48
|
+
return (config.format === 'standard-schema'
|
|
49
|
+
? errors.map(error => errorToIssue(error))
|
|
50
|
+
: errors);
|
|
62
51
|
}
|
|
63
52
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { StandardJSONSchemaV1 } from '
|
|
1
|
+
import { StandardJSONSchemaV1 } from '../../_standard/standard-schema.mjs';
|
|
2
2
|
export declare function ResolveJsonSchema(input: StandardJSONSchemaV1): Record<string, unknown>;
|
|
@@ -24,6 +24,7 @@ function AsDraft2020_12(input) {
|
|
|
24
24
|
return undefined;
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
+
// Standard JSON Schema should provide mechanism to query supported specifications
|
|
27
28
|
export function ResolveJsonSchema(input) {
|
|
28
29
|
const jsonschema = AsDraft2020_12(input) ?? AsDraft7(input) ?? AsOpenAPI3_0(input);
|
|
29
30
|
if (Guard.IsUndefined(jsonschema))
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from '../../_standard/standard-schema.mjs';
|
|
2
|
+
import { Validator, type TErrorOptions, type TErrorResult } from '../../validator.mjs';
|
|
3
|
+
export declare class StandardJsonSchemaValidator<Input extends StandardJSONSchemaV1 & StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferOutput<Input>> extends Validator<Input, Output> {
|
|
4
|
+
private readonly input;
|
|
5
|
+
private readonly validator;
|
|
6
|
+
constructor(input: Input);
|
|
7
|
+
schema(): Input;
|
|
8
|
+
isJsonSchema(): boolean;
|
|
9
|
+
toJsonSchema(): unknown;
|
|
10
|
+
isAccelerated(): boolean;
|
|
11
|
+
check(value: unknown): value is Output;
|
|
12
|
+
parse(value: unknown): Output;
|
|
13
|
+
errors<Options extends Partial<TErrorOptions>>(value: unknown, options?: Options): TErrorResult<Options>;
|
|
14
|
+
}
|
|
@@ -1,20 +1,9 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
3
|
-
import { Validator } from '
|
|
2
|
+
import { System } from 'typebox/system';
|
|
3
|
+
import { Validator, resolveErrorOptions } from '../../validator.mjs';
|
|
4
|
+
import { ParseError, errorToIssue } from '../../errors/index.mjs';
|
|
4
5
|
import { Validator as TBValidator } from 'typebox/compile';
|
|
5
6
|
import { ResolveJsonSchema } from './resolve.mjs';
|
|
6
|
-
/**
|
|
7
|
-
* High-performance Json Schema validator that uses library-specific
|
|
8
|
-
* inference mechanisms. The validator assumes the source library
|
|
9
|
-
* produces accurate schematics that encode the runtime
|
|
10
|
-
* representations of its types.
|
|
11
|
-
*
|
|
12
|
-
* Note:
|
|
13
|
-
*
|
|
14
|
-
* Standard JSON Schema does not advertise which Draft versions it
|
|
15
|
-
* supports, and the resolver is using try/catch resolution. This
|
|
16
|
-
* should be brought up in RFC feedback.
|
|
17
|
-
*/
|
|
18
7
|
export class StandardJsonSchemaValidator extends Validator {
|
|
19
8
|
constructor(input) {
|
|
20
9
|
super();
|
|
@@ -38,7 +27,7 @@ export class StandardJsonSchemaValidator extends Validator {
|
|
|
38
27
|
return this.validator.Type();
|
|
39
28
|
}
|
|
40
29
|
// ----------------------------------------------------------------
|
|
41
|
-
//
|
|
30
|
+
// Acceleration
|
|
42
31
|
// ----------------------------------------------------------------
|
|
43
32
|
isAccelerated() {
|
|
44
33
|
return this.validator.IsEvaluated();
|
|
@@ -51,10 +40,15 @@ export class StandardJsonSchemaValidator extends Validator {
|
|
|
51
40
|
}
|
|
52
41
|
parse(value) {
|
|
53
42
|
if (!this.validator.Check(value))
|
|
54
|
-
throw new ParseError(this.errors(value));
|
|
43
|
+
throw new ParseError(value, this.errors(value));
|
|
55
44
|
return value;
|
|
56
45
|
}
|
|
57
|
-
errors(value) {
|
|
58
|
-
|
|
46
|
+
errors(value, options) {
|
|
47
|
+
const config = resolveErrorOptions(options);
|
|
48
|
+
System.Locale.Set(System.Locale[config.locale]);
|
|
49
|
+
const errors = this.validator.Errors(value);
|
|
50
|
+
return (config.format === 'standard-schema'
|
|
51
|
+
? errors.map(error => errorToIssue(error))
|
|
52
|
+
: errors);
|
|
59
53
|
}
|
|
60
54
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { StandardSchemaV1 } from '
|
|
2
|
-
import { Validator } from '
|
|
1
|
+
import { StandardSchemaV1 } from '../../_standard/standard-schema.mjs';
|
|
2
|
+
import { Validator, type TErrorOptions, type TErrorResult } from '../../validator.mjs';
|
|
3
3
|
export declare class StandardSchemaValidator<Input extends StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferOutput<Input>> extends Validator<Input, Output> {
|
|
4
4
|
private readonly input;
|
|
5
5
|
constructor(input: Input);
|
|
@@ -9,5 +9,5 @@ export declare class StandardSchemaValidator<Input extends StandardSchemaV1, Out
|
|
|
9
9
|
isAccelerated(): boolean;
|
|
10
10
|
check(value: unknown): value is Output;
|
|
11
11
|
parse(value: unknown): Output;
|
|
12
|
-
errors(value: unknown):
|
|
12
|
+
errors<Options extends Partial<TErrorOptions>>(value: unknown, options?: Options): TErrorResult<Options>;
|
|
13
13
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import { ParseError, UnknownError } from '
|
|
3
|
-
import { Validator } from '
|
|
2
|
+
import { ParseError, UnknownError, issueToError } from '../../errors/index.mjs';
|
|
3
|
+
import { Validator, resolveErrorOptions } from '../../validator.mjs';
|
|
4
4
|
export class StandardSchemaValidator extends Validator {
|
|
5
5
|
constructor(input) {
|
|
6
6
|
super();
|
|
@@ -22,7 +22,7 @@ export class StandardSchemaValidator extends Validator {
|
|
|
22
22
|
return {};
|
|
23
23
|
}
|
|
24
24
|
// ----------------------------------------------------------------
|
|
25
|
-
//
|
|
25
|
+
// Acceleration
|
|
26
26
|
// ----------------------------------------------------------------
|
|
27
27
|
isAccelerated() {
|
|
28
28
|
return false;
|
|
@@ -37,15 +37,17 @@ export class StandardSchemaValidator extends Validator {
|
|
|
37
37
|
parse(value) {
|
|
38
38
|
const result = this.input['~standard'].validate(value);
|
|
39
39
|
if ('issues' in result)
|
|
40
|
-
throw new ParseError(result.issues
|
|
40
|
+
throw new ParseError(value, result.issues ?? []);
|
|
41
41
|
if ('value' in result)
|
|
42
42
|
return result.value;
|
|
43
43
|
throw new UnknownError('Invalid result');
|
|
44
44
|
}
|
|
45
|
-
errors(value) {
|
|
45
|
+
errors(value, options) {
|
|
46
46
|
const result = this.input['~standard'].validate(value);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return
|
|
47
|
+
const issues = (('issues' in result) ? result.issues : []);
|
|
48
|
+
const config = resolveErrorOptions(options);
|
|
49
|
+
return (config.format === 'json-schema'
|
|
50
|
+
? issues.map(issue => issueToError(issue))
|
|
51
|
+
: issues.map(issue => ({ path: issue.path, message: issue.message })));
|
|
50
52
|
}
|
|
51
53
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Validator } from '
|
|
1
|
+
import { Validator, type TErrorOptions, type TErrorResult } from '../../validator.mjs';
|
|
2
2
|
import Type from 'typebox';
|
|
3
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
4
|
private readonly validator;
|
|
@@ -11,5 +11,5 @@ export declare class TypeScriptValidator<Input extends string, Schema extends Ty
|
|
|
11
11
|
isAccelerated(): boolean;
|
|
12
12
|
check(value: unknown): value is Output;
|
|
13
13
|
parse(value: unknown): Output;
|
|
14
|
-
errors(value: unknown):
|
|
14
|
+
errors<Options extends Partial<TErrorOptions>>(value: unknown, options?: Options): TErrorResult<Options>;
|
|
15
15
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { System } from 'typebox/system';
|
|
2
3
|
import { Validator as TBValidator } from 'typebox/compile';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { ParseError, errorToIssue } from '../../errors/index.mjs';
|
|
5
|
+
import { Validator, resolveErrorOptions } from '../../validator.mjs';
|
|
5
6
|
import Type from 'typebox';
|
|
6
7
|
export class TypeScriptValidator extends Validator {
|
|
7
8
|
constructor(script) {
|
|
@@ -26,7 +27,7 @@ export class TypeScriptValidator extends Validator {
|
|
|
26
27
|
return this.jsonschema;
|
|
27
28
|
}
|
|
28
29
|
// ----------------------------------------------------------------
|
|
29
|
-
//
|
|
30
|
+
// Acceleration
|
|
30
31
|
// ----------------------------------------------------------------
|
|
31
32
|
isAccelerated() {
|
|
32
33
|
return this.validator.IsEvaluated();
|
|
@@ -39,10 +40,15 @@ export class TypeScriptValidator extends Validator {
|
|
|
39
40
|
}
|
|
40
41
|
parse(value) {
|
|
41
42
|
if (!this.validator.Check(value))
|
|
42
|
-
throw new ParseError(this.errors(value));
|
|
43
|
+
throw new ParseError(value, this.errors(value));
|
|
43
44
|
return value;
|
|
44
45
|
}
|
|
45
|
-
errors(value) {
|
|
46
|
-
|
|
46
|
+
errors(value, options) {
|
|
47
|
+
const config = resolveErrorOptions(options);
|
|
48
|
+
System.Locale.Set(System.Locale[config.locale]);
|
|
49
|
+
const errors = this.validator.Errors(value);
|
|
50
|
+
return (config.format === 'standard-schema'
|
|
51
|
+
? errors.map(error => errorToIssue(error))
|
|
52
|
+
: errors);
|
|
47
53
|
}
|
|
48
54
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typedriver",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "0.8.
|
|
3
|
+
"description": "High Performance Driver for Runtime Type System Integration",
|
|
4
|
+
"version": "0.8.6",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
7
7
|
"json-schema",
|
|
@@ -20,25 +20,13 @@
|
|
|
20
20
|
"types": "./build/index.d.mts",
|
|
21
21
|
"module": "./build/index.mjs",
|
|
22
22
|
"exports": {
|
|
23
|
-
"./standard-schema": {
|
|
24
|
-
"import": "./build/standard-schema/index.mjs",
|
|
25
|
-
"default": "./build/standard-schema/index.mjs"
|
|
26
|
-
},
|
|
27
23
|
"./guard": {
|
|
28
24
|
"import": "./build/guard/index.mjs",
|
|
29
25
|
"default": "./build/guard/index.mjs"
|
|
30
26
|
},
|
|
31
|
-
"./
|
|
32
|
-
"import": "./build/
|
|
33
|
-
"default": "./build/
|
|
34
|
-
},
|
|
35
|
-
"./standard-json-schema": {
|
|
36
|
-
"import": "./build/standard-json-schema/index.mjs",
|
|
37
|
-
"default": "./build/standard-json-schema/index.mjs"
|
|
38
|
-
},
|
|
39
|
-
"./typescript": {
|
|
40
|
-
"import": "./build/typescript/index.mjs",
|
|
41
|
-
"default": "./build/typescript/index.mjs"
|
|
27
|
+
"./errors": {
|
|
28
|
+
"import": "./build/errors/index.mjs",
|
|
29
|
+
"default": "./build/errors/index.mjs"
|
|
42
30
|
},
|
|
43
31
|
".": {
|
|
44
32
|
"import": "./build/index.mjs",
|
|
@@ -47,20 +35,11 @@
|
|
|
47
35
|
},
|
|
48
36
|
"typesVersions": {
|
|
49
37
|
"*": {
|
|
50
|
-
"standard-schema": [
|
|
51
|
-
"./build/standard-schema/index.d.mts"
|
|
52
|
-
],
|
|
53
38
|
"guard": [
|
|
54
39
|
"./build/guard/index.d.mts"
|
|
55
40
|
],
|
|
56
|
-
"
|
|
57
|
-
"./build/
|
|
58
|
-
],
|
|
59
|
-
"standard-json-schema": [
|
|
60
|
-
"./build/standard-json-schema/index.d.mts"
|
|
61
|
-
],
|
|
62
|
-
"typescript": [
|
|
63
|
-
"./build/typescript/index.d.mts"
|
|
41
|
+
"errors": [
|
|
42
|
+
"./build/errors/index.d.mts"
|
|
64
43
|
],
|
|
65
44
|
".": [
|
|
66
45
|
"./build/index.d.mts"
|
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<h1>TypeDriver</h1>
|
|
4
4
|
|
|
5
|
-
<p>
|
|
5
|
+
<p>High Performance Driver for Runtime Type System Integration</p>
|
|
6
6
|
|
|
7
7
|
<img src="typedriver.png" />
|
|
8
8
|
|
|
@@ -23,10 +23,7 @@ $ npm install typedriver
|
|
|
23
23
|
|
|
24
24
|
## Example
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Ref: [TypeScript](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoAqBPADgUwMoDGATgJbYAuqYStd9D4ypAttgPbEWgDeoh7NqQA2uUAF9QAM2KDQAcgo5cAEzIA3XMXnJkAgHYBnbgDVchCpwDMoALz9B2EbgAUAAx7JQoAB4AuUH0AVxYAIy0AGi9QTADgsMjogC84kPDiZHE3AEpdFXNhAENiMQNjUHVC4SDcAKD9AGt9dgB3fV0y7g5DUgpSdn07UDMLawA6bGLDV0rq3GzvRaXFtE7Qbt7+-QDPZb39g8OjpbRvf0C0xOPrm5vTmNSE4ijb17eTsG8Ui6fo9--rmhJPYxqCgA) | [JSON Schema](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhqmEvQ40+MgJYUAO2ATgC6gBvUEWydWAGzKgAvqABm3UaADkvAJ4cyAE26sAbmW7LkyEbkz8AamSK8eAZlABeYaI4SyACgHJQodZoAXCrYAEYAVja8ygA0vqDcZACOAK6siVrBANrKAB6xKmoFygBeygC6cX4cipp8rGSYwT5+frnN-hpkwcq4KRShhsoyVa1qHQHdKn0DQyPxfiUTXT0zg0Yy8dLI0gCUJlo24lSJrub8elTiKVMpuADWuNgA7rgmZhagXJisvKx4zlA1lsDgAdBwTpgvJdrmRdq0EYi0B9+N9fv9cM0Foicbi8fiCaA0G1gmtDKNCZSqYTiaBxqAydwKdSWayiWBFqT+utsWy+QS0LIXKCRUA) | [TypeBox](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhgFygAqAngA5kBC2AHqmEn-wMHhkASwrNsAJwAuoAN6gi2cSIA2ZUAF9QAM0nLQAcmksyAE0kiAbmUmHR4qbKatd+ikZOsARl0OgA5GQlXExZADUyImkpAGZQAF5FZWY1MgAKFzIAOgB5bwArKOl0uWQAzjos7IA5AFcKb1t0gEoAGnLQRirTWoamyVaOgIAvHtY+xuaW5E0WmeQzKNUqSQ0QsNArKlU6sjo63ABrXGwAd1wgjdkJTBFpETxE0EjouOzmVcwM7d2yFoCgKBgLQ11At3uj1wdDKwLh8IRiKRQLQFTouH6tmGyJxuJxqK66MxkmxeLJ5ICBLGoAxU0knQpjPxYG0SWy7KAA) | [Zod](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygBaA9vqmEl9z7+MgEsaABxZFMoAN6gyLEQIA2VUAF9QAMyJzQAckwBPYVXxEBANypEdyQSLESAVKGwBnUAC8NWmrvdtryLK4LhIAalRkmGIAzKAAvDJywopUABTuAHQsAEYAVhGYqZLIoKAAHoyZuACuNNmWqQCUADQloPqVGTV1DS1t7p3d9URNyCqNjTb4EQrEykEhoGbYCtVUjNW4ANa4LADuuDYLEqIuApgCLLjxoOGRMRnCxC5py6tUjaVf319ox6Cnc6XXCMYo-cEQyFQ6HfNClCqgIaWVow1FotFw9qMJFEFHo-EE35gUoDRG1YZtQlU1FoNQJDIMoA) | [Valibot](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygBq2ANgJYBGA9pqmEkFDhI8MnY0ADtyKZQAb1BluU9qyqgAvqABmRFaADkmAJ6Sq+IuwBuVIoeTipMuQCpQ2AM6hru-TSNrNi5eB2RlXE85JioyTBkAZlAAXiUVSTUqAAprADpuTgArWMws+WRQUAAPRjzcAFcaTjssgEoAGgrQE1rchqaWjq6AL17+5qI25E1W1sd8WNZiDQionzZ6qkZ63ABrXG4Ad1xHVblpT3ZMdm5cFOYSxNzJYk9soNZN1sqf35+0M6gC5XG64RjlP6QqHQmGw35oSo1UDjOydOHojEYhHdRgoohozGEon-MCVUbIxoTLrEmnotDaVK5JlAA) | [ArkType](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCCRA1gCoCeADlamEkFDhI8MgCWNbgHsimUAG9QZaVPEAbKqAC+oAGZFVoAOSYeVfEXEA3KkWPIJU2fKVneO-YZonird1QOyCq4AM7yAGpUZJiyAMygALzKqtwaVAAUARkKyKCgAB6MxrgArjQARnbGADR5oJzFZZXVdfkAXk3lVfbI2gCU-Y740erEWiHhoNbY6qVUjKW4rLjSAO64jpPyMqHimOLSuEmgUTHxAHTcxKGZM3NU-fnPL89o26C7+4e4jLmvAMBQOBIJeaHyRVAzR6bVBcPhcPBDUY0LssIRGMx+SRnSh3Ts9SxRMRYF0yQuFKAA) | [Effect](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCiAZi1WZqmEr3-wPDIAljQAOAeyKZQAb1BkJ44QBsqoAL6gWRJaADkmAJ5iq+IsIBuVIvpHipM+ekq1sm7bpoGqbDpjtkRVwAZxkANX8pAGZQAF4FJTFVKgAKF2o6ADowvEISDLdwgEZ012ysIgBXTlTZZFBQAA9GQuyAOSqaACMbABoG0CNW8uwszp7+wYAvEcyxid6iZA0ASnXkZHwOFWJ1YLDQS2wVKqpGKtwAa1wJAHdcTYOZSRDhTGEJXHjQSM4YrJiYghNLHU5UVaNKHQqFoZ6gV7vT64Rj1GHojGYrHY6FoRpEKjYfBfFRGZqMXBdJYDHG0ul0vGgAlEklk4agSmTIg0+m8vmwsD4wnE3Ck0CzDlUmyDfmy2loLQJLLKoA) | [Arri](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCCRRAlqmEtz73+MjY0ADgHsimUAG9QZUSLYAbKqAC+oAGZF5oAOSYAnsKr52ANypFdgkeMkzsazdpp6AAsXZFhZYAGdKWmxrZDlcP0kANSoyTHEAZlAAXll5YSUqAApsADpRACMAKxjMTKlkUFAAD0Zc3ABXGnzLTIBKABoK0ANanIamlo6ugC9e-uaiNuRVVtbkZHwYxWIVMIjQM2xFeqpGetwAa1xRAHdcebXJMT82TDZRXGTQaNiEnOFiPyzN7apWyoBgIBaEuoGut3uuEY5SBsLh8IRiMBaEqNVA40snSR2JxOJR3UYGKIWNxpLJwLAlVG6MaEy65IZ2LQ6hSOTZQA) | [Sury](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFwYCuRAnqmEl9z7+MgEsaABwD2RTKADeoMqJECANlVABfUADMi80AHJMrYVXxEBANypFdgkeMkAqUNgDOGTdpp7nLVrtD+AwKDg-zQAFgA6AGZQAGsAIVAAChoBXFAAalAAcwAvAWEASmRkOVxnSQA1KjJMcRiAXll5YSUqJPQI50pabCSpZH8AD0ZO3CYaACNLABpB0FZRiPGp2fncpZXpojnVQuLkfBrFYhUyitAzbEUmKkYmXFjcUQB3XBLzyTFnAUwBUXSTWqtXqEWExGc7SuNyohRCQTQn1A31+-1wjAG8Kx2JxOLQw0YWzWuJJpOx+IWhIm2zmZLp9NAFI2oCJRHmDI55LA6iaET5QA)
|
|
29
|
-
|
|
26
|
+
Multi-Library Type Compiler and Inference System for High Performance Frameworks
|
|
30
27
|
|
|
31
28
|
```typescript
|
|
32
29
|
import { compile } from 'typedriver'
|
|
@@ -46,26 +43,37 @@ const position = Vector3.parse(value) // const position: {
|
|
|
46
43
|
// } = ...
|
|
47
44
|
```
|
|
48
45
|
|
|
49
|
-
|
|
46
|
+
Supports: [TypeScript](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoAqBPADgUwMoDGATgJbYAuqYStd9D4ypAttgPbEWgDeoh7NqQA2uUAF9QAM2KDQAcgo5cAEzIA3XMXnJkAgHYBnbgDVchCpwDMoALz9B2EbgAUAAx7JQoAB4AuUH0AVxYAIy0AGi9QTADgsMjogC84kPDiZHE3AEpdFXNhAENiMQNjUHVC4SDcAKD9AGt9dgB3fV0y7g5DUgpSdn07UDMLawA6bGLDV0rq3GzvRaXFtE7Qbt7+-QDPZb39g8OjpbRvf0C0xOPrm5vTmNSE4ijb17eTsG8Ui6fo9--rmhJPYxqCgA) | [JSON Schema](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhqmEvQ40+MgJYUAO2ATgC6gBvUEWydWAGzKgAvqABm3UaADkvAJ4cyAE26sAbmW7LkyEbkz8AamSK8eAZlABeYaI4SyACgHJQodZoAXCrYAEYAVja8ygA0vqDcZACOAK6siVrBANrKAB6xKmoFygBeygC6cX4cipp8rGSYwT5+frnN-hpkwcq4KRShhsoyVa1qHQHdKn0DQyPxfiUTXT0zg0Yy8dLI0gCUJlo24lSJrub8elTiKVMpuADWuNgA7rgmZhagXJisvKx4zlA1lsDgAdBwTpgvJdrmRdq0EYi0B9+N9fv9cM0Foicbi8fiCaA0G1gmtDKNCZSqYTiaBxqAydwKdSWayiWBFqT+utsWy+QS0LIXKCRUA) | [TypeBox](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhgFygAqAngA5kBC2AHqmEn-wMHhkASwrNsAJwAuoAN6gi2cSIA2ZUAF9QAM0nLQAcmksyAE0kiAbmUmHR4qbKatd+ikZOsARl0OgA5GQlXExZADUyImkpAGZQAF5FZWY1MgAKFzIAOgB5bwArKOl0uWQAzjos7IA5AFcKb1t0gEoAGnLQRirTWoamyVaOgIAvHtY+xuaW5E0WmeQzKNUqSQ0QsNArKlU6sjo63ABrXGwAd1wgjdkJTBFpETxE0EjouOzmVcwM7d2yFoCgKBgLQ11At3uj1wdDKwLh8IRiKRQLQFTouH6tmGyJxuJxqK66MxkmxeLJ5ICBLGoAxU0knQpjPxYG0SWy7KAA) | [Effect](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCiAZi1WZqmEr3-wPDIAljQAOAeyKZQAb1BkJ44QBsqoAL6gWRJaADkmAJ5iq+IsIBuVIvpHipM+ekq1sm7bpoGqbDpjtkRVwAZxkANX8pAGZQAF4FJTFVKgAKF2o6ADowvEISDLdwgEZ012ysIgBXTlTZZFBQAA9GQuyAOSqaACMbABoG0CNW8uwszp7+wYAvEcyxid6iZA0ASnXkZHwOFWJ1YLDQS2wVKqpGKtwAa1wJAHdcTYOZSRDhTGEJXHjQSM4YrJiYghNLHU5UVaNKHQqFoZ6gV7vT64Rj1GHojGYrHY6FoRpEKjYfBfFRGZqMXBdJYDHG0ul0vGgAlEklk4agSmTIg0+m8vmwsD4wnE3Ck0CzDlUmyDfmy2loLQJLLKoA) | [Zod](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygBaA9vqmEl9z7+MgEsaABxZFMoAN6gyLEQIA2VUAF9QAMyJzQAckwBPYVXxEBANypEdyQSLESAVKGwBnUAC8NWmrvdtryLK4LhIAalRkmGIAzKAAvDJywopUABTuAHQsAEYAVhGYqZLIoKAAHoyZuACuNNmWqQCUADQloPqVGTV1DS1t7p3d9URNyCqNjTb4EQrEykEhoGbYCtVUjNW4ANa4LADuuDYLEqIuApgCLLjxoOGRMRnCxC5py6tUjaVf319ox6Cnc6XXCMYo-cEQyFQ6HfNClCqgIaWVow1FotFw9qMJFEFHo-EE35gUoDRG1YZtQlU1FoNQJDIMoA) | [ArkType](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCCRA1gCoCeADlamEkFDhI8MgCWNbgHsimUAG9QZaVPEAbKqAC+oAGZFVoAOSYeVfEXEA3KkWPIJU2fKVneO-YZonird1QOyCq4AM7yAGpUZJiyAMygALzKqtwaVAAUARkKyKCgAB6MxrgArjQARnbGADR5oJzFZZXVdfkAXk3lVfbI2gCU-Y740erEWiHhoNbY6qVUjKW4rLjSAO64jpPyMqHimOLSuEmgUTHxAHTcxKGZM3NU-fnPL89o26C7+4e4jLmvAMBQOBIJeaHyRVAzR6bVBcPhcPBDUY0LssIRGMx+SRnSh3Ts9SxRMRYF0yQuFKAA) | [Valibot](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygBq2ANgJYBGA9pqmEkFDhI8MnY0ADtyKZQAb1BluU9qyqgAvqABmRFaADkmAJ6Sq+IuwBuVIoeTipMuQCpQ2AM6hru-TSNrNi5eB2RlXE85JioyTBkAZlAAXiUVSTUqAAprADpuTgArWMws+WRQUAAPRjzcAFcaTjssgEoAGgrQE1rchqaWjq6AL17+5qI25E1W1sd8WNZiDQionzZ6qkZ63ABrXG4Ad1xHVblpT3ZMdm5cFOYSxNzJYk9soNZN1sqf35+0M6gC5XG64RjlP6QqHQmGw35oSo1UDjOydOHojEYhHdRgoohozGEon-MCVUbIxoTLrEmnotDaVK5JlAA) | [Arri](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCCRRAlqmEtz73+MjY0ADgHsimUAG9QZUSLYAbKqAC+oAGZF5oAOSYAnsKr52ANypFdgkeMkzsazdpp6AAsXZFhZYAGdKWmxrZDlcP0kANSoyTHEAZlAAXll5YSUqAApsADpRACMAKxjMTKlkUFAAD0Zc3ABXGnzLTIBKABoK0ANanIamlo6ugC9e-uaiNuRVVtbkZHwYxWIVMIjQM2xFeqpGetwAa1xRAHdcebXJMT82TDZRXGTQaNiEnOFiPyzN7apWyoBgIBaEuoGut3uuEY5SBsLh8IRiMBaEqNVA40snSR2JxOJR3UYGKIWNxpLJwLAlVG6MaEy65IZ2LQ6hSOTZQA) | [Sury](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFwYCuRAnqmEl9z7+MgEsaABwD2RTKADeoMqJECANlVABfUADMi80AHJMrYVXxEBANypFdgkeMkAqUNgDOGTdpp7nLVrtD+AwKDg-zQAFgA6AGZQAGsAIVAAChoBXFAAalAAcwAvAWEASmRkOVxnSQA1KjJMcRiAXll5YSUqJPQI50pabCSpZH8AD0ZO3CYaACNLABpB0FZRiPGp2fncpZXpojnVQuLkfBrFYhUyitAzbEUmKkYmXFjcUQB3XBLzyTFnAUwBUXSTWqtXqEWExGc7SuNyohRCQTQn1A31+-1wjAG8Kx2JxOLQw0YWzWuJJpOx+IWhIm2zmZLp9NAFI2oCJRHmDI55LA6iaET5QA) | ... and more
|
|
50
47
|
|
|
51
|
-
|
|
48
|
+
## Overview
|
|
52
49
|
|
|
53
|
-
|
|
50
|
+
TypeDriver is a high-performance validation and type inference middleware designed to integrate JSON Schema and Standard Schema compliant validation into framework interfaces such as HTTP routes and RPC endpoints. It targets type safety at I/O boundaries by providing a unified validation and inference pipeline that can be bound directly to request-receiving interfaces.
|
|
54
51
|
|
|
55
|
-
|
|
52
|
+
This project is designed to unify heterogeneous runtime schema systems based on JSON Schema and Standard Schema into a single system that preserves static type inference, runtime validation, and schema reflection, while remaining compatible with multiple schema ecosystems.
|
|
56
53
|
|
|
57
54
|
License MIT
|
|
58
55
|
|
|
59
56
|
## Features
|
|
60
57
|
|
|
61
|
-
-
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
66
|
-
-
|
|
67
|
-
-
|
|
68
|
-
-
|
|
58
|
+
- Framework Integration
|
|
59
|
+
- Designed for [Type-Safe](#Framework) public IO interfaces (Routes)
|
|
60
|
+
- One [Function](#Compile) to compile schematics into uniform Validators.
|
|
61
|
+
- One [Type](#Static) to infer schematics into TypeScript types.
|
|
62
|
+
- [Extension](#Extensions) Model for Framework Specific Runtime Type APIs
|
|
63
|
+
- Schema Support
|
|
64
|
+
- TypeScript [DSL](#Script) feature for TS7-native (supported in TS5+).
|
|
65
|
+
- Scalable [JSON Schema Type Inference](https://tsplay.dev/NlrkxW) as standard.
|
|
66
|
+
- Supports JSON Schema Drafts 3 through 2020-12.
|
|
67
|
+
- Full support for Standard Schema
|
|
68
|
+
- Validation Compiler
|
|
69
|
+
- High-performance JIT compiler for faster application start up.
|
|
70
|
+
- High-performance runtime validation (approx 2x Ajv under comparable workloads)
|
|
71
|
+
- Automatic [Acceleration](#Accelerate) for libraries supporting Standard JSON Schema.
|
|
72
|
+
- Automatic JIT fallback for [Content-Security](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP) restrictive environments (Cloudflare)
|
|
73
|
+
- Tooling and Interoperability
|
|
74
|
+
- JSON Schema [Reflect](#Reflect) API for OpenAPI, MCP, and IDL-Based Systems
|
|
75
|
+
- Error [Formats](#Errors) for JSON Schema and Standard Schema Based Systems
|
|
76
|
+
- [Localized](#Locales) Error Messages (i18n)
|
|
69
77
|
|
|
70
78
|
## Contents
|
|
71
79
|
|
|
@@ -76,20 +84,23 @@ License MIT
|
|
|
76
84
|
- [Check](#Check)
|
|
77
85
|
- [Parse](#Parse)
|
|
78
86
|
- [Errors](#Errors)
|
|
87
|
+
- [Locales](#Locales)
|
|
79
88
|
- [Static](#Static)
|
|
89
|
+
- [Script](#Script)
|
|
80
90
|
- [Reflect](#Schema)
|
|
81
|
-
- [
|
|
91
|
+
- [Extensions](#Extensions)
|
|
82
92
|
- [Accelerate](#Accelerate)
|
|
93
|
+
- [Compression](#Compression)
|
|
83
94
|
- [Contribute](#Contribute)
|
|
84
95
|
|
|
85
96
|
## Framework
|
|
86
97
|
|
|
87
|
-
TypeDriver is designed for framework integration
|
|
98
|
+
TypeDriver is designed for framework integration. It provides a simple infrastructure to connect type inference and validation to framework interfaces.
|
|
88
99
|
|
|
89
100
|
Ref: [TypeScript](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YyAoEoAiBTAzgSwOYB2qYSZ5FcyADgPbYAuAFAOTAsA0oA3sqKACNaAEwCeALlAADXv34APSYQCuAWwGYAThz5yJoFeq065oAF5K1GzboC+U5La5NNmAI7KcDAJSgAvAB8PLoAxrSEjDyg8lyiXGagtv6grh5eAHRCYqBoDKLUmODYAIYAZpgAhI7eyCQQlA2N8HUAYprFqpgA7rSaANZ1TUPDyLiqdJoMUXkFoADKDMUMuCFcYeO4ADaYiaClmrSqoCwzmMKauABuWiy1mPITU7iEDFqlxSE7AEq0yq8A8tRluFsAAeABCIlEoHur0IwmwoGUhD6hFoXUIyWRqPRhCCskEUMkkLEjmQ90eoFOoB+f0wAGFiptNgIPn1QQAFJYACxh8jhCNAjAuhHwXEBwIifIFiNpAKBuBBQT8oCYuhc7k8jEk3CRmk2ki5DG5XCy+gWSxWoIlioiAG0WGaWABdIK2XyBUCXWi4YTIGoU3pTUrIkKS0B0RicnnSzDwxHC55i0BhCJTG0g2Pxmm-eWS7BcXSM5mskJ9ZJyhlMlls6PG8UKpXIAJMag8w08ri0RsRSQZiJraulvqSYs1su+AmpyJmgD6lyZvqWvWS62oW0wTG7+YdTudNX4uREtEk6TPoDG1G2nReKVzO02tHwKzJQA) | [JSON Schema](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YyAoEoAiBTAzgSwOYB2qYSZ5FcyADgPbYAuAFAOTAsA0oA3sqKACNaAEwCeALh59+oBqOqZJLWgIBWmAMYNO0-gCdMARwCuuA8MkBtFgA9OoFqPssAXiwC6HXaGp7aCvQZcHEleGRkbUNl5RQdCYwBbAUw9FlAAXy9w-gkeaIUleKSUtMzvfhcouQK4xOTUjKyZdOkWzNAmAxMcBgBKUABeAD4pfg1aQkY8my5RLhcMwdAu4x6AOiExUDRqzHBsAEMAM0wAQmR03uQSCEo7+-gbgDE9A4TMAHdaPQBrG4eAYDkLgEnRAnldqAAMoMA5BDRccag3AAG0wiyOfgSDl2wj0uAAbiVrpgbGCGKBcIQGCkjgcNOiAEq0Yw0gDy1CCE2wAB4AEIiUSgUk0wjCbCgYyEH6EWgfQhLKUyuWEEZhQSCyQCsQXZCk8n5JksmkAYQOKJRAnpPx5AAU4QALYU2UXi0CMfGEfBcDlcybO10S5mszC+3DckYDDrSTpGVaMKLGPQoyT2hgOribXIwuG4DQ8sPc6xZjwjS6DEYE2i4YTIK7674Uo5SrThhV0Rh2x0BzBiiUeqne0DjSYUwv+kW9t3B9mctvYLjSM0Wq0aH5LGeYZeW61d9M+ucR5BDJjUR2px1cfx+7CSccL4fmndryTb1c-frqkdTLMAfQJ5o1nC3xLEi1CopgTDXvOxaCh4Vz8DsIi0JIaxoZSoJou81LLMa6IorQ+B5rqQA) | [Standard Schema](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YyAoEoAiBTAzgSwOYB2qYSZ5FcyuAtgA4D2ATgC6gBUoAhtqAF6gAZkwY1QAcj4MAJuOTJG2FgApxwcQBpQAb2ShQAIxkBPAFz8AdAwMArTAGMVu-foAe5vhcIBXGgcxMygCUGnouZpY+fgHBoS78Hl6+-oEhYQC+QcjpWspMmACO3jgsQaAAvAB8OmH2DIRKOqCuWsZaAukVoPlFJRZG0sagaCzGdJjg2FyCmACE2VkkEJQrq-BLAGJMXDSYAO7MANZLa6dn1PTMbNqgo+OgAMosXCy49lp19LgANpigncJRBI7phpExcAA3AJyZCYVyMVigXCEFgBQRcex-ABKDG8qIA8nRXvVsAAeABCJlAcNRhGkvG8hEOhAYe0IXUZzNZhGqzkMJnMlMG2Vh8KutzG2NxqIAwlxvt8DBjDqSAAovAAW1NctPpoCU4MI+C0hOJDW1ut4OLxmFNuBJ1XKoGUYTyhWKSnMN28TG+5nVLA1WgGESeLzepLtJIA2uIQ+IALrVTIVaoQhi4aTILJwhFsQSMxz29mKFhqzUWzB03gG5HG0B1BpsKPmmlVvXWglE4vYLRhOUKpX2Q5dTuYAeK5XlwMm7sO5CVZR0TX+zVaBhzhrmFu9hvyyfD8wToeHMp8xuNEMAfQh8szL2YXU+dB+mGUG7N2Fj8YTWX0IxkBhzAsECkXoX5dhRbppT+b4GHwN4RSAA)
|
|
90
101
|
|
|
91
102
|
```typescript
|
|
92
|
-
|
|
103
|
+
route('/', {
|
|
93
104
|
body: `{
|
|
94
105
|
x: number,
|
|
95
106
|
y: number,
|
|
@@ -100,11 +111,11 @@ post('/', {
|
|
|
100
111
|
})
|
|
101
112
|
```
|
|
102
113
|
|
|
103
|
-
|
|
114
|
+
The above interface design is achieved with the following TypeScript definitions
|
|
104
115
|
|
|
105
116
|
<details>
|
|
106
117
|
|
|
107
|
-
<summary>
|
|
118
|
+
<summary>Route Interface Definition (Expand)</summary>
|
|
108
119
|
|
|
109
120
|
|
|
110
121
|
```typescript
|
|
@@ -116,7 +127,7 @@ export interface RouteOptions<Body extends unknown = unknown> {
|
|
|
116
127
|
export type RouteCallback<Path extends string, Options extends RouteOptions> = (
|
|
117
128
|
(request: { url: Path, body: Static<Options['body']> }) => void
|
|
118
129
|
)
|
|
119
|
-
export function
|
|
130
|
+
export function route<Path extends string, const Options extends RouteOptions,
|
|
120
131
|
Callback = RouteCallback<Path, Options>
|
|
121
132
|
>(path: Path, options: Options, callback: Callback) {
|
|
122
133
|
const body_validator = compile(options['body'])
|
|
@@ -134,7 +145,7 @@ TypeDriver consists of a single compile(...) function that accepts JSON Schema,
|
|
|
134
145
|
import { compile } from 'typedriver'
|
|
135
146
|
```
|
|
136
147
|
|
|
137
|
-
|
|
148
|
+
Ref: [TypeScript](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFAAMErOHAAeALjicAriABGzFXApbdBo6oBeZvYaitcCgJSq37j56-ef7gPR+Ujz8gsJQIlp8AIYYwDRRYQA8AHSpADSIxr7ZObluAW6a2nbMaVl5FZX5gaqmxRZQZVXNlQXWtg1NLd3ZBbgAfOxcvHCQ3MAwwBCcEnACQqLJYFFQ3PJIxkUADF11AIxdNnBbjs5AA)
|
|
138
149
|
|
|
139
150
|
```typescript
|
|
140
151
|
const Vector3 = compile(`{
|
|
@@ -144,7 +155,7 @@ const Vector3 = compile(`{
|
|
|
144
155
|
}`)
|
|
145
156
|
```
|
|
146
157
|
|
|
147
|
-
JSON Schema
|
|
158
|
+
Ref: [JSON Schema](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFEjhLlK1WtUB6DVJ79BwqCIBccPgEMMwGmYMAeAHSOANIlZLK1E6QgAjAFb6pC7qIaFhWsoAHiacAK4gPsxObnBQWACOscBpNCYA2qSRQWQUxaQAXqQAusFhdXARShQx8YlQyUpgxNSwwFjcJor1w+HaSuUtCcwpStGIcB5YXnFTLHi1I6ERuAB8M3DN84vLrcyk6-sTR1RLZCtt57gpT7gAlOxcvHCQ3MAwwBBOBJTPpRPYwGYoNx5EgUnMAAwdA4mACMSKu8NYbyAA)
|
|
148
159
|
|
|
149
160
|
```typescript
|
|
150
161
|
const Vector3 = compile({
|
|
@@ -158,7 +169,7 @@ const Vector3 = compile({
|
|
|
158
169
|
})
|
|
159
170
|
```
|
|
160
171
|
|
|
161
|
-
Standard Schema
|
|
172
|
+
Ref: [Standard Schema](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV0SWOAKjgEMAznABehYiDIiINNq1QA7QfABqWZDGgBmOAF4UaMJiwAKEQDoIAIwBW6mCaRxnL12+cB6DwaWr72gC44FX4MYBp+TSgAHnM4gBpEVmcADyCLBQBXECtmEwBKRPdiktK3Lxc0uCyc5njkuAp08xrcqAKisq7uuArnJurstvrnEWbWvMKe6dK+0SCJqFZcfPyZ9Y3e71wAPnZFZThIQWAYYAgFPWD-KC1zMH4oQVMkBqqABhHGoIBGL7G4O9lvkgA)
|
|
162
173
|
|
|
163
174
|
```typescript
|
|
164
175
|
import * as z from 'zod'
|
|
@@ -200,17 +211,104 @@ const { x, y, z } = Vector3.parse(value)
|
|
|
200
211
|
|
|
201
212
|
## Errors
|
|
202
213
|
|
|
203
|
-
The errors(...) function returns diagnostics (use
|
|
214
|
+
The errors(...) function returns error diagnostics for values (use after failed check only)
|
|
204
215
|
|
|
205
216
|
```typescript
|
|
206
|
-
// Vector3.errors(value: unknown):
|
|
217
|
+
// Vector3.errors(value: unknown): TLocalizedValidationError[]
|
|
207
218
|
|
|
208
219
|
const errors = Vector3.errors(value)
|
|
209
220
|
```
|
|
210
221
|
|
|
222
|
+
The errors(...) function can generate both JSON Schema and Standard Schema error formats.
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
const errors = Vector3.errors({ x: 1, y: true }, {
|
|
226
|
+
format: 'json-schema'
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
const issues = Vector3.errors({ x: 1, y: true }, {
|
|
230
|
+
format: 'standard-schema'
|
|
231
|
+
})
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
<details>
|
|
235
|
+
|
|
236
|
+
<summary>Generated Errors and Issues</summary>
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
// TLocalizedValidationError[]
|
|
240
|
+
|
|
241
|
+
const errors = [{
|
|
242
|
+
keyword: "required",
|
|
243
|
+
schemaPath: "#",
|
|
244
|
+
instancePath: "",
|
|
245
|
+
params: { requiredProperties: [ "z" ] },
|
|
246
|
+
message: "must have required properties z"
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
keyword: "type",
|
|
250
|
+
schemaPath: "#/properties/y",
|
|
251
|
+
instancePath: "/y",
|
|
252
|
+
params: { type: "number" },
|
|
253
|
+
message: "must be number"
|
|
254
|
+
}]
|
|
255
|
+
|
|
256
|
+
// StandardSchemaV1.Issue[]
|
|
257
|
+
|
|
258
|
+
const issues = [
|
|
259
|
+
{ path: [], message: "must have required properties z" },
|
|
260
|
+
{ path: [ "y" ], message: "must be number" }
|
|
261
|
+
]
|
|
262
|
+
```
|
|
263
|
+
</details>
|
|
264
|
+
|
|
265
|
+
## Locales
|
|
266
|
+
|
|
267
|
+
TypeDriver has translations for many different languages and locales.
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
const issues = Vector3.errors({ x: 1, y: true }, {
|
|
271
|
+
format: 'standard-schema',
|
|
272
|
+
locale: 'ko_KR'
|
|
273
|
+
})
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
<details>
|
|
277
|
+
|
|
278
|
+
<summary>Generated Localization</summary>
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
|
|
282
|
+
// StandardSchemaV1.Issue[]
|
|
283
|
+
|
|
284
|
+
const issues = [
|
|
285
|
+
{ path: [], message: "필수 속성 z을(를) 가지고 있어야 합니다" },
|
|
286
|
+
{ path: [ "y" ], message: "number이어야 합니다" }
|
|
287
|
+
]
|
|
288
|
+
```
|
|
289
|
+
</details>
|
|
290
|
+
|
|
291
|
+
<details>
|
|
292
|
+
|
|
293
|
+
<summary>Supported Locales</summary>
|
|
294
|
+
|
|
295
|
+
```typescript
|
|
296
|
+
type LocaleString = (
|
|
297
|
+
| "ar_001" | "bn_BD" | "cs_CZ" | "de_DE" | "el_GR" | "en_US" | "es_419"
|
|
298
|
+
| "es_AR" | "es_ES" | "es_MX" | "fa_IR" | "fil_PH" | "fr_CA" | "fr_FR"
|
|
299
|
+
| "ha_NG" | "hi_IN" | "hu_HU" | "id_ID" | "it_IT" | "ja_JP" | "ko_KR"
|
|
300
|
+
| "ms_MY" | "nl_NL" | "pl_PL" | "pt_BR" | "pt_PT" | "ro_RO" | "ru_RU"
|
|
301
|
+
| "sv_SE" | "sw_TZ" | "th_TH" | "tr_TR" | "uk_UA" | "ur_PK" | "vi_VN"
|
|
302
|
+
| "yo_NG" | "zh_Hans" | "zh_Hant"
|
|
303
|
+
)
|
|
304
|
+
```
|
|
305
|
+
</details>
|
|
306
|
+
|
|
307
|
+
Localization support is only available for JSON Schema
|
|
308
|
+
|
|
211
309
|
## Static
|
|
212
310
|
|
|
213
|
-
TypeDriver provides type
|
|
311
|
+
TypeDriver provides unified type inference for JSON Schema, Standard Schema and TypeScript | [Reference](https://www.typescriptlang.org/play/?target=99&module=7#code/JYWwDg9gTgLgBAbzjAnmApnAyjAhjYAYzgF84AzKCEOAclQwBMpgA3dKWgKC4Hpe4ACQjsocdLkIALOI3TlgAO2AEIi5BDhKAzhkLwYUzA3Q9+cACpp0WQizAweJuAEE4AXmx4ChADwADJC44OAAPAC44RQBXEAAjDgAaYLgUSJj4pJSAL3TYhKguEn8APjMBACksAHkAOWxpdBBcJ2s4ACEPL3wiXwQUk0jaCDiAK3R9WmSQqHQAR2jgWcZIgG1aUKm6FC3abNoAXWm4MCoMWGB0bUj+kJCIxGRrIYyC2lJjkLTHwbpXjneJE+cFyP2ef3yAI+KRIRTKfAEOFwikYuCgjAaRmaPFAkFgcAAVHBcNoQRQqDQ9hBGNwuM4AMJdJE+XwmCDkOD0sqENTaeCMzzZAB0I3G+gAFLcwpFhf8oOKAJTHb6yyHypU5GVCuWK5IkBVcIA)
|
|
214
312
|
|
|
215
313
|
```typescript
|
|
216
314
|
import { type Static } from 'typedriver'
|
|
@@ -239,16 +337,44 @@ type B = Static<{
|
|
|
239
337
|
|
|
240
338
|
import * as z from 'zod'
|
|
241
339
|
|
|
340
|
+
type C = Static<typeof C>
|
|
242
341
|
const C = z.object({
|
|
243
342
|
x: z.number(),
|
|
244
343
|
y: z.number(),
|
|
245
344
|
z: z.number(),
|
|
246
345
|
})
|
|
247
|
-
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Localization support is only available for JSON Schema
|
|
349
|
+
|
|
350
|
+
## Script
|
|
351
|
+
|
|
352
|
+
The TypeScript DSL is a runtime and type-level emulation of TypeScript. This feature is based on TypeBox’s ability to transform TypeScript syntax into JSON Schema. The TypeScript DSL supports most TypeScript definitions, as well as type-level constraints expressed using JSON Schema keywords.
|
|
353
|
+
|
|
354
|
+
> ⚠️ The name `Options<T>` is subject to change. Because Options is a commonly used type name, it may not be appropriate for schema constraint augmentation. As a result, Options should be considered an experimental feature. TypeBox and TypeDriver are open to suggestions for a more suitable name for this type.
|
|
248
355
|
|
|
356
|
+
```typescript
|
|
357
|
+
const ClampedVector3 = compile(`{
|
|
358
|
+
x: Options<number, { minimum: 0, maximum: 1 }>,
|
|
359
|
+
y: Options<number, { minimum: 0, maximum: 1 }>,
|
|
360
|
+
z: Options<number, { minimum: 0, maximum: 1 }>
|
|
361
|
+
}`)
|
|
249
362
|
|
|
363
|
+
const JsonSchema = ClampedVector3.toJsonSchema()
|
|
364
|
+
|
|
365
|
+
// const JsonSchema = {
|
|
366
|
+
// type: "object",
|
|
367
|
+
// required: [ "x", "y", "z" ],
|
|
368
|
+
// properties: {
|
|
369
|
+
// x: { type: "number", minimum: 0, maximum: 1 },
|
|
370
|
+
// y: { type: "number", minimum: 0, maximum: 1 },
|
|
371
|
+
// z: { type: "number", minimum: 0, maximum: 1 }
|
|
372
|
+
// }
|
|
373
|
+
// }
|
|
250
374
|
```
|
|
251
375
|
|
|
376
|
+
Refer to [TypeBox](https://github.com/sinclairzx81/typebox) for additional information on this feature.
|
|
377
|
+
|
|
252
378
|
## Reflect
|
|
253
379
|
|
|
254
380
|
Validators can reflect back a JSON Schema representation if the underlying type supports it. This is true for all TypeScript and JSON Schema source types. Reflect can be used for OpenAPI metadata publishing, or RPC systems that need to publish JSON based IDL (interface definition language) to remote callers. Validators provide two functions for this.
|
|
@@ -274,54 +400,39 @@ The source type used for compilation can also be returned via
|
|
|
274
400
|
validator.schema() // will return the schematic used for compile.
|
|
275
401
|
```
|
|
276
402
|
|
|
277
|
-
##
|
|
403
|
+
## Extensions
|
|
278
404
|
|
|
279
|
-
TypeDriver
|
|
405
|
+
TypeDriver enables Frameworks to define custom Runtime Types specific to the Framework. This can be achieved by creating functions that return statically observable JSON Schema. These schematics can be passed to compile(...) and Static and used like any other type.
|
|
406
|
+
|
|
407
|
+
Ref: [Framework Types](https://www.typescriptlang.org/play/?target=99&module=7#code/JYWwDg9gTgLgBAbzgYwuYAbApgGjjATzCzgGUYBDGYZOAXzgDMo04ByQ4gEymADcsUNgChhAejFwAtDNlz5CxUuUrVU8ZICqAZwoBzLBulqTps2uGoAdtvgA1LMhjQAzHAC8KNGExYAFBAARgBWjjB+CMJwcAAeAFxwVgCuIIGCfgCUOFFwBAnJqelZOQBe+SlpUJnCdBkZopwkDk6uHmSU1MgAPI0QjHDNzlAuAHyiEsbmU9MmRppWwBBWACoQy0lg2Ak6WFz4EHAAojEwUBROcAAKLMSwBADSWATaTCwgVzeC1FjaRjP-AIUwkacHmixWEAAklYYIJtGFwV1NCM2n5NHAsCcsFYuC8KFYCHAAPxwPzxUEZDwovgQYB7fJYARQSmY2E4l5khLAKyMQRwSGU9zU2l7EmQuAMpnAogkAAyFFsAHlGEiUZ4wUtVtDYVB4U5EaCMVj2XB8YSSZyKVS4DS6RLEoy+SjWdjcaTydzeVA4LLBcK7STZfarI6oNLiKCFpq1htsEi8PLbG1EzBlaq1XAANqaAC6RrZbszIaZeZJmbzCUzADoaxqIetNlgusdkBgklwm5oEwqYCMRt3bDnxpJAaPAUYAGJnEBYADu0AA1oqQmE-mP11NwyQAEpYACOSWAUF2AEEoGcCF1rhBbt8Xi6TbvUFAuF1bLwrHo8EkrAurBBZysfscmvW8Hiee9jTdd9uT0NpjlOc4YC6Bcnj6D4by+YAfjwGDP2A6JdwPI9T3PChCXVKN61jJtQK+cDnjGDMiMPY8uDPC9hExSBYDgbkdUYc4SCnCgZ3nKAlxXJwr0+WBsMggsXifaBXzwr84B-P8AKAlFImiRoEjYIJQicNg8ByMBZLvBI6LknC4ByY9iLYhIWJI9iyMvWy7zGOguJiHj4EYH99SWOBjLCLprCTbz5PzV0lMcFS31OWDv1-f9AL7PxLMwuztBsqz5IyBIRLExdlxM5DYp+XTHKwGAkigKxEHwGVDIi0y8Cc1jdgSSqwirVDnhyoqfiyOBctvOKGAVB0pT8iYN2W0xJ2nOdFwAOQqQQ1xW-alGEfjBEE5BhPW8SF22wpvT0triEMgpKhEPzuOgIKQuoMKnqKUqLq2nbboc6Jj0a5rWoM9gfqEegaiAA)
|
|
280
408
|
|
|
281
409
|
```typescript
|
|
282
|
-
import { compile,
|
|
410
|
+
import { compile, type Static } from 'typedriver'
|
|
283
411
|
|
|
284
|
-
|
|
412
|
+
interface FrameworkString {
|
|
413
|
+
type: 'string'
|
|
414
|
+
}
|
|
415
|
+
export function number(): FrameworkString {
|
|
416
|
+
return { type: 'string' }
|
|
417
|
+
}
|
|
285
418
|
|
|
286
|
-
|
|
287
|
-
| "ar_001" | "bn_BD" | "cs_CZ" | "de_DE" | "el_GR" | "en_US" | "es_419"
|
|
288
|
-
| "es_AR" | "es_ES" | "es_MX" | "fa_IR" | "fil_PH" | "fr_CA" | "fr_FR"
|
|
289
|
-
| "ha_NG" | "hi_IN" | "hu_HU" | "id_ID" | "it_IT" | "ja_JP" | "ko_KR"
|
|
290
|
-
| "ms_MY" | "nl_NL" | "pl_PL" | "pt_BR" | "pt_PT" | "ro_RO" | "ru_RU"
|
|
291
|
-
| "sv_SE" | "sw_TZ" | "th_TH" | "tr_TR" | "uk_UA" | "ur_PK" | "vi_VN"
|
|
292
|
-
| "yo_NG" | "zh_Hans" | "zh_Hant"
|
|
293
|
-
)
|
|
419
|
+
// ... Usage
|
|
294
420
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
// keyword: "type",
|
|
299
|
-
// schemaPath: "#",
|
|
300
|
-
// instancePath: "",
|
|
301
|
-
// params: { type: "string" },
|
|
302
|
-
// message: "must be string"
|
|
303
|
-
// }]
|
|
304
|
-
|
|
305
|
-
locale('ko_KR') // Set: Korean | South Korea
|
|
306
|
-
|
|
307
|
-
console.log(compile('string').errors(42)) // [{
|
|
308
|
-
// keyword: "type",
|
|
309
|
-
// schemaPath: "#",
|
|
310
|
-
// instancePath: "",
|
|
311
|
-
// params: { type: "string" },
|
|
312
|
-
// message: "string이어야 합니다"
|
|
313
|
-
// }]
|
|
421
|
+
const T = number() // const T: FrameworkString = { type: 'number' }
|
|
422
|
+
|
|
423
|
+
type T = Static<typeof T> // type T = number
|
|
314
424
|
```
|
|
315
|
-
|
|
425
|
+
|
|
426
|
+
This is an advanced feature based on TypeBox's extension model for JSON Schema.
|
|
316
427
|
|
|
317
428
|
## Accelerate
|
|
318
429
|
|
|
319
|
-
TypeDriver provides acceleration support for libraries that implement the Standard JSON Schema specification. This is a new specification
|
|
430
|
+
TypeDriver provides acceleration support for libraries that implement the upcoming Standard JSON Schema specification. This is a new specification enables remote type libraries to be integrated directly into TypeBox validation infrastructure. This project tracks upstream implementations of this specification and maintains a benchmark measuring compariative performance with and without compile(...).
|
|
320
431
|
|
|
321
432
|
```bash
|
|
322
433
|
$ deno task bench
|
|
323
434
|
```
|
|
324
|
-
Benchmark 16M Parse Operations of this Structure
|
|
435
|
+
Benchmark 16M Parse Operations of this Structure | We Measure Time To Complete
|
|
325
436
|
```typescript
|
|
326
437
|
const Vector3 = compile(`{
|
|
327
438
|
x: number,
|
|
@@ -349,6 +460,22 @@ Accelerated Indicates Support for Standard JSON Schema
|
|
|
349
460
|
Last Run: Thu Dec 04 2025
|
|
350
461
|
```
|
|
351
462
|
|
|
463
|
+
## Compression
|
|
464
|
+
|
|
465
|
+
TypeDriver is intended for server-side validation in Node, Deno and Bun runtimes, but can be used in Browsers also. TypeDriver depends on most of TypeBox's internal compiler and TS emulation infrastructure. The following shows the compression metrics for bundled, minified and gzipped. TypeDriver uses esbuild for bundling and Deno local gzipped compression.
|
|
466
|
+
|
|
467
|
+
```bash
|
|
468
|
+
$ deno task metrics
|
|
469
|
+
```
|
|
470
|
+
Compression Rates
|
|
471
|
+
```bash
|
|
472
|
+
┌───────┬─────────────────────────┬─────────────┬─────────────┬────────────┐
|
|
473
|
+
│ (idx) │ path │ bundled │ minified │ gzipped │
|
|
474
|
+
├───────┼─────────────────────────┼─────────────┼─────────────┼────────────┤
|
|
475
|
+
│ 0 │ "./task/metrics/all.ts" │ "579.74 KB" │ "286.01 KB" │ "55.14 KB" │
|
|
476
|
+
└───────┴─────────────────────────┴─────────────┴─────────────┴────────────┘
|
|
477
|
+
```
|
|
478
|
+
|
|
352
479
|
## Contribute
|
|
353
480
|
|
|
354
481
|
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.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { Validator } from '../validator.mjs';
|
|
2
|
-
import Type from 'typebox';
|
|
3
|
-
/**
|
|
4
|
-
* High-performance Json Schema validator that uses library-specific
|
|
5
|
-
* inference mechanisms. The validator assumes the source library
|
|
6
|
-
* produces accurate schematics that encode the runtime
|
|
7
|
-
* representations of its types.
|
|
8
|
-
*
|
|
9
|
-
* In TypeBox terminology, this falls under the TUnsafe<T> category.
|
|
10
|
-
* Preferably, TypeScript types "should" be derived from the
|
|
11
|
-
* schematics rather than assumed.
|
|
12
|
-
*
|
|
13
|
-
* Note:
|
|
14
|
-
*
|
|
15
|
-
* Standard JSON Schema does not advertise which Draft versions it
|
|
16
|
-
* supports, and the resolver is using try/catch resolution. This
|
|
17
|
-
* should be brought up in RFC feedback, not by me of course, I don't
|
|
18
|
-
* know anything about Json Schema.
|
|
19
|
-
*/
|
|
20
|
-
export declare class JsonSchemaValidator<Input extends Type.TSchema, Output extends unknown = Type.Static<Input>> extends Validator<Input, Output> {
|
|
21
|
-
private readonly input;
|
|
22
|
-
private readonly validator;
|
|
23
|
-
constructor(input: Input);
|
|
24
|
-
schema(): Input;
|
|
25
|
-
isJsonSchema(): boolean;
|
|
26
|
-
toJsonSchema(): unknown;
|
|
27
|
-
isAccelerated(): boolean;
|
|
28
|
-
check(value: unknown): value is Output;
|
|
29
|
-
parse(value: unknown): Output;
|
|
30
|
-
errors(value: unknown): object[];
|
|
31
|
-
}
|
package/build/locale.d.mts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { System } from 'typebox/system';
|
|
2
|
-
declare const Locale: typeof System.Locale;
|
|
3
|
-
type LocaleString = Exclude<keyof typeof Locale, 'Get' | 'Set' | 'Reset'> & ({} & string);
|
|
4
|
-
/** Sets the locale for which errors are generated. */
|
|
5
|
-
export declare function locale(locale: LocaleString): void;
|
|
6
|
-
export {};
|
package/build/locale.mjs
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { System } from 'typebox/system';
|
|
2
|
-
const Locale = System.Locale;
|
|
3
|
-
/** Sets the locale for which errors are generated. */
|
|
4
|
-
export function locale(locale) {
|
|
5
|
-
const F = locale in System.Locale ? System.Locale[locale] : System.Locale.en_US;
|
|
6
|
-
System.Locale.Set(F);
|
|
7
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { StandardJSONSchemaV1, StandardSchemaV1 } from '../_standard/standard-schema.mjs';
|
|
2
|
-
import { Validator } from '../validator.mjs';
|
|
3
|
-
/**
|
|
4
|
-
* High-performance Json Schema validator that uses library-specific
|
|
5
|
-
* inference mechanisms. The validator assumes the source library
|
|
6
|
-
* produces accurate schematics that encode the runtime
|
|
7
|
-
* representations of its types.
|
|
8
|
-
*
|
|
9
|
-
* Note:
|
|
10
|
-
*
|
|
11
|
-
* Standard JSON Schema does not advertise which Draft versions it
|
|
12
|
-
* supports, and the resolver is using try/catch resolution. This
|
|
13
|
-
* should be brought up in RFC feedback.
|
|
14
|
-
*/
|
|
15
|
-
export declare class StandardJsonSchemaValidator<Input extends StandardJSONSchemaV1 & StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferOutput<Input>> extends Validator<Input, Output> {
|
|
16
|
-
private readonly input;
|
|
17
|
-
private readonly validator;
|
|
18
|
-
constructor(input: Input);
|
|
19
|
-
schema(): Input;
|
|
20
|
-
isJsonSchema(): boolean;
|
|
21
|
-
toJsonSchema(): unknown;
|
|
22
|
-
isAccelerated(): boolean;
|
|
23
|
-
check(value: unknown): value is Output;
|
|
24
|
-
parse(value: unknown): Output;
|
|
25
|
-
errors(value: unknown): object[];
|
|
26
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './validator.mjs';
|