typedriver 0.8.3 → 0.8.5
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 +34 -3
- 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 +17 -28
- 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 +17 -23
- package/build/{standard-schema → validators/standard-schema}/validator.d.mts +4 -4
- package/build/{standard-schema → validators/standard-schema}/validator.mjs +15 -13
- package/build/{typescript → validators/typescript}/validator.d.mts +3 -3
- package/build/{typescript → validators/typescript}/validator.mjs +17 -11
- package/package.json +6 -27
- package/readme.md +104 -56
- 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,7 +1,35 @@
|
|
|
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
|
-
/** Returns true if this validator is accelerated */
|
|
4
|
-
abstract accelerated(): boolean;
|
|
5
33
|
/** Returns the schema used to construct this validator */
|
|
6
34
|
abstract schema(): Input;
|
|
7
35
|
/** Checks a value matches the given schema */
|
|
@@ -9,9 +37,12 @@ export declare abstract class Validator<Input extends unknown = unknown, Output
|
|
|
9
37
|
/** Parses a value and throws if invalid */
|
|
10
38
|
abstract parse(value: unknown): Output;
|
|
11
39
|
/** Returns errors for the given value */
|
|
12
|
-
abstract errors(value: unknown):
|
|
40
|
+
abstract errors<Options extends Partial<TErrorOptions>>(value: unknown, options?: Options): TErrorResult<Options>;
|
|
13
41
|
/** True if the validator has a Json Schema representation */
|
|
14
42
|
abstract isJsonSchema(): boolean;
|
|
15
43
|
/** Return the validator Json Schema representation. */
|
|
16
44
|
abstract toJsonSchema(): unknown;
|
|
45
|
+
/** Returns true if this validator is JIT accelerated */
|
|
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();
|
|
@@ -26,12 +10,6 @@ export class JsonSchemaValidator extends Validator {
|
|
|
26
10
|
this.validator = new TBValidator({}, input);
|
|
27
11
|
}
|
|
28
12
|
// ----------------------------------------------------------------
|
|
29
|
-
// Accelerated
|
|
30
|
-
// ----------------------------------------------------------------
|
|
31
|
-
accelerated() {
|
|
32
|
-
return this.validator.IsEvaluated();
|
|
33
|
-
}
|
|
34
|
-
// ----------------------------------------------------------------
|
|
35
13
|
// Schema
|
|
36
14
|
// ----------------------------------------------------------------
|
|
37
15
|
schema() {
|
|
@@ -47,6 +25,12 @@ export class JsonSchemaValidator extends Validator {
|
|
|
47
25
|
return this.input;
|
|
48
26
|
}
|
|
49
27
|
// ----------------------------------------------------------------
|
|
28
|
+
// Acceleration
|
|
29
|
+
// ----------------------------------------------------------------
|
|
30
|
+
isAccelerated() {
|
|
31
|
+
return this.validator.IsEvaluated();
|
|
32
|
+
}
|
|
33
|
+
// ----------------------------------------------------------------
|
|
50
34
|
// Validation
|
|
51
35
|
// ----------------------------------------------------------------
|
|
52
36
|
check(value) {
|
|
@@ -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
|
+
// Why doesn't the api provide a discovery mechanism?
|
|
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();
|
|
@@ -23,12 +12,6 @@ export class StandardJsonSchemaValidator extends Validator {
|
|
|
23
12
|
this.validator = new TBValidator({}, schema);
|
|
24
13
|
}
|
|
25
14
|
// ----------------------------------------------------------------
|
|
26
|
-
// Accelerated
|
|
27
|
-
// ----------------------------------------------------------------
|
|
28
|
-
accelerated() {
|
|
29
|
-
return this.validator.IsEvaluated();
|
|
30
|
-
}
|
|
31
|
-
// ----------------------------------------------------------------
|
|
32
15
|
// Schema
|
|
33
16
|
// ----------------------------------------------------------------
|
|
34
17
|
schema() {
|
|
@@ -44,6 +27,12 @@ export class StandardJsonSchemaValidator extends Validator {
|
|
|
44
27
|
return this.validator.Type();
|
|
45
28
|
}
|
|
46
29
|
// ----------------------------------------------------------------
|
|
30
|
+
// Acceleration
|
|
31
|
+
// ----------------------------------------------------------------
|
|
32
|
+
isAccelerated() {
|
|
33
|
+
return this.validator.IsEvaluated();
|
|
34
|
+
}
|
|
35
|
+
// ----------------------------------------------------------------
|
|
47
36
|
// Validation
|
|
48
37
|
// ----------------------------------------------------------------
|
|
49
38
|
check(value) {
|
|
@@ -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,13 +1,13 @@
|
|
|
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);
|
|
6
|
-
accelerated(): boolean;
|
|
7
6
|
schema(): Input;
|
|
8
7
|
isJsonSchema(): boolean;
|
|
9
8
|
toJsonSchema(): unknown;
|
|
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,18 +1,12 @@
|
|
|
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();
|
|
7
7
|
this.input = input;
|
|
8
8
|
}
|
|
9
9
|
// ----------------------------------------------------------------
|
|
10
|
-
// Accelerated
|
|
11
|
-
// ----------------------------------------------------------------
|
|
12
|
-
accelerated() {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
// ----------------------------------------------------------------
|
|
16
10
|
// Schema
|
|
17
11
|
// ----------------------------------------------------------------
|
|
18
12
|
schema() {
|
|
@@ -28,6 +22,12 @@ export class StandardSchemaValidator extends Validator {
|
|
|
28
22
|
return {};
|
|
29
23
|
}
|
|
30
24
|
// ----------------------------------------------------------------
|
|
25
|
+
// Acceleration
|
|
26
|
+
// ----------------------------------------------------------------
|
|
27
|
+
isAccelerated() {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
// ----------------------------------------------------------------
|
|
31
31
|
// Validation
|
|
32
32
|
// ----------------------------------------------------------------
|
|
33
33
|
check(value) {
|
|
@@ -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,15 +1,15 @@
|
|
|
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;
|
|
5
5
|
private readonly script;
|
|
6
6
|
private readonly jsonschema;
|
|
7
7
|
constructor(script: Input);
|
|
8
|
-
accelerated(): boolean;
|
|
9
8
|
schema(): Input;
|
|
10
9
|
isJsonSchema(): boolean;
|
|
11
10
|
toJsonSchema(): unknown;
|
|
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) {
|
|
@@ -11,12 +12,6 @@ export class TypeScriptValidator extends Validator {
|
|
|
11
12
|
this.validator = new TBValidator({}, this.jsonschema);
|
|
12
13
|
}
|
|
13
14
|
// ----------------------------------------------------------------
|
|
14
|
-
// Accelerated
|
|
15
|
-
// ----------------------------------------------------------------
|
|
16
|
-
accelerated() {
|
|
17
|
-
return this.validator.IsEvaluated();
|
|
18
|
-
}
|
|
19
|
-
// ----------------------------------------------------------------
|
|
20
15
|
// Schema
|
|
21
16
|
// ----------------------------------------------------------------
|
|
22
17
|
schema() {
|
|
@@ -32,6 +27,12 @@ export class TypeScriptValidator extends Validator {
|
|
|
32
27
|
return this.jsonschema;
|
|
33
28
|
}
|
|
34
29
|
// ----------------------------------------------------------------
|
|
30
|
+
// Acceleration
|
|
31
|
+
// ----------------------------------------------------------------
|
|
32
|
+
isAccelerated() {
|
|
33
|
+
return this.validator.IsEvaluated();
|
|
34
|
+
}
|
|
35
|
+
// ----------------------------------------------------------------
|
|
35
36
|
// Validation
|
|
36
37
|
// ----------------------------------------------------------------
|
|
37
38
|
check(value) {
|
|
@@ -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
3
|
"description": "Unified Runtime Validation and Inference Driver for TypeScript",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.5",
|
|
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>Integration
|
|
5
|
+
<p>Integration Middleware for High Performance Runtime Validation</p>
|
|
6
6
|
|
|
7
7
|
<img src="typedriver.png" />
|
|
8
8
|
|
|
@@ -23,7 +23,7 @@ $ npm install typedriver
|
|
|
23
23
|
|
|
24
24
|
## Example
|
|
25
25
|
|
|
26
|
-
Framework Compiler for JSON Schema, Standard Schema and TypeScript
|
|
26
|
+
Framework Compiler System for JSON Schema, Standard Schema and TypeScript
|
|
27
27
|
|
|
28
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
29
|
|
|
@@ -58,14 +58,15 @@ License MIT
|
|
|
58
58
|
|
|
59
59
|
## Features
|
|
60
60
|
|
|
61
|
-
-
|
|
62
|
-
-
|
|
61
|
+
- Built Specifically for Framework Integration
|
|
62
|
+
- TypeScript DSL for TS7 Native (supported in TS5 and above)
|
|
63
63
|
- Unified Validation for JSON Schema and Standard Schema
|
|
64
64
|
- Unified Inference for JSON Schema and Standard Schema
|
|
65
65
|
- High Performance Validation Compiler (performance approx 2x Ajv)
|
|
66
66
|
- Acceleration support for Standard JSON Schema
|
|
67
67
|
- JSON Schema Reflect for OpenAPI, MCP and IDL Based Systems
|
|
68
|
-
-
|
|
68
|
+
- Error Formats for JSON Schema and Standard Schema Based Reporting
|
|
69
|
+
- Error Message Localization (i18n)
|
|
69
70
|
|
|
70
71
|
## Contents
|
|
71
72
|
|
|
@@ -76,20 +77,20 @@ License MIT
|
|
|
76
77
|
- [Check](#Check)
|
|
77
78
|
- [Parse](#Parse)
|
|
78
79
|
- [Errors](#Errors)
|
|
80
|
+
- [Locales](#Locales)
|
|
79
81
|
- [Static](#Static)
|
|
80
82
|
- [Reflect](#Schema)
|
|
81
|
-
- [Locale](#Locale)
|
|
82
83
|
- [Accelerate](#Accelerate)
|
|
83
84
|
- [Contribute](#Contribute)
|
|
84
85
|
|
|
85
86
|
## Framework
|
|
86
87
|
|
|
87
|
-
TypeDriver is designed for framework integration
|
|
88
|
+
TypeDriver is designed for framework integration. It provides a simple infrastructure to connect type inference and validation to framework interfaces.
|
|
88
89
|
|
|
89
90
|
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
91
|
|
|
91
92
|
```typescript
|
|
92
|
-
|
|
93
|
+
route('/', {
|
|
93
94
|
body: `{
|
|
94
95
|
x: number,
|
|
95
96
|
y: number,
|
|
@@ -100,11 +101,11 @@ post('/', {
|
|
|
100
101
|
})
|
|
101
102
|
```
|
|
102
103
|
|
|
103
|
-
Where the above design is achieved with the following TS definitions
|
|
104
|
+
Where the above interface design is achieved with the following TS definitions
|
|
104
105
|
|
|
105
106
|
<details>
|
|
106
107
|
|
|
107
|
-
<summary>
|
|
108
|
+
<summary>Route Interface Definition (Expand)</summary>
|
|
108
109
|
|
|
109
110
|
|
|
110
111
|
```typescript
|
|
@@ -116,7 +117,7 @@ export interface RouteOptions<Body extends unknown = unknown> {
|
|
|
116
117
|
export type RouteCallback<Path extends string, Options extends RouteOptions> = (
|
|
117
118
|
(request: { url: Path, body: Static<Options['body']> }) => void
|
|
118
119
|
)
|
|
119
|
-
export function
|
|
120
|
+
export function route<Path extends string, const Options extends RouteOptions,
|
|
120
121
|
Callback = RouteCallback<Path, Options>
|
|
121
122
|
>(path: Path, options: Options, callback: Callback) {
|
|
122
123
|
const body_validator = compile(options['body'])
|
|
@@ -134,7 +135,7 @@ TypeDriver consists of a single compile(...) function that accepts JSON Schema,
|
|
|
134
135
|
import { compile } from 'typedriver'
|
|
135
136
|
```
|
|
136
137
|
|
|
137
|
-
|
|
138
|
+
Ref: [TypeScript](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFAAMErOHAAeALjicAriABGzFXApbdBo6oBeZvYaitcCgJSq37j56-ef7gPR+Ujz8gsJQIlp8AIYYwDRRYQA8AHSpADSIxr7ZObluAW6a2nbMaVl5FZX5gaqmxRZQZVXNlQXWtg1NLd3ZBbgAfOxcvHCQ3MAwwBCcEnACQqLJYFFQ3PJIxkUADF11AIxdNnBbjs5AA)
|
|
138
139
|
|
|
139
140
|
```typescript
|
|
140
141
|
const Vector3 = compile(`{
|
|
@@ -144,7 +145,7 @@ const Vector3 = compile(`{
|
|
|
144
145
|
}`)
|
|
145
146
|
```
|
|
146
147
|
|
|
147
|
-
JSON Schema
|
|
148
|
+
Ref: [JSON Schema](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFEjhLlK1WtUB6DVJ79BwqCIBccPgEMMwGmYMAeAHSOANIlZLK1E6QgAjAFb6pC7qIaFhWsoAHiacAK4gPsxObnBQWACOscBpNCYA2qSRQWQUxaQAXqQAusFhdXARShQx8YlQyUpgxNSwwFjcJor1w+HaSuUtCcwpStGIcB5YXnFTLHi1I6ERuAB8M3DN84vLrcyk6-sTR1RLZCtt57gpT7gAlOxcvHCQ3MAwwBBOBJTPpRPYwGYoNx5EgUnMAAwdA4mACMSKu8NYbyAA)
|
|
148
149
|
|
|
149
150
|
```typescript
|
|
150
151
|
const Vector3 = compile({
|
|
@@ -158,7 +159,7 @@ const Vector3 = compile({
|
|
|
158
159
|
})
|
|
159
160
|
```
|
|
160
161
|
|
|
161
|
-
Standard Schema
|
|
162
|
+
Ref: [Standard Schema](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV0SWOAKjgEMAznABehYiDIiINNq1QA7QfABqWZDGgBmOAF4UaMJiwAKEQDoIAIwBW6mCaRxnL12+cB6DwaWr72gC44FX4MYBp+TSgAHnM4gBpEVmcADyCLBQBXECtmEwBKRPdiktK3Lxc0uCyc5njkuAp08xrcqAKisq7uuArnJurstvrnEWbWvMKe6dK+0SCJqFZcfPyZ9Y3e71wAPnZFZThIQWAYYAgFPWD-KC1zMH4oQVMkBqqABhHGoIBGL7G4O9lvkgA)
|
|
162
163
|
|
|
163
164
|
```typescript
|
|
164
165
|
import * as z from 'zod'
|
|
@@ -200,14 +201,101 @@ const { x, y, z } = Vector3.parse(value)
|
|
|
200
201
|
|
|
201
202
|
## Errors
|
|
202
203
|
|
|
203
|
-
The errors(...) function returns diagnostics (use
|
|
204
|
+
The errors(...) function returns error diagnostics for values (use after failed check only)
|
|
204
205
|
|
|
205
206
|
```typescript
|
|
206
|
-
// Vector3.errors(value: unknown):
|
|
207
|
+
// Vector3.errors(value: unknown): TLocalizedValidationError[]
|
|
207
208
|
|
|
208
209
|
const errors = Vector3.errors(value)
|
|
209
210
|
```
|
|
210
211
|
|
|
212
|
+
The errors(...) function can generate both JSON Schema and Standard Schema error formats.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const errors = Vector3.errors({ x: 1, y: true }, {
|
|
216
|
+
format: 'json-schema'
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
const issues = Vector3.errors({ x: 1, y: true }, {
|
|
220
|
+
format: 'standard-schema'
|
|
221
|
+
})
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
<details>
|
|
225
|
+
|
|
226
|
+
<summary>Generated Errors and Issues</summary>
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
// TLocalizedValidationError[]
|
|
230
|
+
|
|
231
|
+
const errors = [{
|
|
232
|
+
keyword: "required",
|
|
233
|
+
schemaPath: "#",
|
|
234
|
+
instancePath: "",
|
|
235
|
+
params: { requiredProperties: [ "z" ] },
|
|
236
|
+
message: "must have required properties z"
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
keyword: "type",
|
|
240
|
+
schemaPath: "#/properties/y",
|
|
241
|
+
instancePath: "/y",
|
|
242
|
+
params: { type: "number" },
|
|
243
|
+
message: "must be number"
|
|
244
|
+
}]
|
|
245
|
+
|
|
246
|
+
// StandardSchemaV1.Issue[]
|
|
247
|
+
|
|
248
|
+
const issues = [
|
|
249
|
+
{ path: [], message: "must have required properties z" },
|
|
250
|
+
{ path: [ "y" ], message: "must be number" }
|
|
251
|
+
]
|
|
252
|
+
```
|
|
253
|
+
</details>
|
|
254
|
+
|
|
255
|
+
## Locales
|
|
256
|
+
|
|
257
|
+
TypeDriver has translations for many different languages and locales.
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
const issues = Vector3.errors({ x: 1, y: true }, {
|
|
261
|
+
format: 'standard-schema',
|
|
262
|
+
locale: 'ko_KR'
|
|
263
|
+
})
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
<details>
|
|
267
|
+
|
|
268
|
+
<summary>Generated Localization</summary>
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
|
|
272
|
+
// StandardSchemaV1.Issue[]
|
|
273
|
+
|
|
274
|
+
const issues = [
|
|
275
|
+
{ path: [], message: "필수 속성 z을(를) 가지고 있어야 합니다" },
|
|
276
|
+
{ path: [ "y" ], message: "number이어야 합니다" }
|
|
277
|
+
]
|
|
278
|
+
```
|
|
279
|
+
</details>
|
|
280
|
+
|
|
281
|
+
<details>
|
|
282
|
+
|
|
283
|
+
<summary>Supported Locales</summary>
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
type LocaleString = (
|
|
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
|
+
)
|
|
294
|
+
```
|
|
295
|
+
</details>
|
|
296
|
+
|
|
297
|
+
Localization support is only available for JSON Schema
|
|
298
|
+
|
|
211
299
|
## Static
|
|
212
300
|
|
|
213
301
|
TypeDriver provides type infernece for JSON Schema, Standard Schema or TypeScript | [Example](https://www.typescriptlang.org/play/?target=99&module=7#code/JYWwDg9gTgLgBAbzjAnmApnAyjAhjYAYzgF84AzKCEOAclQwBMpgA3dKWgKC4Hpe4ASQB25DsjToAzhSo1cAGwXIAFsGEBzKQEIeDTAEE4AXmx4ChADwADJFzhwAHgC44wgK4gARhwA09uBRXD28-AIAvYM8fKC4SawA+PUk4ACETM3wiSwQA-VdaCC8AK3RCGFp-Byh0AEd3YBrGVwBtWkdKuhRO2nDaAF0quDAqDFhgaVdchwcXRAkMApCY2lIhhyD5-LpljlWSdbhIrckl6L21gJI4pK5QSFg4ACo4XBlw2Wo6cIhGbi5CBBhFJ4ABhDLhAB0RVK5QAFNMnK4obsoHCAJRDTYo85ozERZGQ1EY-wkdFcfRwcGmHBZKz6CDkKm3IA)
|
|
@@ -274,46 +362,6 @@ The source type used for compilation can also be returned via
|
|
|
274
362
|
validator.schema() // will return the schematic used for compile.
|
|
275
363
|
```
|
|
276
364
|
|
|
277
|
-
## Locale
|
|
278
|
-
|
|
279
|
-
TypeDriver provides (i18n) error message translations for many languages and locales. Locales are defined as [IETF BCP 47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt) localization codes. Additional localizations can be submitted to the TypeBox project.
|
|
280
|
-
|
|
281
|
-
```typescript
|
|
282
|
-
import { compile, locale } from 'typedriver'
|
|
283
|
-
|
|
284
|
-
// Supported Locales
|
|
285
|
-
|
|
286
|
-
type LocaleString = (
|
|
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
|
-
)
|
|
294
|
-
|
|
295
|
-
locale('en_US') // Set: English | US (Default)
|
|
296
|
-
|
|
297
|
-
console.log(compile('string').errors(42)) // [{
|
|
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
|
-
// }]
|
|
314
|
-
```
|
|
315
|
-
Localization support is only available for JSON Schema.
|
|
316
|
-
|
|
317
365
|
## Accelerate
|
|
318
366
|
|
|
319
367
|
TypeDriver provides acceleration support for libraries that implement the Standard JSON Schema specification. This is a new specification that enables runtime type libraries to be integrated into TypeBox validation infrastructure. This project tracks upstream implementations of this specification and maintains a benchmark measuring compariative performance with and without compile(...).
|
|
@@ -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
|
-
accelerated(): boolean;
|
|
25
|
-
schema(): Input;
|
|
26
|
-
isJsonSchema(): boolean;
|
|
27
|
-
toJsonSchema(): unknown;
|
|
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
|
-
accelerated(): boolean;
|
|
20
|
-
schema(): Input;
|
|
21
|
-
isJsonSchema(): boolean;
|
|
22
|
-
toJsonSchema(): unknown;
|
|
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';
|