typebox 1.0.16 → 1.0.18

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,211 +1,216 @@
1
- export type TValidationError = TAdditionalPropertiesError | TAnyOfError | TBooleanError | TConstError | TContainsError | TDependenciesError | TDependentRequiredError | TEnumError | TExclusiveMaximumError | TExclusiveMinimumError | TFormatError | TIfError | TMaximumError | TMaxItemsError | TMaxLengthError | TMaxPropertiesError | TMinimumError | TMinItemsError | TMinLengthError | TMinPropertiesError | TMultipleOfError | TNotError | TOneOfError | TPatternError | TPropertyNamesError | TRefineError | TStandardV1Error | TRequiredError | TTypeError | TUnevaluatedItemsError | TUnevaluatedPropertiesError | TUniqueItemsError;
1
+ export type TValidationError = TAdditionalPropertiesError | TAnyOfError | TBooleanError | TConstError | TContainsError | TDependenciesError | TDependentRequiredError | TEnumError | TExclusiveMaximumError | TExclusiveMinimumError | TFormatError | TIfError | TMaximumError | TMaxItemsError | TMaxLengthError | TMaxPropertiesError | TMinimumError | TMinItemsError | TMinLengthError | TMinPropertiesError | TMultipleOfError | TNotError | TOneOfError | TPatternError | TPropertyNamesError | TRefineError | TRequiredError | TStandardSchemaV1Error | TTypeError | TUnevaluatedItemsError | TUnevaluatedPropertiesError | TUniqueItemsError;
2
+ export declare function IsValidationError(value: unknown): value is TValidationError;
2
3
  export type TLocalizedValidationError = TValidationError & {
3
4
  message: string;
4
5
  };
6
+ export declare function IsLocalizedValidationError(value: unknown): value is TLocalizedValidationError;
5
7
  export type TLocalizedValidationMessageCallback = (error: TValidationError) => string;
