typedriver 0.8.0 → 0.8.2

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.
Files changed (37) hide show
  1. package/build/_standard/standard-schema.d.mts +120 -0
  2. package/build/_standard/standard-schema.mjs +1 -0
  3. package/build/compile.d.mts +4 -3
  4. package/build/compile.mjs +11 -5
  5. package/build/errors.d.mts +0 -4
  6. package/build/errors.mjs +1 -6
  7. package/build/guard/index.d.mts +4 -0
  8. package/build/guard/index.mjs +5 -0
  9. package/build/guard/json-schema.d.mts +3 -0
  10. package/build/guard/json-schema.mjs +7 -0
  11. package/build/guard/standard-json-schema.d.mts +2 -0
  12. package/build/guard/standard-json-schema.mjs +33 -0
  13. package/build/guard/standard-schema.d.mts +2 -0
  14. package/build/{guard.mjs → guard/standard-schema.mjs} +2 -17
  15. package/build/guard/typescript.d.mts +1 -0
  16. package/build/guard/typescript.mjs +5 -0
  17. package/build/index.d.mts +1 -0
  18. package/build/index.mjs +1 -0
  19. package/build/json-schema/validator.d.mts +18 -2
  20. package/build/json-schema/validator.mjs +19 -6
  21. package/build/locale.d.mts +6 -0
  22. package/build/locale.mjs +9 -0
  23. package/build/standard-json-schema/index.d.mts +1 -0
  24. package/build/standard-json-schema/index.mjs +1 -0
  25. package/build/standard-json-schema/resolve.d.mts +2 -0
  26. package/build/standard-json-schema/resolve.mjs +32 -0
  27. package/build/standard-json-schema/validator.d.mts +25 -0
  28. package/build/standard-json-schema/validator.mjs +54 -0
  29. package/build/standard-schema/validator.d.mts +2 -3
  30. package/build/standard-schema/validator.mjs +2 -7
  31. package/build/static.d.mts +1 -1
  32. package/build/typescript/validator.d.mts +1 -2
  33. package/build/typescript/validator.mjs +2 -6
  34. package/build/validator.d.mts +4 -6
  35. package/package.json +16 -3
  36. package/readme.md +172 -89
  37. package/build/guard.d.mts +0 -5
@@ -0,0 +1,120 @@
1
+ /** The Standard Typed interface. This is a base type extended by other specs. */
2
+ export interface StandardTypedV1<Input = unknown, Output = Input> {
3
+ /** The Standard properties. */
4
+ readonly '~standard': StandardTypedV1.Props<Input, Output>;
5
+ }
6
+ export declare namespace StandardTypedV1 {
7
+ /** The Standard Typed properties interface. */
8
+ export interface Props<Input = unknown, Output = Input> {
9
+ /** The version number of the standard. */
10
+ readonly version: 1;
11
+ /** The vendor name of the schema library. */
12
+ readonly vendor: string;
13
+ /** Inferred types associated with the schema. */
14
+ readonly types?: Types<Input, Output> | undefined;
15
+ }
16
+ /** The Standard Typed types interface. */
17
+ export interface Types<Input = unknown, Output = Input> {
18
+ /** The input type of the schema. */
19
+ readonly input: Input;
20
+ /** The output type of the schema. */
21
+ readonly output: Output;
22
+ }
23
+ /** Infers the input type of a Standard Typed. */
24
+ export type InferInput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['input'];
25
+ /** Infers the output type of a Standard Typed. */
26
+ export type InferOutput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['output'];
27
+ export {};
28
+ }
29
+ /** The Standard Schema interface. */
30
+ export interface StandardSchemaV1<Input = unknown, Output = Input> {
31
+ /** The Standard Schema properties. */
32
+ readonly '~standard': StandardSchemaV1.Props<Input, Output>;
33
+ }
34
+ export declare namespace StandardSchemaV1 {
35
+ /** The Standard Schema properties interface. */
36
+ export interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
37
+ /** Validates unknown input values. */
38
+ readonly validate: (value: unknown, options?: StandardSchemaV1.Options | undefined) => Result<Output> | Promise<Result<Output>>;
39
+ }
40
+ /** The result interface of the validate function. */
41
+ export type Result<Output> = SuccessResult<Output> | FailureResult;
42
+ /** The result interface if validation succeeds. */
43
+ export interface SuccessResult<Output> {
44
+ /** The typed output value. */
45
+ readonly value: Output;
46
+ /** A falsy value for `issues` indicates success. */
47
+ readonly issues?: undefined;
48
+ }
49
+ export interface Options {
50
+ /** Explicit support for additional vendor-specific parameters, if needed. */
51
+ readonly libraryOptions?: Record<string, unknown> | undefined;
52
+ }
53
+ /** The result interface if validation fails. */
54
+ export interface FailureResult {
55
+ /** The issues of failed validation. */
56
+ readonly issues: ReadonlyArray<Issue>;
57
+ }
58
+ /** The issue interface of the failure output. */
59
+ export interface Issue {
60
+ /** The error message of the issue. */
61
+ readonly message: string;
62
+ /** The path of the issue, if any. */
63
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
64
+ }
65
+ /** The path segment interface of the issue. */
66
+ export interface PathSegment {
67
+ /** The key representing a path segment. */
68
+ readonly key: PropertyKey;
69
+ }
70
+ /** The Standard types interface. */
71
+ export interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {
72
+ }
73
+ /** Infers the input type of a Standard. */
74
+ export type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
75
+ /** Infers the output type of a Standard. */
76
+ export type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
77
+ export {};
78
+ }
79
+ /** The Standard JSON Schema interface. */
80
+ export interface StandardJSONSchemaV1<Input = unknown, Output = Input> {
81
+ /** The Standard JSON Schema properties. */
82
+ readonly '~standard': StandardJSONSchemaV1.Props<Input, Output>;
83
+ }
84
+ export declare namespace StandardJSONSchemaV1 {
85
+ /** The Standard JSON Schema properties interface. */
86
+ export interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
87
+ /** Methods for generating the input/output JSON Schema. */
88
+ readonly jsonSchema: StandardJSONSchemaV1.Converter;
89
+ }
90
+ /** The Standard JSON Schema converter interface. */
91
+ export interface Converter {
92
+ /** Converts the input type to JSON Schema. May throw if conversion is not supported. */
93
+ readonly input: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
94
+ /** Converts the output type to JSON Schema. May throw if conversion is not supported. */
95
+ readonly output: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
96
+ }
97
+ /**
98
+ * The target version of the generated JSON Schema.
99
+ *
100
+ * It is *strongly recommended* that implementers support `"draft-2020-12"` and `"draft-07"`, as they are both in wide use. All other targets can be implemented on a best-effort basis. Libraries should throw if they don't support a specified target.
101
+ *
102
+ * The `"openapi-3.0"` target is intended as a standardized specifier for OpenAPI 3.0 which is a superset of JSON Schema `"draft-04"`.
103
+ */
104
+ export type Target = 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | ({} & string);
105
+ /** The options for the input/output methods. */
106
+ export interface Options {
107
+ /** Specifies the target version of the generated JSON Schema. Support for all versions is on a best-effort basis. If a given version is not supported, the library should throw. */
108
+ readonly target: Target;
109
+ /** Explicit support for additional vendor-specific parameters, if needed. */
110
+ readonly libraryOptions?: Record<string, unknown> | undefined;
111
+ }
112
+ /** The Standard types interface. */
113
+ export interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {
114
+ }
115
+ /** Infers the input type of a Standard. */
116
+ export type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
117
+ /** Infers the output type of a Standard. */
118
+ export type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
119
+ export {};
120
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,11 +1,12 @@
1
- import { StandardSchemaV1 } from '@standard-schema/spec';
2
1
  import Type from 'typebox';
