typedriver 0.8.0 → 0.8.1

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.
@@ -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.HasPropertyKey(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') &&
@@ -19,7 +16,7 @@ function IsTypicalStandardSchemaV1(value) {
19
16
  Guard.HasPropertyKey(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
+ }
@@ -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 @@
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.1",
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,143 +2,158 @@
2
2
 
3
3
  <h1>TypeDriver</h1>
4
4
 
5
- <p>Unified Runtime Validation and Inference Driver for TypeScript</p>
5
+ <p>Integration Driver 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
16
19
 
17
- ## Example
20
+ ```bash
21
+ $ npm install typedriver
22
+ ```
18
23
 
19
- Compile, Infer and Parse
24
+ ## Example
20
25
 
21
26
  ```typescript
22
- import { compile, type Static } from 'typedriver'
23
-
24
- // Compile
27
+ import { compile } from 'typedriver'
25
28
 
26
- const Vector3 = compile(`{
27
- x: number
28
- y: number
29
+ const Vec3 = compile(`{
30
+ x: number,
31
+ y: number,
29
32
  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
- }) // } = ...
33
+ }`)
34
+
35
+ const normal = Vec3.parse({
36
+ x: 0,
37
+ y: 1,
38
+ z: 0
39
+ })
51
40
  ```
52
41
 
53
42
  ## Overview
54
43
 
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.
44
+ TypeDriver is a runtime type system and integration driver for web frameworks that need to support type inference and validation for TypeScript, Json Schema, and Standard Schema. It includes a high-performance Json Schema validation compiler, an integrated TypeScript engine, and seamless integration support for Standard Schema libraries.
45
+
46
+ TypeDriver supports Json Schema drafts 3 through 2020-12. It provides advanced type inference support for both Json Schema and TypeScript DSL definitions as standard. It uses TypeBox as a foundation for high performance runtime validation, and Standard Schema for cross-library compatibility.
56
47
 
57
48
  License MIT
58
49
 
59
50
  ## Contents
60
51
 
61
52
  - [Overview](#Overview)
53
+ - [Framework](#Framework)
62
54
  - [Compile](#Compile)
63
- - [Validator](#Validator)
64
- - [Assert](#Assert)
65
55
  - [Check](#Check)
66
56
  - [Parse](#Parse)
67
57
  - [Errors](#VaErrorslidator)
68
58
  - [Static](#Static)
69
- - [Schema](#Schema)
59
+ - [Reflect](#Schema)
70
60
  - [Contribute](#Contribute)
71
61
 
62
+ ## Framework
63
+
64
+ TypeDriver is designed specifically for framework developers who need to develop systems to host multiple type safe libraries on framework interfaces. It offers a simple infrastructure to connect type inference and validation to the framework, and is unopinionated with regards to framework design.
65
+
66
+ [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)
67
+
68
+ ```typescript
69
+ post('/', {
70
+ body: `{
71
+ x: number,
72
+ y: number,
73
+ z: number
74
+ }`
75
+ }, (request) => {
76
+ const { x, y, z } = request.body // type-safe!
77
+ })
78
+ ```
79
+
80
+ The above design is achieved in a few lines of code.
81
+
82
+ ```typescript
83
+ import { type Static, compile } from 'typedriver'
84
+
85
+ export interface RouteOptions<Body extends unknown = unknown> {
86
+ body: Body
87
+ }
88
+ export type RouteCallback<Path extends string, Options extends RouteOptions> = (
89
+ (request: { url: Path, body: Static<Options['body']> }) => void
90
+ )
91
+ export function post<Path extends string, const Options extends RouteOptions,
92
+ Callback = RouteCallback<Path, Options>
93
+ >(path: Path, options: Options, callback: Callback) {
94
+ const body_validator = compile(options['body'])
95
+ // todo: ... implement route logic
96
+ }
97
+ ```
98
+
99
+
72
100
  ## Compile
73
101
 
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.
102
+ TypeDriver consists of a singular compile(...) function that accepts either a TypeScript, Json Schema and Standard Schema definition and returns an optimized Validator.
75
103
 
76
104
  ```typescript
77
105
  import { compile } from 'typedriver'
78
106
  ```
79
107
 
80
- Use the compile function can compile TypeScript types ...
108
+ Pass TypeScript definitions via strings. | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFAAMErOHAAeALjicAriABGzFXApbdBo6oBeZvYaitcCgJSq37j56-ef7gPR+Ujz8gsJQIlp8AIYYwDRRYQA8AHSpADSIxr7ZObluAW6a2nbMaVl5FZX5gaqmxRZQZVXNlQXWtg1NLd3ZBbgAfOxcvHCQ3MAwwBCcEnACQqLJYFFQ3PJIxkUADF11AIxdNnBbjs5AA)
81
109
 
82
110
  ```typescript
83
- const Vector3 = compile(`{
111
+ const Vec3 = compile(`{
84
112
  x: number
85
113
  y: number
86
114
  z: number
87
- }`) // const Vector3: Validator<..., {
88
- // x: number,
89
- // y: number,
90
- // z: number,
91
- // }>
115
+ }`)
92
116
  ```
93
117
 
94
- ... or Json Schema schematics
118
+ ... or Json Schema | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFEjhLlK1WtUB6DVJ79BwqCIBccPgEMMwGmYMAeAHSOANIlZLK1E6QgAjAFb6pC7qIaFhWsoAHiacAK4gPsxObnBQWACOscBpNCYA2qSRQWQUxaQAXqQAusFhdXARShQx8YlQyUpgxNSwwFjcJor1w+HaSuUtCcwpStGIcB5YXnFTLHi1I6ERuAB8M3DN84vLrcyk6-sTR1RLZCtt57gpT7gAlOxcvHCQ3MAwwBBOBJTPpRPYwGYoNx5EgUnMAAwdA4mACMSKu8NYbyAA)
95
119
 
96
120
  ```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' }, // }>
121
+ const Vec3 = compile({
122
+ type: 'object',
123
+ required: ['x', 'y', 'z'],
124
+ properties: {
125
+ x: { type: 'number' },
102
126
  y: { type: 'number' },
103
127
  z: { type: 'number' }
104
128
  }
105
129
  })
106
130
  ```
107
131
 
108
- ... or libraries that implement Standard Schema.
132
+ ... or libraries that implement Standard Schema | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV0SWOAKjgEMAznABehYiDIiINNq1QA7QfABqWZDGgBmOAF4UaMJiwAKEQDoIAIwBW6mCaRxnL12+cB6DwaWr72gC44FX4MYBp+TSgAHnM4gBpEVmcADyCLBQBXECtmEwBKRPdiktK3Lxc0uCyc5njkuAp08xrcqAKisq7uuArnJurstvrnEWbWvMKe6dK+0SCJqFZcfPyZ9Y3e71wAPnZFZThIQWAYYAgFPWD-KC1zMH4oQVMkBqqABhHGoIBGL7G4O9lvkgA)
109
133
 
110
134
  ```typescript
111
- import * z from 'zod'
135
+ import * as z from 'zod'
112
136
 
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
- })) // }>
137
+ const Vec3 = compile(z.object({
138
+ x: z.number(),
139
+ y: z.number(),
140
+ z: z.number(),
141
+ }))
118
142
  ```
119
143
 
120
- ## Validator
121
-
122
- Validators contain functions to assert, parse and check JavaScript values.
123
144
 
124
- ### Assert
125
-
126
- The assert(...) function will throw if the value is invalid.
127
-
128
- ```typescript
129
- // Vector3.assert(value: unknown): asserts value is Vector3
145
+ ## Validator
130
146
 
