@sinclair/typebox 0.30.4 → 0.31.0-dev-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,48 @@
1
+ import { ValueErrorType } from '../errors/errors';
1
2
  import * as Types from '../typebox';
2
- export declare class TypeSystemDuplicateTypeKind extends Error {
3
+ export declare class TypeSystemDuplicateTypeKind extends Types.TypeBoxError {
3
4
  constructor(kind: string);
4
5
  }
5
- export declare class TypeSystemDuplicateFormat extends Error {
6
+ export declare class TypeSystemDuplicateFormat extends Types.TypeBoxError {
6
7
  constructor(kind: string);
7
8
  }
8
9
  /** Creates user defined types and formats and provides overrides for value checking behaviours */
9
10
  export declare namespace TypeSystem {
11
+ /** Creates a new type */
12
+ function Type<Type, Options = Record<PropertyKey, unknown>>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => Types.TUnsafe<Type>;
13
+ /** Creates a new string format */
14
+ function Format<F extends string>(format: F, check: (value: string) => boolean): F;
15
+ }
16
+ /** Manages error message providers */
17
+ export declare namespace TypeSystemErrorFunction {
18
+ /** Resets the error message function to en-us */
19
+ function Reset(): void;
20
+ /** Sets the error message function used to generate error messages */
21
+ function Set(callback: ErrorFunction): void;
22
+ /** Gets the error message function */
23
+ function Get(): ErrorFunction;
24
+ }
25
+ /** Shared assertion routines used by the value and errors modules */
26
+ export declare namespace TypeSystemPolicy {
10
27
  /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */
11
28
  let ExactOptionalPropertyTypes: boolean;
12
29
  /** Sets whether arrays should be treated as a kind of objects. The default is `false` */
13
- let AllowArrayObjects: boolean;
30
+ let AllowArrayObject: boolean;
14
31
  /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */
15
32
  let AllowNaN: boolean;
16
33
  /** Sets whether `null` should validate for void types. The default is `false` */
17
- let AllowVoidNull: boolean;
18
- /** Creates a new type */
19
- function Type<Type, Options = object>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => Types.TUnsafe<Type>;
20
- /** Creates a new string format */
21
- function Format<F extends string>(format: F, check: (value: string) => boolean): F;
34
+ let AllowNullVoid: boolean;
35
+ /** Asserts this value using the ExactOptionalPropertyTypes policy */
36
+ function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string): boolean;
37
+ /** Asserts this value using the AllowArrayObjects policy */
38
+ function IsObjectLike(value: unknown): value is Record<keyof any, unknown>;
39
+ /** Asserts this value as a record using the AllowArrayObjects policy */
40
+ function IsRecordLike(value: unknown): value is Record<keyof any, unknown>;
41
+ /** Asserts this value using the AllowNaN policy */
42
+ function IsNumberLike(value: unknown): value is number;
43
+ /** Asserts this value using the AllowVoidNull policy */
44
+ function IsVoidLike(value: unknown): value is void;
22
45
  }
46
+ export type ErrorFunction = (schema: Types.TSchema, type: ValueErrorType) => string;
47
+ /** Creates an error message using en-US as the default locale */
48
+ export declare function DefaultErrorFunction(schema: Types.TSchema, errorType: ValueErrorType): string;
package/system/system.js CHANGED
@@ -27,31 +27,31 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeSystem = exports.TypeSystemDuplicateFormat = exports.TypeSystemDuplicateTypeKind = void 0;
30
+ exports.DefaultErrorFunction = exports.TypeSystemPolicy = exports.TypeSystemErrorFunction = exports.TypeSystem = exports.TypeSystemDuplicateFormat = exports.TypeSystemDuplicateTypeKind = void 0;
31
+ const guard_1 = require("../value/guard");
32
+ const errors_1 = require("../errors/errors");
31
33
  const Types = require("../typebox");
32
- class TypeSystemDuplicateTypeKind extends Error {
34
+ // --------------------------------------------------------------------------
35
+ // Errors
36
+ // --------------------------------------------------------------------------
37
+ class TypeSystemDuplicateTypeKind extends Types.TypeBoxError {
33
38
  constructor(kind) {
34
39
  super(`Duplicate type kind '${kind}' detected`);
35
40
  }
36
41
  }
37
42
  exports.TypeSystemDuplicateTypeKind = TypeSystemDuplicateTypeKind;
38
- class TypeSystemDuplicateFormat extends Error {
43
+ class TypeSystemDuplicateFormat extends Types.TypeBoxError {
39
44
  constructor(kind) {
40
45
  super(`Duplicate string format '${kind}' detected`);
41
46
  }
42
47
  }
43
48
  exports.TypeSystemDuplicateFormat = TypeSystemDuplicateFormat;
49
+ // -------------------------------------------------------------------------------------------
50
+ // TypeSystem
51
+ // -------------------------------------------------------------------------------------------
44
52
  /** Creates user defined types and formats and provides overrides for value checking behaviours */
45
53
  var TypeSystem;
46
54
  (function (TypeSystem) {
47
- /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */
48
- TypeSystem.ExactOptionalPropertyTypes = false;
49
- /** Sets whether arrays should be treated as a kind of objects. The default is `false` */
50
- TypeSystem.AllowArrayObjects = false;
51
- /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */
52
- TypeSystem.AllowNaN = false;
53
- /** Sets whether `null` should validate for void types. The default is `false` */
54
- TypeSystem.AllowVoidNull = false;
55
55
  /** Creates a new type */
56
56
  function Type(kind, check) {
57
57
  if (Types.TypeRegistry.Has(kind))
@@ -69,3 +69,206 @@ var TypeSystem;
69
69
  }
70
70
  TypeSystem.Format = Format;
71
71
  })(TypeSystem || (exports.TypeSystem = TypeSystem = {}));
72
+ // --------------------------------------------------------------------------
73
+ // TypeSystemErrorFunction
74
+ // --------------------------------------------------------------------------
75
+ /** Manages error message providers */
76
+ var TypeSystemErrorFunction;
77
+ (function (TypeSystemErrorFunction) {
78
+ let errorMessageFunction = DefaultErrorFunction;
79
+ /** Resets the error message function to en-us */
80
+ function Reset() {
81
+ errorMessageFunction = DefaultErrorFunction;
82
+ }
83
+ TypeSystemErrorFunction.Reset = Reset;
84
+ /** Sets the error message function used to generate error messages */
85
+ function Set(callback) {
86
+ errorMessageFunction = callback;
87
+ }
88
+ TypeSystemErrorFunction.Set = Set;
89
+ /** Gets the error message function */
90
+ function Get() {
91
+ return errorMessageFunction;
92
+ }
93
+ TypeSystemErrorFunction.Get = Get;
94
+ })(TypeSystemErrorFunction || (exports.TypeSystemErrorFunction = TypeSystemErrorFunction = {}));
95
+ // --------------------------------------------------------------------------
96
+ // TypeSystemPolicy
97
+ // --------------------------------------------------------------------------
98
+ /** Shared assertion routines used by the value and errors modules */
99
+ var TypeSystemPolicy;
100
+ (function (TypeSystemPolicy) {
101
+ /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */
102
+ TypeSystemPolicy.ExactOptionalPropertyTypes = false;
103
+ /** Sets whether arrays should be treated as a kind of objects. The default is `false` */
104
+ TypeSystemPolicy.AllowArrayObject = false;
105
+ /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */
106
+ TypeSystemPolicy.AllowNaN = false;
107
+ /** Sets whether `null` should validate for void types. The default is `false` */
108
+ TypeSystemPolicy.AllowNullVoid = false;
109
+ /** Asserts this value using the ExactOptionalPropertyTypes policy */
110
+ function IsExactOptionalProperty(value, key) {
111
+ return TypeSystemPolicy.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
112
+ }
113
+ TypeSystemPolicy.IsExactOptionalProperty = IsExactOptionalProperty;
114
+ /** Asserts this value using the AllowArrayObjects policy */
115
+ function IsObjectLike(value) {
116
+ const isObject = (0, guard_1.IsObject)(value);
117
+ return TypeSystemPolicy.AllowArrayObject ? isObject : isObject && !(0, guard_1.IsArray)(value);
118
+ }
119
+ TypeSystemPolicy.IsObjectLike = IsObjectLike;
120
+ /** Asserts this value as a record using the AllowArrayObjects policy */
121
+ function IsRecordLike(value) {
122
+ return IsObjectLike(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
123
+ }
124
+ TypeSystemPolicy.IsRecordLike = IsRecordLike;
125
+ /** Asserts this value using the AllowNaN policy */
126
+ function IsNumberLike(value) {
127
+ const isNumber = (0, guard_1.IsNumber)(value);
128
+ return TypeSystemPolicy.AllowNaN ? isNumber : isNumber && Number.isFinite(value);
129
+ }
130
+ TypeSystemPolicy.IsNumberLike = IsNumberLike;
131
+ /** Asserts this value using the AllowVoidNull policy */
132
+ function IsVoidLike(value) {
133
+ const isUndefined = (0, guard_1.IsUndefined)(value);
134
+ return TypeSystemPolicy.AllowNullVoid ? isUndefined || value === null : isUndefined;
135
+ }
136
+ TypeSystemPolicy.IsVoidLike = IsVoidLike;
137
+ })(TypeSystemPolicy || (exports.TypeSystemPolicy = TypeSystemPolicy = {}));
138
+ // --------------------------------------------------------------------------
139
+ // DefaultErrorFunction
140
+ // --------------------------------------------------------------------------
141
+ /** Creates an error message using en-US as the default locale */
142
+ function DefaultErrorFunction(schema, errorType) {
143
+ switch (errorType) {
144
+ case errors_1.ValueErrorType.ArrayContains:
145
+ return 'Expected array to contain at least one matching value';
146
+ case errors_1.ValueErrorType.ArrayMaxContains:
147
+ return `Expected array to contain no more than ${schema.maxContains} matching values`;
148
+ case errors_1.ValueErrorType.ArrayMinContains:
149
+ return `Expected array to contain at least ${schema.minContains} matching values`;
150
+ case errors_1.ValueErrorType.ArrayMaxItems:
151
+ return `Expected array length to be less or equal to ${schema.maxItems}`;
152
+ case errors_1.ValueErrorType.ArrayMinItems:
153
+ return `Expected array length to be greater or equal to ${schema.minItems}`;
154
+ case errors_1.ValueErrorType.ArrayUniqueItems:
155
+ return 'Expected array elements to be unique';
156
+ case errors_1.ValueErrorType.Array:
157
+ return 'Expected array';
158
+ case errors_1.ValueErrorType.AsyncIterator:
159
+ return 'Expected AsyncIterator';
160
+ case errors_1.ValueErrorType.BigIntExclusiveMaximum:
161
+ return `Expected bigint to be less than ${schema.exclusiveMaximum}`;
162
+ case errors_1.ValueErrorType.BigIntExclusiveMinimum:
163
+ return `Expected bigint to be greater than ${schema.exclusiveMinimum}`;
164
+ case errors_1.ValueErrorType.BigIntMaximum:
165
+ return `Expected bigint to be less or equal to ${schema.maximum}`;
166
+ case errors_1.ValueErrorType.BigIntMinimum:
167
+ return `Expected bigint to be greater or equal to ${schema.minimum}`;
168
+ case errors_1.ValueErrorType.BigIntMultipleOf:
169
+ return `Expected bigint to be a multiple of ${schema.multipleOf}`;
170
+ case errors_1.ValueErrorType.BigInt:
171
+ return 'Expected bigint';
172
+ case errors_1.ValueErrorType.Boolean:
173
+ return 'Expected boolean';
174
+ case errors_1.ValueErrorType.DateExclusiveMinimumTimestamp:
175
+ return `Expected Date timestamp to be greater than ${schema.exclusiveMinimumTimestamp}`;
176
+ case errors_1.ValueErrorType.DateExclusiveMaximumTimestamp:
177
+ return `Expected Date timestamp to be less than ${schema.exclusiveMaximumTimestamp}`;
178
+ case errors_1.ValueErrorType.DateMinimumTimestamp:
179
+ return `Expected Date timestamp to be greater or equal to ${schema.minimumTimestamp}`;
180
+ case errors_1.ValueErrorType.DateMaximumTimestamp:
181
+ return `Expected Date timestamp to be less or equal to ${schema.maximumTimestamp}`;
182
+ case errors_1.ValueErrorType.DateMultipleOfTimestamp:
183
+ return `Expected Date timestamp to be a multiple of ${schema.multipleOfTimestamp}`;
184
+ case errors_1.ValueErrorType.Date:
185
+ return 'Expected Date';
186
+ case errors_1.ValueErrorType.Function:
187
+ return 'Expected function';
188
+ case errors_1.ValueErrorType.IntegerExclusiveMaximum:
189
+ return `Expected integer to be less than ${schema.exclusiveMaximum}`;
190
+ case errors_1.ValueErrorType.IntegerExclusiveMinimum:
191
+ return `Expected integer to be greater than ${schema.exclusiveMinimum}`;
192
+ case errors_1.ValueErrorType.IntegerMaximum:
193
+ return `Expected integer to be less or equal to ${schema.maximum}`;
194
+ case errors_1.ValueErrorType.IntegerMinimum:
195
+ return `Expected integer to be greater or equal to ${schema.minimum}`;
196
+ case errors_1.ValueErrorType.IntegerMultipleOf:
197
+ return `Expected integer to be a multiple of ${schema.multipleOf}`;
198
+ case errors_1.ValueErrorType.Integer:
199
+ return 'Expected integer';
200
+ case errors_1.ValueErrorType.IntersectUnevaluatedProperties:
201
+ return 'Unexpected property';
202
+ case errors_1.ValueErrorType.Intersect:
203
+ return 'Expected all values to match';
204
+ case errors_1.ValueErrorType.Iterator:
205
+ return 'Expected Iterator';
206
+ case errors_1.ValueErrorType.Literal:
207
+ return `Expected ${typeof schema.const === 'string' ? `'${schema.const}'` : schema.const}`;
208
+ case errors_1.ValueErrorType.Never:
209
+ return 'Never';
210
+ case errors_1.ValueErrorType.Not:
211
+ return 'Value should not match';
212
+ case errors_1.ValueErrorType.Null:
213
+ return 'Expected null';
214
+ case errors_1.ValueErrorType.NumberExclusiveMaximum:
215
+ return `Expected number to be less than ${schema.exclusiveMaximum}`;
216
+ case errors_1.ValueErrorType.NumberExclusiveMinimum:
217
+ return `Expected number to be greater than ${schema.exclusiveMinimum}`;
218
+ case errors_1.ValueErrorType.NumberMaximum:
219
+ return `Expected number to be less or equal to ${schema.maximum}`;
220
+ case errors_1.ValueErrorType.NumberMinimum:
221
+ return `Expected number to be greater or equal to ${schema.minimum}`;
222
+ case errors_1.ValueErrorType.NumberMultipleOf:
223
+ return `Expected number to be a multiple of ${schema.multipleOf}`;
224
+ case errors_1.ValueErrorType.Number:
225
+ return 'Expected number';
226
+ case errors_1.ValueErrorType.Object:
227
+ return 'Expected object';
228
+ case errors_1.ValueErrorType.ObjectAdditionalProperties:
229
+ return 'Unexpected property';
230
+ case errors_1.ValueErrorType.ObjectMaxProperties:
231
+ return `Expected object to have no more than ${schema.maxProperties} properties`;
232
+ case errors_1.ValueErrorType.ObjectMinProperties:
233
+ return `Expected object to have at least ${schema.minProperties} properties`;
234
+ case errors_1.ValueErrorType.ObjectRequiredProperty:
235
+ return 'Required property';
236
+ case errors_1.ValueErrorType.Promise:
237
+ return 'Expected Promise';
238
+ case errors_1.ValueErrorType.StringFormatUnknown:
239
+ return `Unknown format '${schema.format}'`;
240
+ case errors_1.ValueErrorType.StringFormat:
241
+ return `Expected string to match '${schema.format}' format`;
242
+ case errors_1.ValueErrorType.StringMaxLength:
243
+ return `Expected string length less or equal to ${schema.maxLength}`;
244
+ case errors_1.ValueErrorType.StringMinLength:
245
+ return `Expected string length greater or equal to ${schema.minLength}`;
246
+ case errors_1.ValueErrorType.StringPattern:
247
+ return `Expected string to match '${schema.pattern}'`;
248
+ case errors_1.ValueErrorType.String:
249
+ return 'Expected string';
250
+ case errors_1.ValueErrorType.Symbol:
251
+ return 'Expected symbol';
252
+ case errors_1.ValueErrorType.TupleLength:
253
+ return `Expected tuple to have ${schema.maxItems || 0} elements`;
254
+ case errors_1.ValueErrorType.Tuple:
255
+ return 'Expected tuple';
256
+ case errors_1.ValueErrorType.Uint8ArrayMaxByteLength:
257
+ return `Expected byte length less or equal to ${schema.maxByteLength}`;
258
+ case errors_1.ValueErrorType.Uint8ArrayMinByteLength:
259
+ return `Expected byte length greater or equal to ${schema.minByteLength}`;
260
+ case errors_1.ValueErrorType.Uint8Array:
261
+ return 'Expected Uint8Array';
262
+ case errors_1.ValueErrorType.Undefined:
263
+ return 'Expected undefined';
264
+ case errors_1.ValueErrorType.Union:
265
+ return 'Expected union value';
266
+ case errors_1.ValueErrorType.Void:
267
+ return 'Expected void';
268
+ case errors_1.ValueErrorType.Kind:
269
+ return `Expected kind '${schema[Types.Kind]}'`;
270
+ default:
271
+ return 'Unknown error type';
272
+ }
273
+ }
274
+ exports.DefaultErrorFunction = DefaultErrorFunction;