6
- export interface TBaseError {
8
+ export interface TValidationErrorBase {
7
9
  keyword: string;
8
10
  schemaPath: string;
9
11
  instancePath: string;
10
12
  params: object;
11
13
  }
12
- export interface TAdditionalPropertiesError extends TBaseError {
14
+ export interface TAdditionalPropertiesError extends TValidationErrorBase {
13
15
  keyword: 'additionalProperties';
14
16
  params: {
15
17
  additionalProperties: string[];
16
18
  };
17
19
  }
18
- export interface TAnyOfError extends TBaseError {
20
+ export interface TAnyOfError extends TValidationErrorBase {
19
21
  keyword: 'anyOf';
20
22
  params: {};
21
23
  }
22
- export interface TBooleanError extends TBaseError {
24
+ export interface TBooleanError extends TValidationErrorBase {
23
25
  keyword: 'boolean';
24
26
  params: {};
25
27
  }
26
- export interface TConstError extends TBaseError {
28
+ export interface TConstError extends TValidationErrorBase {
27
29
  keyword: 'const';
28
30
  params: {
29
31
  allowedValue: unknown;
30
32
  };
31
33
  }
32
- export interface TContainsError extends TBaseError {
34
+ export interface TContainsError extends TValidationErrorBase {
33
35
  keyword: 'contains';
34
36
  params: {
35
37
  minContains: number;
36
38
  maxContains?: number;
37
39
  };
38
40
  }
39
- export interface TDependenciesError extends TBaseError {
41
+ export interface TDependenciesError extends TValidationErrorBase {
40
42
  keyword: 'dependencies';
41
43
  params: {
42
44
  property: string;
43
45
  dependencies: string[];
44
46
  };
45
47
  }
46
- export interface TDependentRequiredError extends TBaseError {
48
+ export interface TDependentRequiredError extends TValidationErrorBase {
47
49
  keyword: 'dependentRequired';
48
50
  params: {
49
51
  property: string;
50
52
  dependencies: string[];
51
53
  };
52
54
  }
53
- export interface TEnumError extends TBaseError {
55
+ export interface TEnumError extends TValidationErrorBase {
54
56
  keyword: 'enum';
55
57
  params: {
56
58
  allowedValues: unknown[];
57
59
  };
58
60
  }
59
- export interface TExclusiveMaximumError extends TBaseError {
61
+ export interface TExclusiveMaximumError extends TValidationErrorBase {
60
62
  keyword: 'exclusiveMaximum';
61
63
  params: {
62
64
  comparison: '<';
63
65
  limit: number | bigint;
64
66
  };
65
67
  }
66
- export interface TExclusiveMinimumError extends TBaseError {
68
+ export interface TExclusiveMinimumError extends TValidationErrorBase {
67
69
  keyword: 'exclusiveMinimum';
68
70
  params: {
69
71
  comparison: '>';
70
72
  limit: number | bigint;
71
73
  };
72
74
  }
73
- export interface TFormatError extends TBaseError {
75
+ export interface TFormatError extends TValidationErrorBase {
74
76
  keyword: 'format';
75
77
  params: {
76
78
  format: string;
77
79
  };
78
80
  }
79
- export interface TIfError extends TBaseError {
81
+ export interface TIfError extends TValidationErrorBase {
80
82
  keyword: 'if';
81
83
  params: {
82
84
  failingKeyword: 'then' | 'else';
83
85
  };
84
86
  }
85
- export interface TMaximumError extends TBaseError {
87
+ export interface TMaximumError extends TValidationErrorBase {
86
88
  keyword: 'maximum';
87
89
  params: {
88
90
  comparison: '<=';
89
91
  limit: number | bigint;
90
92
  };
91
93
  }
92
- export interface TMaxItemsError extends TBaseError {
94
+ export interface TMaxItemsError extends TValidationErrorBase {
93
95
  keyword: 'maxItems';
94
96
  params: {
95
97
  limit: number;
96
98
  };
97
99
  }
98
- export interface TMaxLengthError extends TBaseError {
100
+ export interface TMaxLengthError extends TValidationErrorBase {
99
101
  keyword: 'maxLength';
100
102
  params: {
101
103
  limit: number;
102
104
  };
103
105
  }
104
- export interface TMaxPropertiesError extends TBaseError {
106
+ export interface TMaxPropertiesError extends TValidationErrorBase {
105
107
  keyword: 'maxProperties';
106
108
  params: {
107
109
  limit: number;
108
110
  };
109
111
  }
110
- export interface TMinimumError extends TBaseError {
112
+ export interface TMinimumError extends TValidationErrorBase {
111
113
  keyword: 'minimum';
112
114
  params: {
113
115
  comparison: '>=';
114
116
  limit: number | bigint;
115
117
  };
116
118
  }
117
- export interface TMinItemsError extends TBaseError {
119
+ export interface TMinItemsError extends TValidationErrorBase {
118
120
  keyword: 'minItems';
119
121
  params: {
120
122
  limit: number;
121
123
  };
122
124
  }
123
- export interface TMinLengthError extends TBaseError {
125
+ export interface TMinLengthError extends TValidationErrorBase {
124
126
  keyword: 'minLength';
125
127
  params: {
126
128
  limit: number;
127
129
  };
128
130
  }
129
- export interface TMinPropertiesError extends TBaseError {
131
+ export interface TMinPropertiesError extends TValidationErrorBase {
130
132
  keyword: 'minProperties';
131
133
  params: {
132
134
  limit: number;
133
135
  };
134
136
  }
135
- export interface TMultipleOfError extends TBaseError {
137
+ export interface TMultipleOfError extends TValidationErrorBase {
136
138
  keyword: 'multipleOf';
137
139
  params: {
138
140
  multipleOf: number | bigint;
139
141
  };
140
142
  }
141
- export interface TMinimumError extends TBaseError {
143
+ export interface TMinimumError extends TValidationErrorBase {
142
144
  keyword: 'minimum';
143
145
  params: {
144
146
  comparison: '>=';
145
147
  limit: number | bigint;
146
148
  };
147
149
  }
148
- export interface TNotError extends TBaseError {
150
+ export interface TNotError extends TValidationErrorBase {
149
151
  keyword: 'not';
150
152
  params: {};
151
153
  }
152
- export interface TOneOfError extends TBaseError {
154
+ export interface TOneOfError extends TValidationErrorBase {
153
155
  keyword: 'oneOf';
154
156
  params: {
155
157
  passingSchemas: number[];
156
158
  };
157
159
  }
158
- export interface TPatternError extends TBaseError {
160
+ export interface TPatternError extends TValidationErrorBase {
159
161
  keyword: 'pattern';
160
162
  params: {
161
163
  pattern: string | RegExp;
162
164
  };
163
165
  }
164
- export interface TPropertyNamesError extends TBaseError {
166
+ export interface TPropertyNamesError extends TValidationErrorBase {
165
167
  keyword: 'propertyNames';
166
168
  params: {
167
169
  propertyNames: string[];
168
170
  };
169
171
  }
170
- export interface TRefineError extends TBaseError {
172
+ export interface TRefineError extends TValidationErrorBase {
171
173
  keyword: '~refine';
172
174
  params: {
173
175
  index: number;
174
176
  message: string;
175
177
  };
176
178
  }
177
- export interface TRequiredError extends TBaseError {
179
+ export interface TRequiredError extends TValidationErrorBase {
178
180
  keyword: 'required';
179
181
  params: {
180
182
  requiredProperties: string[];
181
183
  };
182
184
  }
183
- export interface TStandardV1Error extends TBaseError {
185
+ export interface TStandardSchemaV1Error extends TValidationErrorBase {
184
186
  keyword: '~standard';
185
187
  params: {
186
188
  vendor: string;
187
- issues: object[];
189
+ issues: {
190
+ message: string;
191
+ path?: string[];
192
+ }[];
188
193
  };
189
194
  }
190
- export interface TTypeError extends TBaseError {
195
+ export interface TTypeError extends TValidationErrorBase {
191
196
  keyword: 'type';
192
197
  params: {
193
198
  type: string | string[];
194
199
  };
195
200
  }
196
- export interface TUnevaluatedItemsError extends TBaseError {
201
+ export interface TUnevaluatedItemsError extends TValidationErrorBase {
197
202
  keyword: 'unevaluatedItems';
198
203
  params: {
199
204
  unevaluatedItems: number[];
200
205
  };
201
206
  }
202
- export interface TUnevaluatedPropertiesError extends TBaseError {
207
+ export interface TUnevaluatedPropertiesError extends TValidationErrorBase {
203
208
  keyword: 'unevaluatedProperties';
204
209
  params: {
205
210
  unevaluatedProperties: PropertyKey[];
206
211
  };
207
212
  }
208
- export interface TUniqueItemsError extends TBaseError {
213
+ export interface TUniqueItemsError extends TValidationErrorBase {
209
214
  keyword: 'uniqueItems';
210
215
  params: {
211
216
  duplicateItems: number[];
@@ -1 +1,17 @@
1
- export {};
1
+ import { Guard } from '../guard/index.mjs';
2
+ export function IsValidationError(value) {
3
+ return Guard.IsObject(value) &&
4
+ Guard.HasPropertyKey(value, 'keyword') &&
5
+ Guard.HasPropertyKey(value, 'schemaPath') &&
6
+ Guard.HasPropertyKey(value, 'instancePath') &&
7
+ Guard.HasPropertyKey(value, 'params') &&
8
+ Guard.IsString(value.keyword) &&
9
+ Guard.IsString(value.schemaPath) &&
10
+ Guard.IsString(value.instancePath) &&
11
+ Guard.IsObject(value.params);
12
+ }
13
+ export function IsLocalizedValidationError(value) {
14
+ return IsValidationError(value) &&
15
+ Guard.HasPropertyKey(value, 'message') &&
16
+ Guard.IsString(value.message);
17
+ }
@@ -1,26 +1,15 @@
1
1
  import { type TSchema } from './schema.mjs';
2
- type TResult<Value extends unknown = unknown> = TValue<Value> | TIssues;
3
- interface TValue<Value extends unknown = unknown> {
4
- value: Value;
5
- }
6
- interface TIssues {
7
- issues: object[];
8
- }
9
- declare class StandardValidatorV1<Value extends unknown = unknown> {
10
- private readonly check;
11
- private readonly errors;
12
- readonly vendor = "typebox";
13
- readonly version = 1;
14
- constructor(check: (value: unknown) => boolean, errors: (value: unknown) => object[]);
15
- readonly validate: (value: unknown) => TResult<Value>;
16
- }
17
2
  export declare class BaseNotImplemented extends Error {
18
- constructor(type: Base, override: string);
3
+ readonly cause: {
4
+ type: Base;
5
+ method: string;
6
+ };
7
+ constructor(type: Base, method: string);
19
8
  }
20
9
  /** Base class for creating extension types. */
21
10
  export declare class Base<Value extends unknown = unknown> implements TSchema {
22
11
  readonly '~kind': 'Base';
23
- readonly '~standard': StandardValidatorV1<Value>;
12
+ readonly '~standard': StandardSchemaV1.Props<Value>;
24
13
  constructor();
25
14
  /** Checks a value or returns false if invalid */
26
15
  Check(value: unknown): value is Value;
@@ -37,4 +26,37 @@ export declare class Base<Value extends unknown = unknown> implements TSchema {
37
26
  }
38
27
  /** Returns true if the given value is a Base type. */
39
28
  export declare function IsBase(value: unknown): value is Base;
29
+ interface StandardSchemaV1<Input = unknown, Output = Input> {
30
+ readonly '~standard': StandardSchemaV1.Props<Input, Output>;
31
+ }
32
+ declare namespace StandardSchemaV1 {
33
+ export interface Props<Input = unknown, Output = Input> {
34
+ readonly version: 1;
35
+ readonly vendor: string;
36
+ readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
37
+ readonly types?: Types<Input, Output> | undefined;
38
+ }
39
+ export type Result<Output> = SuccessResult<Output> | FailureResult;
40
+ export interface SuccessResult<Output> {
41
+ readonly value: Output;
42
+ readonly issues?: undefined;
43
+ }
44
+ export interface FailureResult {
45
+ readonly issues: ReadonlyArray<Issue>;
46
+ }
47
+ export interface Issue {
48
+ readonly message: string;
49
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
50
+ }
51
+ export interface PathSegment {
52
+ readonly key: PropertyKey;
53
+ }
54
+ export interface Types<Input = unknown, Output = Input> {
55
+ readonly input: Input;
56
+ readonly output: Output;
57
+ }
58
+ export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input'];
59
+ export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output'];
60
+ export {};
61
+ }
40
62
  export {};
@@ -1,47 +1,57 @@
1
1
  // deno-fmt-ignore-file
2
+ import { IsLocalizedValidationError } from '../../error/index.mjs';
3
+ import { Guard } from '../../guard/index.mjs';
2
4
  import { IsKind } from './schema.mjs';
3
- function Value(value) {
4
- return { value };
5
- }
6
- function Issues(issues) {
7
- return { issues };
5
+ // ------------------------------------------------------------------
6
+ // BaseNotImplemented
7
+ // ------------------------------------------------------------------
8
+ export class BaseNotImplemented extends Error {
9
+ constructor(type, method) {
10
+ super(`Base type does not implement the '${method}' function`);
11
+ Object.defineProperty(this, 'cause', {
12
+ value: { type, method },
13
+ writable: false,
14
+ configurable: false,
15
+ enumerable: false
16
+ });
17
+ }
8
18
  }
9
19
  // ------------------------------------------------------------------------------------
10
- // StandardValidatorV1
20
+ // BaseValidator
11
21
  // ------------------------------------------------------------------------------------
12
- class StandardValidatorV1 {
22
+ class BaseValidator {
13
23
  constructor(check, errors) {
14
24
  this.check = check;
15
25
  this.errors = errors;
16
26
  this.vendor = 'typebox';
17
27
  this.version = 1;
18
28
  this.validate = (value) => {
19
- return (this.check(value)) ? Value(value) : Issues(this.errors(value));
29
+ return this.check(value)
30
+ ? this.Success(value)
31
+ : this.Failure(this.errors(value));
20
32
  };
21
33
  }
22
- }
23
- // ------------------------------------------------------------------
24
- // BaseError
25
- // ------------------------------------------------------------------
26
- export class BaseNotImplemented extends Error {
27
- constructor(type, override) {
28
- // @ts-ignore - options supported in deno, node, etc.
29
- super(`Base type does not implement '${override}' function`, { cause: type });
34
+ Success(value) {
35
+ return { value };
36
+ }
37
+ Failure(errors) {
38
+ const issues = errors.reduce((result, error) => [...result, ...CreateIssues(error)], []);
39
+ return { issues };
30
40
  }
31
41
  }
32
42
  // ------------------------------------------------------------------
33
- // Base
43
+ // Type
34
44
  // ------------------------------------------------------------------
35
45
  /** Base class for creating extension types. */
36
46
  export class Base {
37
47
  constructor() {
38
- const validator = new StandardValidatorV1(value => this.Check(value), value => this.Errors(value));
48
+ const validator = new BaseValidator((value) => this.Check(value), (value) => this.Errors(value));
39
49
  const configuration = {
40
50
  writable: false,
41
51
  configurable: false,
42
52
  enumerable: false
43
53
  };
44
- Object.defineProperty(this, '~kind', { ...configuration, value: 'Base', });
54
+ Object.defineProperty(this, '~kind', { ...configuration, value: 'Base' });
45
55
  Object.defineProperty(this, '~standard', { ...configuration, value: validator });
46
56
  }
47
57
  /** Checks a value or returns false if invalid */
@@ -76,3 +86,54 @@ export class Base {
76
86
  export function IsBase(value) {
77
87
  return IsKind(value, 'Base');
78
88
  }
89
+ // --------------------------------------------------------
90
+ // StandardSchema: PathSegments
91
+ // --------------------------------------------------------
92
+ function PathSegments(pointer) {
93
+ if (Guard.IsEqual(pointer.length, 0))
94
+ return [];
95
+ return pointer.slice(1).split("/").map(segment => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
96
+ }
97
+ // --------------------------------------------------------
98
+ // IsStandardSchemaV1Error
99
+ // --------------------------------------------------------
100
+ function IsStandardSchemaV1Error(error) {
101
+ return Guard.IsEqual(error.keyword, '~standard');
102
+ }
103
+ // --------------------------------------------------------
104
+ // IssuesFromLocalizedError
105
+ // --------------------------------------------------------
106
+ function IssuesFromStandardSchemaV1Error(error) {
107
+ const leading = PathSegments(error.instancePath);
108
+ const issues = Guard.IsArray(error.params.issues) ? error.params.issues : [];
109
+ return issues.map(issue => {
110
+ const message = Guard.IsString(issue.message) ? issue.message : 'unknown';
111
+ const path = Guard.IsArray(issue.path) ? [...leading, ...issue.path] : leading;
112
+ return { message, path };
113
+ });
114
+ }
115
+ function IssuesFromRegularError(error) {
116
+ const path = PathSegments(error.instancePath);
117
+ return [{ path, message: error.message }];
118
+ }
119
+ function IssuesFromLocalizedError(error) {
120
+ return IsStandardSchemaV1Error(error)
121
+ ? IssuesFromStandardSchemaV1Error(error)
122
+ : IssuesFromRegularError(error);
123
+ }
124
+ // --------------------------------------------------------
125
+ // IssuesFromUnknown
126
+ // --------------------------------------------------------
127
+ function IssuesFromUnknown(error) {
128
+ const path = Guard.HasPropertyKey(error, 'path') && Guard.IsArray(error.path) && error.path.every(segment => Guard.IsString(segment)) ? error.path : [];
129
+ const message = Guard.HasPropertyKey(error, 'message') && Guard.IsString(error.message) ? error.message : 'unknown';
130
+ return [{ path, message }];
131
+ }
132
+ // --------------------------------------------------------
133
+ // CreateIssues
134
+ // --------------------------------------------------------
135
+ function CreateIssues(error) {
136
+ return IsLocalizedValidationError(error)
137
+ ? IssuesFromLocalizedError(error)
138
+ : IssuesFromUnknown(error);
139
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "A Runtime Type System for JavaScript",
4
- "version": "1.0.16",
4
+ "version": "1.0.18",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"