131
- Vector3.assert(value)
132
- ```
147
+ The compile(...) function returns Validator instances to Check, Parse and report Errors for JavaScript values.
133
148
 
134
149
  ### Check
135
150
 
136
- The check(...) returns a safe boolean result.
151
+ The check(...) returns a boolean result.
137
152
 
138
153
  ```typescript
139
- // Vector3.check(value: unknown): value is Vector3
154
+ // Vec3.check(value: unknown): value is Vec3
140
155
 
141
- if(Vector3.check(value)) {
156
+ if(Vec3.check(value)) {
142
157
 
143
158
  const { x, y, z } = value // safe
144
159
  }
@@ -146,69 +161,86 @@ if(Vector3.check(value)) {
146
161
 
147
162
  ### Parse
148
163
 
149
- The parse(...) function checks and returns a value and throws on error.
164
+ The parse(...) function returns if valid, otherwise throws.
150
165
 
151
166
  ```typescript
152
- // Vector3.parse(value: unknown): Vector3
167
+ // Vec3.parse(value: unknown): Vec3
153
168
 
154
- const { x, y, z } = Vector3.parse(value)
169
+ const { x, y, z } = Vec3.parse(value)
155
170
  ```
156
171
 
157
172
  ### Errors
158
173
 
159
- The errors(...) function returns validation errors for a value.
174
+ The errors(...) function returns diagnostics (use only after failed check)
160
175
 
161
176
  ```typescript
162
- // Vector3.errors(value: unknown): object[]
177
+ // Vec3.errors(value: unknown): object[]
163
178
 
164
- const errors = Vector3.errors(value)
179
+ const errors = Vec3.errors(value)
165
180
  ```
166
181
 
167
182
  ## Static
168
183
 
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.
184
+ 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
185
 
171
186
  ```typescript
172
- import { compile, type Static } from 'typedriver'
187
+ import { type Static } from 'typedriver'
173
188
 
174
- const Vector = compile(`{
189
+ // Infer types from all things!
190
+
191
+ type A = Static<`{
175
192
  x: number,
176
193
  y: number,
177
194
  z: number
178
- }`)
195
+ }`>
196
+
197
+ type B = Static<{
198
+ type: 'object',
199
+ required: ['x', 'y', 'z'],
200
+ properties: {
201
+ x: { type: 'number' },
202
+ y: { type: 'number' },
203
+ z: { type: 'number' },
204
+ }
205
+ }>
206
+
207
+ import * as z from 'zod'
208
+
209
+ const C = z.object({
210
+ x: z.number(),
211
+ y: z.number(),
212
+ z: z.number(),
213
+ })
214
+ type C = Static<typeof C>
215
+
179
216
 
180
- type Vector = Static<typeof Vector> // type Vector = {
181
- // x: number
182
- // y: number
183
- // z: number
184
- // }
185
217
  ```
186
218
 
187
- ## Schema
219
+ ## Reflect
188
220
 
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.
221
+ 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
222
 
191
223
  ```typescript
192
224
  import { compile, type Static } from 'typedriver'
193
225
 
194
226
  const validator = compile(...)
195
227
 
196
-
197
228
  validator.isJsonSchema() // Returns true if the validator can be converted to
198
229
  // Json Schema. This is true when the validator was
199
230
  // compiled with Json Schema or TypeScript, but false
200
231
  // if it was compiled with Standard Schema.
201
232
 
202
- validator.asJsonSchema() // Returns the Json Schema for the validator. If the
233
+ validator.toJsonSchema() // Returns the Json Schema for the validator. If the
203
234
  // validator was compiled with Standard Schema, an
204
235
  // empty {} is returned to indicate an unknown
205
236
  // runtime schema.
237
+ ```
238
+ The source type used for compilation can also be returned via
206
239
 
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.
240
+ ```typescript
241
+ validator.schema() // will return the schematic used for compile.
210
242
  ```
211
243
 
212
244
  ## Contribute
213
245
 
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.
246
+ 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;