typedriver 0.8.7 → 0.8.8

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.
@@ -1,3 +1,3 @@
1
- import { StandardSchemaV1 } from '../_standard/standard-schema.mjs';
2
- import { TLocalizedValidationError } from 'typebox/error';
3
- export declare function errorToIssue(error: TLocalizedValidationError): StandardSchemaV1.Issue;
1
+ import type { TJsonSchemaError, TStandardSchemaError } from '../validator.mjs';
2
+ /** (Internal) Transform TJsonSchemaError to TStandardSchemaError */
3
+ export declare function errorToIssue(error: TJsonSchemaError): TStandardSchemaError;
@@ -8,6 +8,7 @@ function pathSegments(pointer) {
8
8
  return [];
9
9
  return pointer.slice(1).split('/').map((segment) => segment.replace(/~1/g, '/').replace(/~0/g, '~'));
10
10
  }
11
+ /** (Internal) Transform TJsonSchemaError to TStandardSchemaError */
11
12
  export function errorToIssue(error) {
12
13
  const path = pathSegments(error.instancePath);
13
14
  return { path, message: error.message };
@@ -1,4 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
+ // ------------------------------------------------------------------
3
+ // ParseError
4
+ // ------------------------------------------------------------------
2
5
  export class ParseError extends globalThis.Error {
3
6
  constructor(value, errors) {
4
7
  super('ParseError');
@@ -12,6 +15,9 @@ export class ParseError extends globalThis.Error {
12
15
  });
13
16
  }
14
17
  }
