typebox 1.0.17 → 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,84 +1,62 @@
1
1
  import { type TSchema } from './schema.mjs';
2
+ export declare class BaseNotImplemented extends Error {
3
+ readonly cause: {
4
+ type: Base;
5
+ method: string;
6
+ };
7
+ constructor(type: Base, method: string);
8
+ }
9
+ /** Base class for creating extension types. */
10
+ export declare class Base<Value extends unknown = unknown> implements TSchema {
11
+ readonly '~kind': 'Base';
12
+ readonly '~standard': StandardSchemaV1.Props<Value>;
13
+ constructor();
14
+ /** Checks a value or returns false if invalid */
15
+ Check(value: unknown): value is Value;
16
+ /** Returns errors for a value. Return an empty array if valid. */
17
+ Errors(value: unknown): object[];
18
+ /** Converts a value into this type */
19
+ Convert(value: unknown): unknown;
20
+ /** Cleans a value according to this type */
21
+ Clean(value: unknown): unknown;
22
+ /** Returns a default value for this type */
23
+ Default(value: unknown): unknown;
24
+ /** Creates a new instance of this type */
25
+ Create(): Value;
26
+ }
27
+ /** Returns true if the given value is a Base type. */
28
+ export declare function IsBase(value: unknown): value is Base;
2
29
  interface StandardSchemaV1<Input = unknown, Output = Input> {
3
- /** The Standard Schema properties. */
4
30
  readonly '~standard': StandardSchemaV1.Props<Input, Output>;
5
31
  }
6
32
  declare namespace StandardSchemaV1 {
7
- /** The Standard Schema properties interface. */
8
33
  export interface Props<Input = unknown, Output = Input> {
9
- /** The version number of the standard. */
10
34
  readonly version: 1;
11
- /** The vendor name of the schema library. */
12
35
  readonly vendor: string;
13
- /** Validates unknown input values. */
14
36
  readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
15
- /** Inferred types associated with the schema. */
16
37
  readonly types?: Types<Input, Output> | undefined;
17
38
  }
18
- /** The result interface of the validate function. */
19
39
  export type Result<Output> = SuccessResult<Output> | FailureResult;
20
- /** The result interface if validation succeeds. */
21
40
  export interface SuccessResult<Output> {
22
- /** The typed output value. */
23
41
  readonly value: Output;
24
- /** The non-existent issues. */
25
42
  readonly issues?: undefined;
26
43
  }
27
- /** The result interface if validation fails. */
28
44
  export interface FailureResult {
29
- /** The issues of failed validation. */
30
45
  readonly issues: ReadonlyArray<Issue>;
31
46
  }
32
- /** The issue interface of the failure output. */
33
47
  export interface Issue {
34
- /** The error message of the issue. */
35
48
  readonly message: string;
36
- /** The path of the issue, if any. */
37
49
  readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
38
50
  }
39
- /** The path segment interface of the issue. */
40
51
  export interface PathSegment {
41
- /** The key representing a path segment. */
42
52
  readonly key: PropertyKey;
43
53
  }
44
- /** The Standard Schema types interface. */
45
54
  export interface Types<Input = unknown, Output = Input> {
46
- /** The input type of the schema. */
47
55
  readonly input: Input;
48
- /** The output type of the schema. */
49
56
  readonly output: Output;
50
57
  }
51
- /** Infers the input type of a Standard Schema. */
52
58
  export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input'];
53
- /** Infers the output type of a Standard Schema. */
54
59
  export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output'];
55
60
  export {};
56
61
  }
57
- export declare class BaseNotImplemented extends Error {
58
- readonly cause: {
59
- type: Base;
60
- method: string;
61
- };
62
- constructor(type: Base, method: string);
63
- }
64
- /** Base class for creating extension types. */
65
- export declare class Base<Value extends unknown = unknown> implements TSchema {
66
- readonly '~kind': 'Base';
67
- readonly '~standard': StandardSchemaV1.Props<Value>;
68
- constructor();
69
- /** Checks a value or returns false if invalid */
70
- Check(value: unknown): value is Value;
71
- /** Returns errors for a value. Return an empty array if valid. */
72
- Errors(value: unknown): object[];
73
- /** Converts a value into this type */
74
- Convert(value: unknown): unknown;
75
- /** Cleans a value according to this type */
76
- Clean(value: unknown): unknown;
77
- /** Returns a default value for this type */
78
- Default(value: unknown): unknown;
79
- /** Creates a new instance of this type */
80
- Create(): Value;
81
- }
82
- /** Returns true if the given value is a Base type. */
83
- export declare function IsBase(value: unknown): value is Base;
84
62
  export {};
@@ -1,34 +1,9 @@
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
- // --------------------------------------------------------
4
- // Standard Schema Factory
5
- // --------------------------------------------------------
6
- function Value(value) {
7
- return { value };
8
- }
9
- function Issues(issues) {
10
- // We cannot guarantee that the caller will pass an object with an
11
- // error message, but it is generally implied. Additionally, we do
12
- // not want StandardSchema interfaces proliferating throughout the
13
- // codebase; they must remain contained within this module only.
14
- return { issues };
15
- }
16
- // ------------------------------------------------------------------------------------
17
- // StandardValidatorV1
18
- // ------------------------------------------------------------------------------------
19
- class StandardValidatorV1 {
20
- constructor(check, errors) {
21
- this.check = check;
22
- this.errors = errors;
23
- this.vendor = 'typebox';
24
- this.version = 1;
25
- this.validate = (value) => {
26
- return (this.check(value)) ? Value(value) : Issues(this.errors(value));
27
- };
28
- }
29
- }
30
5
  // ------------------------------------------------------------------
31
- // BaseError
6
+ // BaseNotImplemented
32
7
  // ------------------------------------------------------------------
33
8
  export class BaseNotImplemented extends Error {
34
9
  constructor(type, method) {
@@ -41,13 +16,36 @@ export class BaseNotImplemented extends Error {
41
16
  });
42
17
  }
43
18
  }
19
+ // ------------------------------------------------------------------------------------
20
+ // BaseValidator
21
+ // ------------------------------------------------------------------------------------
22
+ class BaseValidator {
23
+ constructor(check, errors) {
24
+ this.check = check;
25
+ this.errors = errors;
26
+ this.vendor = 'typebox';
27
+ this.version = 1;
28
+ this.validate = (value) => {
29
+ return this.check(value)
30
+ ? this.Success(value)
31
+ : this.Failure(this.errors(value));
32
+ };
33
+ }
34
+ Success(value) {
35
+ return { value };
36
+ }
37
+ Failure(errors) {
38
+ const issues = errors.reduce((result, error) => [...result, ...CreateIssues(error)], []);
39
+ return { issues };
40
+ }
41
+ }
44
42
  // ------------------------------------------------------------------
45
- // Base
43
+ // Type
46
44
  // ------------------------------------------------------------------
47
45
  /** Base class for creating extension types. */
48
46
  export class Base {
49
47
  constructor() {
50
- 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));
51
49
  const configuration = {
52
50
  writable: false,
53
51
  configurable: false,
@@ -88,3 +86,54 @@ export class Base {
88
86
  export function IsBase(value) {
89
87
  return IsKind(value, 'Base');
90
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.17",
4
+ "version": "1.0.18",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"