2
+ import { type StandardSchemaV1, type StandardJSONSchemaV1 } from './_standard/standard-schema.mjs';
3
3
  import { Validator } from './validator.mjs';
4
- type TFromTypeScript<Input extends string, Schema extends Type.TSchema = Type.TScript<{}, Input>, Output extends unknown = Type.Static<Schema>, Result extends Validator = Validator<Input, Output>> = Result;
5
4
  type TFromStandardSchema<Input extends StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferInput<Input>, Result extends Validator = Validator<Input, Output>> = Result;
5
+ type TFromStandardJsonSchema<Input extends StandardSchemaV1 & StandardJSONSchemaV1, Output extends unknown = StandardSchemaV1.InferInput<Input>, Result extends Validator = Validator<Input, Output>> = Result;
6
6
  type TFromJsonSchema<Input extends Type.TSchema, Output extends unknown = Type.Static<Input>, Result extends Validator = Validator<Input, Output>> = Result;
7
+ type TFromTypeScript<Input extends string, Schema extends Type.TSchema = Type.TScript<{}, Input>, Output extends unknown = Type.Static<Schema>, Result extends Validator = Validator<Input, Output>> = Result;
7
8
  /** Compiles a schema into a typed Validator */
8
- export type TCompile<Schema extends unknown> = (Schema extends string ? TFromTypeScript<Schema> : Schema extends StandardSchemaV1 ? TFromStandardSchema<Schema> : Schema extends Type.TSchema ? TFromJsonSchema<Schema> : TFromJsonSchema<{}>);
9
+ export type TCompile<Schema extends unknown> = (Schema extends string ? TFromTypeScript<Schema> : Schema extends StandardSchemaV1 ? (Schema extends StandardJSONSchemaV1 ? TFromStandardJsonSchema<Schema> : TFromStandardSchema<Schema>) : Schema extends Type.TSchema ? TFromJsonSchema<Schema> : TFromJsonSchema<{}>);
9
10
  /** Compiles a schema into a typed Validator */
10
11
  export declare function compile<const Schema extends unknown>(schema: Schema): TCompile<Schema>;
11
12
  export {};
package/build/compile.mjs CHANGED
@@ -1,21 +1,27 @@
1
1
  // deno-fmt-ignore-file
2
- import { IsJsonSchema, IsStandardSchemaV1, IsTypeScript } from './guard.mjs';
2
+ import { IsJsonSchema, IsStandardSchemaV1, IsStandardJsonSchemaV1, IsTypeScript } from './guard/index.mjs';
3
3
  import { JsonSchemaValidator } from './json-schema/validator.mjs';
4
+ import { StandardJsonSchemaValidator } from './standard-json-schema/validator.mjs';
4
5
  import { StandardSchemaValidator } from './standard-schema/validator.mjs';
5
6
  import { TypeScriptValidator } from './typescript/validator.mjs';
6
- function FromTypeScript(script) {
7
- return new TypeScriptValidator(script);
8
- }
9
7
  function FromStandardSchema(schema) {
10
8
  return new StandardSchemaValidator(schema);
11
9
  }
10
+ function FromStandardJsonSchema(schema) {
11
+ return new StandardJsonSchemaValidator(schema);
12
+ }
12
13
  function FromJsonSchema(schema) {
13
14
  return new JsonSchemaValidator(schema);
14
15
  }
16
+ function FromTypeScript(script) {
17
+ return new TypeScriptValidator(script);
18
+ }
15
19
  /** Compiles a schema into a typed Validator */
16
20
  export function compile(schema) {
17
21
  return (IsTypeScript(schema) ? FromTypeScript(schema) :
18
- IsStandardSchemaV1(schema) ? FromStandardSchema(schema) :
22
+ IsStandardSchemaV1(schema) ? (IsStandardJsonSchemaV1(schema)
23
+ ? FromStandardJsonSchema(schema)
24
+ : FromStandardSchema(schema)) :
19
25
  IsJsonSchema(schema) ? FromJsonSchema(schema) :
20
26
  FromJsonSchema({}));
21
27
  }