18
+ // ------------------------------------------------------------------
19
+ // UnknownError
20
+ // ------------------------------------------------------------------
15
21
  export class UnknownError extends globalThis.Error {
16
22
  constructor(message) {
17
23
  super(`UnknownError: ${message}`);
@@ -1,3 +1,5 @@
1
1
  export * from './exceptions.mjs';
2
2
  export * from './error-to-issue.mjs';
3
3
  export * from './issue-to-error.mjs';
4
+ export * from './normal-error.mjs';
5
+ export * from './normal-issue.mjs';
@@ -2,3 +2,5 @@
2
2
  export * from './exceptions.mjs';
3
3
  export * from './error-to-issue.mjs';
4
4
  export * from './issue-to-error.mjs';
5
+ export * from './normal-error.mjs';
6
+ export * from './normal-issue.mjs';
@@ -1,3 +1,3 @@
1
- import { StandardSchemaV1 } from '../_standard/standard-schema.mjs';
2
- import { TLocalizedValidationError } from 'typebox/error';
3
- export declare function issueToError(issue: StandardSchemaV1.Issue): TLocalizedValidationError;
1
+ import type { TStandardSchemaError, TJsonSchemaError } from '../validator.mjs';
2
+ /** (Internal) Transform TStandardSchemaError to TJsonSchemaError */
3
+ export declare function issueToError(issue: TStandardSchemaError): TJsonSchemaError;
@@ -1,15 +1,23 @@
1
1
  // deno-fmt-ignore-file
2
- import { Guard } from 'typebox/guard';
3
2
  // ------------------------------------------------------------------
4
- // Issues
3
+ // Escape
5
4
  // ------------------------------------------------------------------
6
- function jsonPointer(segments) {
7
- return `#${segments.join('/')}`;
5
+ function escapeKey(segment) {
6
+ return `${segment}`.replace(/~/g, '~0').replace(/\//g, '~1');
8
7
  }
8
+ // ------------------------------------------------------------------
9
+ // Json Pointer
10
+ // ------------------------------------------------------------------
11
+ function pathToJsonPointer(path) {
12
+ const keys = path.map(escapeKey);
13
+ return `#/${keys.join('/')}`;
14
+ }
15
+ // ------------------------------------------------------------------
16
+ // IssueToError
17
+ // ------------------------------------------------------------------
18
+ /** (Internal) Transform TStandardSchemaError to TJsonSchemaError */
9
19
  export function issueToError(issue) {
10
- const instancePath = Guard.HasPropertyKey(issue, 'path') && Guard.IsArray(issue.path)
11
- ? jsonPointer(issue.path)
12
- : jsonPointer([]);
20
+ const instancePath = pathToJsonPointer(issue.path);
13
21
  return {
14
22
  instancePath,
15
23
  schemaPath: '#',
@@ -0,0 +1,4 @@
1
+ import type { TLocalizedValidationError } from 'typebox/error';
2
+ import type { TJsonSchemaError } from '../validator.mjs';
3
+ /** (Internal) Normalize a TLocalizedValidationError as TJsonSchemaError. */
4
+ export declare function normalError(error: TLocalizedValidationError): TJsonSchemaError;
@@ -0,0 +1,5 @@
1
+ // deno-fmt-ignore-file
2
+ /** (Internal) Normalize a TLocalizedValidationError as TJsonSchemaError. */
3
+ export function normalError(error) {
4
+ return error;
5
+ }
@@ -0,0 +1,4 @@
1
+ import type { StandardSchemaV1 } from '../_standard/standard-schema.mjs';
2
+ import type { TStandardSchemaError } from '../validator.mjs';
3
+ /** (Internal) Normalize a StandardSchemaV1.Issue as TStandardSchemaError. */
4
+ export declare function normalIssue(issue: StandardSchemaV1.Issue): TStandardSchemaError;
@@ -0,0 +1,30 @@
1
+ // deno-fmt-ignore-file
2
+ import { Guard } from 'typebox/guard';
3
+ // ------------------------------------------------------------------
4
+ // Normal: Segments
5
+ // ------------------------------------------------------------------
6
+ function isSegmentObjectWithKey(segment) {
7
+ return Guard.IsObject(segment) && Guard.HasPropertyKey(segment, 'key') && (Guard.IsBigInt(segment.key)
8
+ || Guard.IsBoolean(segment.key)
9
+ || Guard.IsString(segment.key)
10
+ || Guard.IsNumber(segment.key));
11
+ }
12
+ function normalizeSegment(segment) {
13
+ return isSegmentObjectWithKey(segment) ? `${segment.key}` : `${segment}`;
14
+ }
15
+ function normalizeSegments(segments) {
16
+ return segments.map(normalizeSegment);
17
+ }
18
+ // ------------------------------------------------------------------
19
+ // Resolve
20
+ // ------------------------------------------------------------------
21
+ function resolveSegments(issue) {
22
+ return (Guard.HasPropertyKey(issue, 'path') && Guard.IsArray(issue.path)
23
+ ? issue.path
24
+ : []);
25
+ }
26
+ /** (Internal) Normalize a StandardSchemaV1.Issue as TStandardSchemaError. */
27
+ export function normalIssue(issue) {
28
+ const segments = resolveSegments(issue);
29
+ return ({ path: normalizeSegments(segments), message: issue.message });
30
+ }
@@ -1,35 +1,27 @@
1
1
  import { System } from 'typebox/system';
2
- import { TLocalizedValidationError } from 'typebox/error';
3
- import { StandardSchemaV1 } from './_standard/standard-schema.mjs';
4
- export type TStandardSchemaError = StandardSchemaV1.Issue;
5
- export type TJsonSchemaError = TLocalizedValidationError;
6
2
  export type TErrorFormat = 'json-schema' | 'standard-schema';
3
+ export interface TJsonSchemaError {
4
+ keyword: string;
5
+ schemaPath: string;
6
+ instancePath: string;
7
+ params: object;
8
+ message: string;
9
+ }
10
+ export interface TStandardSchemaError {
11
+ path: string[];
12
+ message: string;
13
+ }
7
14
  declare const Locale: typeof System.Locale;
8
15
  export type TErrorLocale = (Exclude<keyof typeof Locale, 'Get' | 'Set' | 'Reset'> & ({} & string));
9
16
  /** Internal */
10
17
  export declare function resolveErrorOptions(options?: Partial<TErrorOptions>): TErrorOptions;
11
18
  export interface TErrorOptions {
12
- /**
13
- * Specifies the error message generation format.
14
- *
15
- * Supported values are `json-schema` and `standard-schema`.
16
- *
17
- * Default: `json-schema`
18
- */
19
+ /** Specifies the error format. Default: `json-schema` */
19
20
  format: TErrorFormat;
20
- /**
21
- * Specifies the locale used when generating error messages.
22
- *
23
- * This setting applies only when used with TypeScript, JSON Schema,
24
- * or implementations that follow the Standard JSON Schema specification.
25
- * When the compiled type is Standard Schema only, the error message
26
- * will be whatever is returned from that library.
27
- *
28
- * Default: `en_US`
29
- */
21
+ /** Specifies the error format. Default: `en_US` */
30
22
  locale: TErrorLocale;
31
23
  }
32
- export type TErrorResult<Options extends Partial<TErrorOptions>> = (Options['format'] extends 'standard-schema' ? StandardSchemaV1.Issue[] : TLocalizedValidationError[]);
24
+ export type TErrorResult<Options extends Partial<TErrorOptions>> = (Options['format'] extends 'standard-schema' ? TStandardSchemaError[] : TJsonSchemaError[]);
33
25
  /** Abstract Base for all Validator types. */
34
26
  export declare abstract class Validator<Input extends unknown = unknown, Output extends unknown = unknown> {
35
27
  /** Returns the schema used to construct this validator */
@@ -1,7 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { System } from 'typebox/system';
3
3
  import { Validator as TBValidator } from 'typebox/compile';
4
- import { ParseError, errorToIssue } from '../../errors/index.mjs';
4
+ import { ParseError, errorToIssue, normalError } from '../../errors/index.mjs';
5
5
  import { Validator, resolveErrorOptions } from '../../validator.mjs';
6
6
  export class JsonSchemaValidator extends Validator {
7
7
  constructor(input) {
@@ -46,7 +46,7 @@ export class JsonSchemaValidator extends Validator {
46
46
  System.Locale.Set(System.Locale[config.locale]);
47
47
  const errors = this.validator.Errors(value);
48
48
  return (config.format === 'standard-schema'
49
- ? errors.map(error => errorToIssue(error))
50
- : errors);
49
+ ? errors.map(error => errorToIssue(normalError(error)))
50
+ : errors.map(error => normalError(error)));
51
51
  }
52
52
  }
@@ -1,2 +1,3 @@
1
1
  import { StandardJSONSchemaV1 } from '../../_standard/standard-schema.mjs';
2
- export declare function ResolveJsonSchema(input: StandardJSONSchemaV1): Record<string, unknown>;
2
+ /** (Internal) Resolves a JsonSchema representation (need discovery mechanism here) */
3
+ export declare function resolveJsonSchema(input: StandardJSONSchemaV1): Record<string, unknown>;
@@ -1,6 +1,6 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Guard } from 'typebox/guard';
3
- function AsOpenAPI3_0(input) {
3
+ function OpenAPI3_0(input) {
4
4
  try {
5
5
  return input['~standard'].jsonSchema.input({ target: 'openapi-3.0' });
6
6
  }
@@ -8,7 +8,7 @@ function AsOpenAPI3_0(input) {
8
8
  return undefined;
9
9
  }
10
10
  }
11
- function AsDraft7(input) {
11
+ function Draft7(input) {
12
12
  try {
13
13
  return input['~standard'].jsonSchema.input({ target: 'draft-07' });
14
14
  }
@@ -16,7 +16,7 @@ function AsDraft7(input) {
16
16
  return undefined;
17
17
  }
18
18
  }
19
- function AsDraft2020_12(input) {
19
+ function Draft2020_12(input) {
20
20
  try {
21
21
  return input['~standard'].jsonSchema.input({ target: 'draft-2020-12' });
22
22
  }
@@ -24,9 +24,9 @@ function AsDraft2020_12(input) {
24
24
  return undefined;
25
25
  }
26
26
  }
27
- // Standard JSON Schema should provide mechanism to query supported specifications
28
- export function ResolveJsonSchema(input) {
29
- const jsonschema = AsDraft2020_12(input) ?? AsDraft7(input) ?? AsOpenAPI3_0(input);
27
+ /** (Internal) Resolves a JsonSchema representation (need discovery mechanism here) */
28
+ export function resolveJsonSchema(input) {
29
+ const jsonschema = Draft2020_12(input) ?? Draft7(input) ?? OpenAPI3_0(input);
30
30
  if (Guard.IsUndefined(jsonschema))
31
31
  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.`);
32
32
  return jsonschema;
@@ -1,14 +1,14 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { System } from 'typebox/system';
3
3
  import { Validator, resolveErrorOptions } from '../../validator.mjs';
4
- import { ParseError, errorToIssue } from '../../errors/index.mjs';
4
+ import { ParseError, errorToIssue, normalError } from '../../errors/index.mjs';
5
5
  import { Validator as TBValidator } from 'typebox/compile';
6
- import { ResolveJsonSchema } from './resolve.mjs';
6
+ import { resolveJsonSchema } from './resolve.mjs';
7
7
  export class StandardJsonSchemaValidator extends Validator {
8
8
  constructor(input) {
9
9
  super();
10
10
  this.input = input;
11
- const schema = ResolveJsonSchema(input);
11
+ const schema = resolveJsonSchema(input);
12
12
  this.validator = new TBValidator({}, schema);
13
13
  }
14
14
  // ----------------------------------------------------------------
@@ -48,7 +48,7 @@ export class StandardJsonSchemaValidator extends Validator {
48
48
  System.Locale.Set(System.Locale[config.locale]);
49
49
  const errors = this.validator.Errors(value);
50
50
  return (config.format === 'standard-schema'
51
- ? errors.map(error => errorToIssue(error))
52
- : errors);
51
+ ? errors.map(error => errorToIssue(normalError(error)))
52
+ : errors.map(error => normalError(error)));
53
53
  }
54
54
  }
@@ -1,5 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
- import { ParseError, UnknownError, issueToError } from '../../errors/index.mjs';
2
+ import { ParseError, UnknownError, issueToError, normalIssue } from '../../errors/index.mjs';
3
3
  import { Validator, resolveErrorOptions } from '../../validator.mjs';
4
4
  export class StandardSchemaValidator extends Validator {
5
5
  constructor(input) {
@@ -47,7 +47,7 @@ export class StandardSchemaValidator extends Validator {
47
47
  const issues = (('issues' in result) ? result.issues : []);
48
48
  const config = resolveErrorOptions(options);
49
49
  return (config.format === 'json-schema'
50
- ? issues.map(issue => issueToError(issue))
51
- : issues.map(issue => ({ path: issue.path, message: issue.message })));
50
+ ? issues.map(issue => issueToError(normalIssue(issue)))
51
+ : issues.map(issue => normalIssue(issue)));
52
52
  }
53
53
  }
@@ -1,5 +1,5 @@
1
- import { Validator, type TErrorOptions, type TErrorResult } from '../../validator.mjs';
2
1
  import Type from 'typebox';
2
+ import { Validator, type TErrorOptions, type TErrorResult } from '../../validator.mjs';
3
3
  export declare class TypeScriptValidator<Input extends string, Schema extends Type.TSchema = Type.TScript<{}, Input>, Output extends unknown = Type.Static<Schema>> extends Validator<Input, Output> {
4
4
  private readonly validator;
5
5
  private readonly script;
@@ -1,9 +1,9 @@
1
1
  // deno-fmt-ignore-file
2
+ import Type from 'typebox';
2
3
  import { System } from 'typebox/system';
3
4
  import { Validator as TBValidator } from 'typebox/compile';
4
- import { ParseError, errorToIssue } from '../../errors/index.mjs';
5
+ import { ParseError, errorToIssue, normalError } from '../../errors/index.mjs';
5
6
  import { Validator, resolveErrorOptions } from '../../validator.mjs';
6
- import Type from 'typebox';
7
7
  export class TypeScriptValidator extends Validator {
8
8
  constructor(script) {
9
9
  super();
@@ -48,7 +48,7 @@ export class TypeScriptValidator extends Validator {
48
48
  System.Locale.Set(System.Locale[config.locale]);
49
49
  const errors = this.validator.Errors(value);
50
50
  return (config.format === 'standard-schema'
51
- ? errors.map(error => errorToIssue(error))
52
- : errors);
51
+ ? errors.map(error => errorToIssue(normalError(error)))
52
+ : errors.map(error => normalError(error)));
53
53
  }
54
54
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typedriver",
3
3
  "description": "High Performance Driver for Runtime Type System Integration",
4
- "version": "0.8.7",
4
+ "version": "0.8.8",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "json-schema",
package/readme.md CHANGED
@@ -34,8 +34,6 @@ const Vector3 = compile(`{
34
34
  z: number
35
35
  }`)
36
36
 
37
- declare const value: unknown
38
-
39
37
  const position = Vector3.parse(value) // const position: {
40
38
  // x: number,
41
39
  // y: number,
@@ -65,7 +63,7 @@ License MIT
65
63
  - [Locales](#Locales)
66
64
  - [Static](#Static)
67
65
  - [Script](#Script)
68
- - [Reflect](#Schema)
66
+ - [Reflect](#Reflect)
69
67
  - [Extensions](#Extensions)
70
68
  - [Accelerate](#Accelerate)
71
69
  - [Compression](#Compression)