@@ -1,7 +1,3 @@
1
- export declare class AssertError extends globalThis.Error {
2
- readonly errors: object[];
3
- constructor(errors: object[]);
4
- }
5
1
  export declare class ParseError extends globalThis.Error {
6
2
  readonly errors: object[];
7
3
  constructor(errors: object[]);
package/build/errors.mjs CHANGED
@@ -1,9 +1,4 @@
1
- export class AssertError extends globalThis.Error {
2
- constructor(errors) {
3
- super('AssertError');
4
- this.errors = errors;
5
- }
6
- }
1
+ // deno-fmt-ignore-file
7
2
  export class ParseError extends globalThis.Error {
8
3
  constructor(errors) {
9
4
  super('ParseError');
@@ -0,0 +1,4 @@
1
+ export * from './json-schema.mjs';
2
+ export * from './standard-json-schema.mjs';
3
+ export * from './standard-schema.mjs';
4
+ export * from './typescript.mjs';
@@ -0,0 +1,5 @@
1
+ // deno-fmt-ignore-file
2
+ export * from './json-schema.mjs';
3
+ export * from './standard-json-schema.mjs';
4
+ export * from './standard-schema.mjs';
5
+ export * from './typescript.mjs';
@@ -0,0 +1,3 @@
1
+ import Type from 'typebox';
2
+ /** Tests if the given schema is JsonSchema like */
3
+ export declare function IsJsonSchema(value: unknown): value is Type.TSchema;
@@ -0,0 +1,7 @@
1
+ // deno-fmt-ignore-file
2
+ import Guard from 'typebox/guard';
3
+ import { IsStandardSchemaV1 } from '../guard/index.mjs';
4
+ /** Tests if the given schema is JsonSchema like */
5
+ export function IsJsonSchema(value) {
6
+ return !IsStandardSchemaV1(value) && Guard.IsObjectNotArray(value);
7
+ }
@@ -0,0 +1,2 @@
1
+ import { StandardSchemaV1, StandardJSONSchemaV1 } from '../_standard/standard-schema.mjs';
2
+ export declare function IsStandardJsonSchemaV1(value: unknown): value is StandardSchemaV1 & StandardJSONSchemaV1;
@@ -0,0 +1,33 @@
1
+ // deno-fmt-ignore-file
2
+ import Guard from 'typebox/guard';
3
+ function IsStandardJsonSchemaConverter(value) {
4
+ return Guard.IsObject(value)
5
+ && Guard.HasPropertyKey(value, 'input')
6
+ && Guard.HasPropertyKey(value, 'output');
7
+ }
8
+ function IsStandardJsonSchemaV1Props(value) {
9
+ return Guard.IsObject(value) &&
10
+ Guard.HasPropertyKey(value, 'version') &&
11
+ Guard.HasPropertyKey(value, 'vendor') &&
12
+ Guard.HasPropertyKey(value, 'jsonSchema') &&
13
+ (Guard.IsEqual(value.version, '1') || // spec
14
+ Guard.IsEqual(value.version, 1) // arktype
15
+ ) &&
16
+ Guard.IsString(value.vendor) &&
17
+ IsStandardJsonSchemaConverter(value.jsonSchema);
18
+ }
19
+ function IsTypicalStandardJsonSchemaV1(value) {
20
+ return Guard.IsObject(value) &&
21
+ !Guard.IsUndefined(value['~standard']) &&
22
+ IsStandardJsonSchemaV1Props(value['~standard']);
23
+ }
24
+ // ArkType (Obviously)
25
+ function IsNonTypicalStandardJsonSchemaV1(value) {
26
+ return Guard.IsFunction(value) &&
27
+ !Guard.IsUndefined(value['~standard']) &&
28
+ IsStandardJsonSchemaV1Props(value['~standard']);
29
+ }
30
+ export function IsStandardJsonSchemaV1(value) {
31
+ return IsTypicalStandardJsonSchemaV1(value) ||
32
+ IsNonTypicalStandardJsonSchemaV1(value);
33
+ }
@@ -0,0 +1,2 @@
1
+ import { StandardSchemaV1 } from '../_standard/standard-schema.mjs';
2
+ export declare function IsStandardSchemaV1(value: unknown): value is StandardSchemaV1;
@@ -1,8 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
2
  import Guard from 'typebox/guard';
3
- // ------------------------------------------------------------------
4
- // IsStandardSchemaV1
5
- // ------------------------------------------------------------------
6
3
  function IsStandardSchemaV1Props(value) {
7
4
  return Guard.IsObject(value) &&
8
5
  Guard.HasPropertyKey(value, 'version') &&
@@ -16,10 +13,10 @@ function IsStandardSchemaV1Props(value) {
16
13
  }
17
14
  function IsTypicalStandardSchemaV1(value) {
18
15
  return Guard.IsObject(value) &&
19
- Guard.HasPropertyKey(value, '~standard') &&
16
+ !Guard.IsUndefined(value['~standard']) &&
20
17
  IsStandardSchemaV1Props(value['~standard']);
21
18
  }
22
- // ArkType
19
+ // ArkType (Obviously)
23
20
  function IsNonTypicalStandardSchemaV1(value) {
24
21
  return Guard.IsFunction(value) &&
25
22
  !Guard.IsUndefined(value['~standard']) &&
@@ -29,15 +26,3 @@ export function IsStandardSchemaV1(value) {
29
26
  return IsTypicalStandardSchemaV1(value) ||
30
27
  IsNonTypicalStandardSchemaV1(value);
31
28
  }
32
- // ------------------------------------------------------------------
33
- // IsJsonSchema
34
- // ------------------------------------------------------------------
35
- export function IsJsonSchema(value) {
36
- return !IsStandardSchemaV1(value) && Guard.IsObjectNotArray(value);
37
- }
38
- // ------------------------------------------------------------------
39
- // IsTypeScript
40
- // ------------------------------------------------------------------
41
- export function IsTypeScript(value) {
42
- return Guard.IsString(value);
43
- }
@@ -0,0 +1 @@
1
+ export declare function IsTypeScript(value: unknown): value is string;
@@ -0,0 +1,5 @@
1
+ // deno-fmt-ignore-file
2
+ import Guard from 'typebox/guard';
3
+ export function IsTypeScript(value) {
4
+ return Guard.IsString(value);
5
+ }
package/build/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './locale.mjs';
1
2
  export * from './compile.mjs';
2
3
  export * from './validator.mjs';
3
4
  export * from './static.mjs';
package/build/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
+ export * from './locale.mjs';
2
3
  export * from './compile.mjs';
3
4
  export * from './validator.mjs';
4
5
  export * from './static.mjs';
@@ -1,13 +1,29 @@
1
1
  import { Validator } from '../validator.mjs';
2
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
+ */
3
20
  export declare class JsonSchemaValidator<Input extends Type.TSchema, Output extends unknown = Type.Static<Input>> extends Validator<Input, Output> {
4
21
  private readonly input;
5
22
  private readonly validator;
6
23
  constructor(input: Input);
7
24
  schema(): Input;
8
25
  isJsonSchema(): boolean;
9
- asJsonSchema(): unknown;
10
- assert(value: unknown): asserts value is Output;
26
+ toJsonSchema(): unknown;
11
27
  check(value: unknown): value is Output;
12
28
  parse(value: unknown): Output;
13
29
  errors(value: unknown): object[];
@@ -1,7 +1,24 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Validator as TBValidator } from 'typebox/compile';
3
- import { AssertError, ParseError } from '../errors.mjs';
3
+ import { ParseError } from '../errors.mjs';
4
4
  import { Validator } from '../validator.mjs';
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
+ */
5
22
  export class JsonSchemaValidator extends Validator {
6
23
  constructor(input) {
7
24
  super();
@@ -20,16 +37,12 @@ export class JsonSchemaValidator extends Validator {
20
37
  isJsonSchema() {
21
38
  return true;
22
39
  }
23
- asJsonSchema() {
40
+ toJsonSchema() {
24
41
  return this.input;
25
42
  }
26
43
  // ----------------------------------------------------------------
27
44
  // Validation
28
45
  // ----------------------------------------------------------------
29
- assert(value) {
30
- if (!this.validator.Check(value))
31
- throw new AssertError(this.errors(value));
32
- }
33
46
  check(value) {
34
47
  return this.validator.Check(value);
35
48
  }
@@ -0,0 +1,6 @@
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 {};
@@ -0,0 +1,9 @@
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
6
+ ? System.Locale[locale]
7
+ : System.Locale.en_US;
8
+ System.Locale.Set(F);
9
+ }
@@ -0,0 +1 @@
1
+ export * from './validator.mjs';
@@ -0,0 +1 @@
1
+ export * from './validator.mjs';
@@ -0,0 +1,2 @@
1
+ import { StandardJSONSchemaV1 } from '../_standard/standard-schema.mjs';
2
+ export declare function ResolveJsonSchema(input: StandardJSONSchemaV1): Record<string, unknown>;
@@ -0,0 +1,32 @@
1
+ // deno-fmt-ignore-file
2
+ import { Guard } from 'typebox/guard';
3
+ function AsOpenAPI3_0(input) {
4
+ try {
5
+ return input['~standard'].jsonSchema.input({ target: 'openapi-3.0' });
6
+ }
7
+ catch {
8
+ return undefined;
9
+ }
10
+ }
11
+ function AsDraft7(input) {
12
+ try {
13
+ return input['~standard'].jsonSchema.input({ target: 'draft-07' });
14
+ }
15
+ catch {
16
+ return undefined;
17
+ }
18
+ }
19
+ function AsDraft2020_12(input) {
20
+ try {
21
+ return input['~standard'].jsonSchema.input({ target: 'draft-2020-12' });
22
+ }
23
+ catch {
24
+ return undefined;
25
+ }
26
+ }
27
+ export function ResolveJsonSchema(input) {
28
+ const jsonschema = AsDraft2020_12(input) ?? AsDraft7(input) ?? AsOpenAPI3_0(input);
29
+ if (Guard.IsUndefined(jsonschema))
30
+ throw Error(`Vendor '${input['~standard'].vendor}' advertised itself as a Standard JSON Schema but failed to produce a schematic. Submit an issue with the vendor.`);
31
+ return jsonschema;
32
+ }
@@ -0,0 +1,25 @@
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
+ check(value: unknown): value is Output;
23
+ parse(value: unknown): Output;
24
+ errors(value: unknown): object[];
25
+ }
@@ -0,0 +1,54 @@
1
+ // deno-fmt-ignore-file
2
+ import { ParseError } from '../errors.mjs';
3
+ import { Validator } from '../validator.mjs';
4
+ import { Validator as TBValidator } from 'typebox/compile';
5
+ 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
+ export class StandardJsonSchemaValidator extends Validator {
19
+ constructor(input) {
20
+ super();
21
+ this.input = input;
22
+ const schema = ResolveJsonSchema(input);
23
+ this.validator = new TBValidator({}, schema);
24
+ }
25
+ // ----------------------------------------------------------------
26
+ // Schema
27
+ // ----------------------------------------------------------------
28
+ schema() {
29
+ return this.input;
30
+ }
31
+ // ----------------------------------------------------------------
32
+ // Json Schema
33
+ // ----------------------------------------------------------------
34
+ isJsonSchema() {
35
+ return true;
36
+ }
37
+ toJsonSchema() {
38
+ return this.validator.Type();
39
+ }
40
+ // ----------------------------------------------------------------
41
+ // Validation
42
+ // ----------------------------------------------------------------
43
+ check(value) {
44
+ return this.validator.Check(value);
45
+ }
46
+ parse(value) {
47
+ if (!this.validator.Check(value))
48
+ throw new ParseError(this.errors(value));
49
+ return value;
50
+ }
51
+ errors(value) {
52
+ return this.validator.Errors(value);
53
+ }
54
+ }
@@ -1,12 +1,11 @@
1
- import { StandardSchemaV1 } from '@standard-schema/spec';
1
+ import { StandardSchemaV1 } from '../_standard/standard-schema.mjs';
2
2
  import { Validator } 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
6
  schema(): Input;
7
7
  isJsonSchema(): boolean;
8
- asJsonSchema(): unknown;
9
- assert(value: unknown): asserts value is Output;
8
+ toJsonSchema(): unknown;
10
9
  check(value: unknown): value is Output;
11
10
  parse(value: unknown): Output;
12
11
  errors(value: unknown): object[];
@@ -1,5 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
- import { AssertError, ParseError, UnknownError } from '../errors.mjs';
2
+ import { ParseError, UnknownError } from '../errors.mjs';
3
3
  import { Validator } from '../validator.mjs';
4
4
  export class StandardSchemaValidator extends Validator {
5
5
  constructor(input) {
@@ -18,17 +18,12 @@ export class StandardSchemaValidator extends Validator {
18
18
  isJsonSchema() {
19
19
  return false;
20
20
  }
21
- asJsonSchema() {
21
+ toJsonSchema() {
22
22
  return {};
23
23
  }
24
24
  // ----------------------------------------------------------------
25
25
  // Validation
26
26
  // ----------------------------------------------------------------
27
- assert(value) {
28
- const result = this.input['~standard'].validate(value);
29
- if ('issues' in result)
30
- throw new AssertError(result.issues || []);
31
- }
32
27
  check(value) {
33
28
  const result = this.input['~standard'].validate(value);
34
29
  return !('issues' in result);
@@ -1,4 +1,4 @@
1
- import { type StandardSchemaV1 } from '@standard-schema/spec';
1
+ import { type StandardSchemaV1 } from './_standard/standard-schema.mjs';
2
2
  import { type Validator } from './validator.mjs';
3
3
  import Type from 'typebox';
4
4
  type StaticValidator<_Input extends unknown, Output extends unknown> = (Output);
@@ -7,9 +7,8 @@ export declare class TypeScriptValidator<Input extends string, Schema extends Ty
7
7
  constructor(script: Input);
8
8
  schema(): Input;
9
9
  isJsonSchema(): boolean;
10
- asJsonSchema(): unknown;
10
+ toJsonSchema(): unknown;
11
11
  check(value: unknown): value is Output;
12
- assert(value: unknown): asserts value is Output;
13
12
  parse(value: unknown): Output;
14
13
  errors(value: unknown): object[];
15
14
  }
@@ -1,7 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Validator as TBValidator } from 'typebox/compile';
3
3
  import { Validator } from '../validator.mjs';
4
- import { AssertError, ParseError } from '../errors.mjs';
4
+ import { ParseError } from '../errors.mjs';
5
5
  import Type from 'typebox';
6
6
  export class TypeScriptValidator extends Validator {
7
7
  constructor(script) {
@@ -22,7 +22,7 @@ export class TypeScriptValidator extends Validator {
22
22
  isJsonSchema() {
23
23
  return true;
24
24
  }
25
- asJsonSchema() {
25
+ toJsonSchema() {
26
26
  return this.jsonschema;
27
27
  }
28
28
  // ----------------------------------------------------------------
@@ -31,10 +31,6 @@ export class TypeScriptValidator extends Validator {
31
31
  check(value) {
32
32
  return this.validator.Check(value);
33
33
  }
34
- assert(value) {
35
- if (!this.validator.Check(value))
36
- throw new AssertError(this.errors(value));
37
- }
38
34
  parse(value) {
39
35
  if (!this.validator.Check(value))
40
36
  throw new ParseError(this.errors(value));
@@ -1,17 +1,15 @@
1
1
  /** Abstract Base for all Validator types. */
2
2
  export declare abstract class Validator<Input extends unknown = unknown, Output extends unknown = unknown> {
3
- /** Returns the internal schema used by this validator */
3
+ /** Returns the schema used to construct this validator */
4
4
  abstract schema(): Input;
5
- /** Asserts a value matches the given schema */
6
- abstract assert(value: unknown): asserts value is Output;
7
5
  /** Checks a value matches the given schema */
8
6
  abstract check(value: unknown): value is Output;
9
7
  /** Parses a value and throws if invalid */
10
8
  abstract parse(value: unknown): Output;
11
9
  /** Returns errors for the given value */
12
10
  abstract errors(value: unknown): object[];
13
- /** True if the the validator supports Json Schema translation */
11
+ /** True if the validator has a Json Schema representation */
14
12
  abstract isJsonSchema(): boolean;
15
- /** Returns this validator as Json Schema. */
16
- abstract asJsonSchema(): unknown;
13
+ /** Return the validator Json Schema representation. */
14
+ abstract toJsonSchema(): unknown;
17
15
  }
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.0",
4
+ "version": "0.8.2",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "json-schema",
@@ -14,8 +14,7 @@
14
14
  "url": "https://github.com/sinclairzx81/typedriver"
15
15
  },
16
16
  "dependencies": {
17
- "@standard-schema/spec": "^1.0.0",
18
- "typebox": "^1.0.53"
17
+ "typebox": "^1.0.61"
19
18
  },
20
19
  "type": "module",
21
20
  "types": "./build/index.d.mts",
@@ -25,10 +24,18 @@
25
24
  "import": "./build/standard-schema/index.mjs",
26
25
  "default": "./build/standard-schema/index.mjs"
27
26
  },
27
+ "./guard": {
28
+ "import": "./build/guard/index.mjs",
29
+ "default": "./build/guard/index.mjs"
30
+ },
28
31
  "./json-schema": {
29
32
  "import": "./build/json-schema/index.mjs",
30
33
  "default": "./build/json-schema/index.mjs"
31
34
  },
35
+ "./standard-json-schema": {
36
+ "import": "./build/standard-json-schema/index.mjs",
37
+ "default": "./build/standard-json-schema/index.mjs"
38
+ },
32
39
  "./typescript": {
33
40
  "import": "./build/typescript/index.mjs",
34
41
  "default": "./build/typescript/index.mjs"
@@ -43,9 +50,15 @@
43
50
  "standard-schema": [
44
51
  "./build/standard-schema/index.d.mts"
45
52
  ],
53
+ "guard": [
54
+ "./build/guard/index.d.mts"
55
+ ],
46
56
  "json-schema": [
47
57
  "./build/json-schema/index.d.mts"
48
58
  ],
59
+ "standard-json-schema": [
60
+ "./build/standard-json-schema/index.d.mts"
61
+ ],
49
62
  "typescript": [
50
63
  "./build/typescript/index.d.mts"
51
64
  ],
package/readme.md CHANGED
@@ -2,138 +2,164 @@
2
2
 
3
3
  <h1>TypeDriver</h1>
4
4
 
5
- <p>Unified Runtime Validation and Inference Driver for TypeScript</p>
5
+ <p>Integration Middlware for High Performance Runtime Validation</p>
6
6
 
7
7
  <img src="typedriver.png" />
8
8
 
9
9
  <br />
10
10
  <br />
11
11
 
12
+ [![npm version](https://badge.fury.io/js/typedriver.svg)](https://badge.fury.io/js/typedriver)
13
+ [![Downloads](https://img.shields.io/npm/dm/typedriver.svg)](https://www.npmjs.com/package/typedriver)
12
14
  [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Test](https://github.com/sinclairzx81/typedriver/actions/workflows/build.yml/badge.svg)](https://github.com/sinclairzx81/typedriver/actions/workflows/build.yml)
13
15
 
14
16
  </div>
15
17
 
18
+ ## Install
19
+
20
+ ```bash
21
+ $ npm install typedriver
22
+ ```
16
23
 
17
24
  ## Example
18
25
 
19
- Compile, Infer and Parse
26
+ Runtime Compile System for Json Schema, Standard Schema and TypeScript
20
27
 
21
- ```typescript
22
- import { compile, type Static } from 'typedriver'
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) | [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) | [TypeBox](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhgFygAqAngA5kBC2AHqmEn-wMHhkASwrNsAJwAuoAN6gi2cSIA2ZUAF9QAM0nLQAcmksyAE0kiAbmUmHR4qbKatd+ikZOsARl0OgA5GQlXExZADUyImkpAGZQAF5FZWY1MgAKFzIAOgB5bwArKOl0uWQAzjos7IA5AFcKb1t0gEoAGnLQRirTWoamyVaOgIAvHtY+xuaW5E0WmeQzKNUqSQ0QsNArKlU6sjo63ABrXGwAd1wgjdkJTBFpETxE0EjouOzmVcwM7d2yFoCgKBgLQ11At3uj1wdDKwLh8IRiKRQLQFTouH6tmGyJxuJxqK66MxkmxeLJ5ICBLGoAxU0knQpjPxYG0SWy7KAA)
23
29
 
24
- // Compile
30
+
31
+ ```typescript
32
+ import { compile } from 'typedriver'
25
33
 
26
34
  const Vector3 = compile(`{
27
- x: number
28
- y: number
35
+ x: number,
36
+ y: number,
29
37
  z: number
30
- }`) // const Vector3: Validator<... {
31
- // x: number,
32
- // y: number,
33
- // z: number
34
- // }>
35
-
36
- // Infer
37
-
38
- type Vector3 = Static<typeof Vector3> // type Vector3 = {
39
- // x: number,
40
- // y: number,
41
- // z: number
42
- // }
43
-
44
- // Parse
45
-
46
- const position = Vector3.parse({ // const position: {
47
- x: 0, // x: number,
48
- y: 1, // y: number,
49
- z: 0 // z: number
50
- }) // } = ...
38
+ }`)
39
+
40
+ declare const value: unknown
41
+
42
+ const position = Vector3.parse(value) // const position: {
43
+ // x: number,
44
+ // y: number,
45
+ // z: number
46
+ // } = ...
51
47
  ```
52
48
 
53
49
  ## Overview
54
50
 
55
- TypeDriver is a unified runtime validation and inference system for Json Schema, Standard Schema, and runtime TypeScript. It is designed for framework integration and offers simple Json Schema and Standard Schema integration using a common validation interface.
51
+ TypeDriver is a validation middleware that enables frameworks to incorporate Json Schema and Standard Schema based libraries on framework interfaces (for example HTTP route handlers, RPC endpoints, etc). It offers a unified validation and type inference system to make integrating runtime types simple, and provides a default TypeScript DSL inference system as standard.
52
+
53
+ > TypeDriver is designed to normalize heterogeneous runtime schema systems into a single, framework-consumable contract that preserves static type inference, runtime validation, and schema reflection without forcing a commitment to a single schema ecosystem.
54
+
55
+ TypeDriver provides high performance validation for Json Schema Drafts 3 through 2020-12. Internally it uses TypeBox for schema validation and type inference, and Standard Schema for remote library integration. The TypeScript DSL provides runtime and type-level TypeScript emulation in support of TS 7 native compiler.
56
+
57
+
56
58
 
57
59
  License MIT
58
60
 
59
61
  ## Contents
60
62
 
61
63
  - [Overview](#Overview)
64
+ - [Framework](#Framework)
62
65
  - [Compile](#Compile)
63
- - [Validator](#Validator)
64
- - [Assert](#Assert)
65
- - [Check](#Check)
66
- - [Parse](#Parse)
67
- - [Errors](#VaErrorslidator)
66
+ - [Check](#Check)
67
+ - [Parse](#Parse)
68
+ - [Errors](#Errors)
68
69
  - [Static](#Static)
69
- - [Schema](#Schema)
70
+ - [Reflect](#Schema)
71
+ - [Locale](#Locale)
70
72
  - [Contribute](#Contribute)
71
73
 
74
+ ## Framework
75
+
76
+ TypeDriver is designed for framework integration to allow multiple type safe libraries to be hosted on framework interfaces. It provides a simple infrastructure to connect type inference and validation to the framework. The following shows a simple design for a route function ...
77
+
78
+ [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)
79
+
80
+ ```typescript
81
+ post('/', {
82
+ body: `{
83
+ x: number,
84
+ y: number,
85
+ z: number
86
+ }`
87
+ }, (request) => {
88
+ const { x, y, z } = request.body // type-safe!
89
+ })
90
+ ```
91
+
92
+ Where the above design is achieved with the following ...
93
+
94
+ ```typescript
95
+ import { type Static, compile } from 'typedriver'
96
+
97
+ export interface RouteOptions<Body extends unknown = unknown> {
98
+ body: Body
99
+ }
100
+ export type RouteCallback<Path extends string, Options extends RouteOptions> = (
101
+ (request: { url: Path, body: Static<Options['body']> }) => void
102
+ )
103
+ export function post<Path extends string, const Options extends RouteOptions,
104
+ Callback = RouteCallback<Path, Options>
105
+ >(path: Path, options: Options, callback: Callback) {
106
+ const body_validator = compile(options['body'])
107
+ // todo: ... implement route logic
108
+ }
109
+ ```
110
+
72
111
  ## Compile
73
112
 
74
- TypeDriver provides a single compile(...) function that accepts Json Schema, Standard Schema or TypeScript definition (expressed as a string). The function returns a Validator instance that can be used to check, parse and assert values.
113
+ TypeDriver consists of a singular compile(...) function that accepts either a TypeScript, Json Schema and Standard Schema definition and returns an optimized Validator.
75
114
 
76
115
  ```typescript
77
116
  import { compile } from 'typedriver'
78
117
  ```
79
118
 
80
- Use the compile function can compile TypeScript types ...
119
+ Pass TypeScript definitions via strings [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFAAMErOHAAeALjicAriABGzFXApbdBo6oBeZvYaitcCgJSq37j56-ef7gPR+Ujz8gsJQIlp8AIYYwDRRYQA8AHSpADSIxr7ZObluAW6a2nbMaVl5FZX5gaqmxRZQZVXNlQXWtg1NLd3ZBbgAfOxcvHCQ3MAwwBCcEnACQqLJYFFQ3PJIxkUADF11AIxdNnBbjs5AA)
81
120
 
82
121
  ```typescript
83
122
  const Vector3 = compile(`{
84
123
  x: number
85
124
  y: number
86
125
  z: number
87
- }`) // const Vector3: Validator<..., {
88
- // x: number,
89
- // y: number,
90
- // z: number,
91
- // }>
126
+ }`)
92
127
  ```
93
128
 
94
- ... or Json Schema schematics
129
+ ... or Json Schema [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFEjhLlK1WtUB6DVJ79BwqCIBccPgEMMwGmYMAeAHSOANIlZLK1E6QgAjAFb6pC7qIaFhWsoAHiacAK4gPsxObnBQWACOscBpNCYA2qSRQWQUxaQAXqQAusFhdXARShQx8YlQyUpgxNSwwFjcJor1w+HaSuUtCcwpStGIcB5YXnFTLHi1I6ERuAB8M3DN84vLrcyk6-sTR1RLZCtt57gpT7gAlOxcvHCQ3MAwwBBOBJTPpRPYwGYoNx5EgUnMAAwdA4mACMSKu8NYbyAA)
95
130
 
96
131
  ```typescript
97
- const Vector3 = compile({ // const Vector3: Validator<..., {
98
- type: 'object', // x: number,
99
- required: ['x', 'y', 'z'], // y: number,
100
- properties: { // z: number
101
- x: { type: 'number' }, // }>
132
+ const Vector3 = compile({
133
+ type: 'object',
134
+ required: ['x', 'y', 'z'],
135
+ properties: {
136
+ x: { type: 'number' },
102
137
  y: { type: 'number' },
103
138
  z: { type: 'number' }
104
139
  }
105
140
  })
106
141
  ```
107
142
 
108
- ... or libraries that implement Standard Schema.
143
+ ... or libraries that implement Standard Schema [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV0SWOAKjgEMAznABehYiDIiINNq1QA7QfABqWZDGgBmOAF4UaMJiwAKEQDoIAIwBW6mCaRxnL12+cB6DwaWr72gC44FX4MYBp+TSgAHnM4gBpEVmcADyCLBQBXECtmEwBKRPdiktK3Lxc0uCyc5njkuAp08xrcqAKisq7uuArnJurstvrnEWbWvMKe6dK+0SCJqFZcfPyZ9Y3e71wAPnZFZThIQWAYYAgFPWD-KC1zMH4oQVMkBqqABhHGoIBGL7G4O9lvkgA)
109
144
 
110
145
  ```typescript
111
- import * z from 'zod'
146
+ import * as z from 'zod'
112
147
 
113
- const Vector3 = compile(z.object({ // const Vector3: Validator<..., {
114
- x: z.number(), // x: number,
115
- y: z.number(), // y: number,
116
- z: z.number(), // z: number
117
- })) // }>
148
+ const Vector3 = compile(z.object({
149
+ x: z.number(),
150
+ y: z.number(),
151
+ z: z.number(),
152
+ }))
118
153
  ```
119
154
 
120
- ## Validator
121
-
122
- Validators contain functions to assert, parse and check JavaScript values.
123
-
124
- ### Assert
125
155
 
126
- The assert(...) function will throw if the value is invalid.
127
-
128
- ```typescript
129
- // Vector3.assert(value: unknown): asserts value is Vector3
156
+ ## Validator
130
157
 
131
- Vector3.assert(value)
132
- ```
158
+ The compile(...) function returns Validator instances to Check, Parse and report Errors for JavaScript values.
133
159
 
134
- ### Check
160
+ ## Check
135
161
 
136
- The check(...) returns a safe boolean result.
162
+ The check(...) returns a boolean result.
137
163
 
138
164
  ```typescript
139
165
  // Vector3.check(value: unknown): value is Vector3
@@ -144,9 +170,9 @@ if(Vector3.check(value)) {
144
170
  }
145
171
  ```
146
172
 
147
- ### Parse
173
+ ## Parse
148
174
 
149
- The parse(...) function checks and returns a value and throws on error.
175
+ The parse(...) function returns if valid, otherwise throws.
150
176
 
151
177
  ```typescript
152
178
  // Vector3.parse(value: unknown): Vector3
@@ -154,9 +180,9 @@ The parse(...) function checks and returns a value and throws on error.
154
180
  const { x, y, z } = Vector3.parse(value)
155
181
  ```
156
182
 
157
- ### Errors
183
+ ## Errors
158
184
 
159
- The errors(...) function returns validation errors for a value.
185
+ The errors(...) function returns diagnostics (use only after failed check)
160
186
 
161
187
  ```typescript
162
188
  // Vector3.errors(value: unknown): object[]
@@ -166,49 +192,106 @@ const errors = Vector3.errors(value)
166
192
 
167
193
  ## Static
168
194
 
169
- TypeDriver provides a unified inference type called `Static<T>` that can infer types from Validator instances as well as Json Schema, Standard Schema or TypeScript syntax. The following infers a Validator.
195
+ The Static type that can infer TypeScript, Json Schema and Standard Schema | [Example](https://www.typescriptlang.org/play/?target=99&module=7#code/JYWwDg9gTgLgBAbzjAnmApnAyjAhjYAYzgF84AzKCEOAclQwBMpgA3dKWgKC4Hpe4ASQB25DsjToAzhSo1cAGwXIAFsGEBzKQEIeDTAEE4AXmx4ChADwADJFzhwAHgC44wgK4gARhwA09uBRXD28-AIAvYM8fKC4SawA+PUk4ACETM3wiSwQA-VdaCC8AK3RCGFp-Byh0AEd3YBrGVwBtWkdKuhRO2nDaAF0quDAqDFhgaVdchwcXRAkMApCY2lIhhyD5-LpljlWSdbhIrckl6L21gJI4pK5QSFg4ACo4XBlw2Wo6cIhGbi5CBBhFJ4ABhDLhAB0RVK5QAFNMnK4obsoHCAJRDTYo85ozERZGQ1EY-wkdFcfRwcGmHBZKz6CDkKm3IA)
170
196
 
171
197
  ```typescript
172
- import { compile, type Static } from 'typedriver'
198
+ import { type Static } from 'typedriver'
199
+
200
+ // Infer types from all things!
173
201
 
174
- const Vector = compile(`{
202
+ type A = Static<`{
175
203
  x: number,
176
204
  y: number,
177
205
  z: number
178
- }`)
206
+ }`>
207
+
208
+ type B = Static<{
209
+ type: 'object',
210
+ required: ['x', 'y', 'z'],
211
+ properties: {
212
+ x: { type: 'number' },
213
+ y: { type: 'number' },
214
+ z: { type: 'number' },
215
+ }
216
+ }>
217
+
218
+ import * as z from 'zod'
219
+
220
+ const C = z.object({
221
+ x: z.number(),
222
+ y: z.number(),
223
+ z: z.number(),
224
+ })
225
+ type C = Static<typeof C>
226
+
179
227
 
180
- type Vector = Static<typeof Vector> // type Vector = {
181
- // x: number
182
- // y: number
183
- // z: number
184
- // }
185
228
  ```
186
229
 
187
- ## Schema
230
+ ## Reflect
188
231
 
189
- Validator instances provide access to internal schematics if required. They also provide support for Validator to Json Schema translation in some cases, with fallbacks for manual Json Schema transformation if required.
232
+ 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.
190
233
 
191
234
  ```typescript
192
235
  import { compile, type Static } from 'typedriver'
193
236
 
194
237
  const validator = compile(...)
195
238
 
196
-
197
239
  validator.isJsonSchema() // Returns true if the validator can be converted to
198
240
  // Json Schema. This is true when the validator was
199
241
  // compiled with Json Schema or TypeScript, but false
200
242
  // if it was compiled with Standard Schema.
201
243
 
202
- validator.asJsonSchema() // Returns the Json Schema for the validator. If the
244
+ validator.toJsonSchema() // Returns the Json Schema for the validator. If the
203
245
  // validator was compiled with Standard Schema, an
204
246
  // empty {} is returned to indicate an unknown
205
247
  // runtime schema.
248
+ ```
249
+ The source type used for compilation can also be returned via
206
250
 
207
- validator.schema() // Returns the schema that was passed to compile. This
208
- // can be used to manually transform the schema if
209
- // isJsonSchema() returns false.
251
+ ```typescript
252
+ validator.schema() // will return the schematic used for compile.
210
253
  ```
211
254
 
255
+ ## Locale
256
+
257
+ 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.
258
+
259
+ ```typescript
260
+ import { compile, locale } from 'typedriver'
261
+
262
+ // Supported Locales
263
+
264
+ type LocaleString =
265
+ | "ar_001" | "bn_BD" | "cs_CZ" | "de_DE" | "el_GR" | "en_US" | "es_419"
266
+ | "es_AR" | "es_ES" | "es_MX" | "fa_IR" | "fil_PH" | "fr_CA" | "fr_FR"
267
+ | "ha_NG" | "hi_IN" | "hu_HU" | "id_ID" | "it_IT" | "ja_JP" | "ko_KR"
268
+ | "ms_MY" | "nl_NL" | "pl_PL" | "pt_BR" | "pt_PT" | "ro_RO" | "ru_RU"
269
+ | "sv_SE" | "sw_TZ" | "th_TH" | "tr_TR" | "uk_UA" | "ur_PK" | "vi_VN"
270
+ | "yo_NG" | "zh_Hans" | "zh_Hant"
271
+
272
+ locale('en_US') // Set: English | US (Default)
273
+
274
+ console.log(compile('string').errors(42)) // [{
275
+ // keyword: "type",
276
+ // schemaPath: "#",
277
+ // instancePath: "",
278
+ // params: { type: "string" },
279
+ // message: "must be string"
280
+ // }]
281
+
282
+ locale('ko_KR') // Set: Korean | South Korea
283
+
284
+ console.log(compile('string').errors(42)) // [{
285
+ // keyword: "type",
286
+ // schemaPath: "#",
287
+ // instancePath: "",
288
+ // params: { type: "string" },
289
+ // message: "string이어야 합니다"
290
+ // }]
291
+ ```
292
+ Localization support is only available for Json Schema and supporting libraries.
293
+
294
+
212
295
  ## Contribute
213
296
 
214
- TypeDriver is open to community contribution. Please ensure you submit an issue before submitting a pull request. This project prefers open community discussion before accepting new features.
297
+ 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.
package/build/guard.d.mts DELETED
@@ -1,5 +0,0 @@
1
- import { StandardSchemaV1 } from '@standard-schema/spec';
2
- import Type from 'typebox';
3
- export declare function IsStandardSchemaV1(value: unknown): value is StandardSchemaV1;
4
- export declare function IsJsonSchema(value: unknown): value is Type.TSchema;
5
- export declare function IsTypeScript(value: unknown): value is string;