@sinclair/typebox 0.24.21 → 0.24.24
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.
- package/compiler/compiler.d.ts +8 -1
- package/compiler/compiler.js +130 -55
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +3 -0
- package/errors/errors.d.ts +56 -0
- package/errors/errors.js +381 -0
- package/errors/index.d.ts +1 -0
- package/errors/index.js +44 -0
- package/guard/guard.js +82 -14
- package/package.json +1 -1
- package/readme.md +84 -104
- package/typebox.js +4 -2
- package/value/cast.d.ts +4 -0
- package/value/cast.js +75 -52
- package/value/check.d.ts +4 -0
- package/value/check.js +75 -49
- package/value/create.d.ts +4 -0
- package/value/create.js +75 -53
- package/value/index.d.ts +1 -1
- package/value/index.js +3 -0
- package/value/value.d.ts +1 -1
- package/value/value.js +2 -2
- package/value/errors.d.ts +0 -10
- package/value/errors.js +0 -305
package/errors/errors.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/errors
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ValueErrors = exports.ValueErrorsInvalidTypeError = exports.ValueErrorType = void 0;
|
|
31
|
+
const index_1 = require("../guard/index");
|
|
32
|
+
// -------------------------------------------------------------------
|
|
33
|
+
// ValueErrorType
|
|
34
|
+
// -------------------------------------------------------------------
|
|
35
|
+
var ValueErrorType;
|
|
36
|
+
(function (ValueErrorType) {
|
|
37
|
+
ValueErrorType[ValueErrorType["Array"] = 0] = "Array";
|
|
38
|
+
ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
|
|
39
|
+
ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
|
|
40
|
+
ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
|
|
41
|
+
ValueErrorType[ValueErrorType["Boolean"] = 4] = "Boolean";
|
|
42
|
+
ValueErrorType[ValueErrorType["Function"] = 5] = "Function";
|
|
43
|
+
ValueErrorType[ValueErrorType["Integer"] = 6] = "Integer";
|
|
44
|
+
ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 7] = "IntegerMultipleOf";
|
|
45
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 8] = "IntegerExclusiveMinimum";
|
|
46
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 9] = "IntegerExclusiveMaximum";
|
|
47
|
+
ValueErrorType[ValueErrorType["IntegerMinimum"] = 10] = "IntegerMinimum";
|
|
48
|
+
ValueErrorType[ValueErrorType["IntegerMaximum"] = 11] = "IntegerMaximum";
|
|
49
|
+
ValueErrorType[ValueErrorType["Literal"] = 12] = "Literal";
|
|
50
|
+
ValueErrorType[ValueErrorType["Null"] = 13] = "Null";
|
|
51
|
+
ValueErrorType[ValueErrorType["Number"] = 14] = "Number";
|
|
52
|
+
ValueErrorType[ValueErrorType["NumberMultipleOf"] = 15] = "NumberMultipleOf";
|
|
53
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 16] = "NumberExclusiveMinimum";
|
|
54
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 17] = "NumberExclusiveMaximum";
|
|
55
|
+
ValueErrorType[ValueErrorType["NumberMinumum"] = 18] = "NumberMinumum";
|
|
56
|
+
ValueErrorType[ValueErrorType["NumberMaximum"] = 19] = "NumberMaximum";
|
|
57
|
+
ValueErrorType[ValueErrorType["Object"] = 20] = "Object";
|
|
58
|
+
ValueErrorType[ValueErrorType["ObjectMinProperties"] = 21] = "ObjectMinProperties";
|
|
59
|
+
ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 22] = "ObjectMaxProperties";
|
|
60
|
+
ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 23] = "ObjectAdditionalProperties";
|
|
61
|
+
ValueErrorType[ValueErrorType["Promise"] = 24] = "Promise";
|
|
62
|
+
ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 25] = "RecordKeyNumeric";
|
|
63
|
+
ValueErrorType[ValueErrorType["RecordKeyString"] = 26] = "RecordKeyString";
|
|
64
|
+
ValueErrorType[ValueErrorType["String"] = 27] = "String";
|
|
65
|
+
ValueErrorType[ValueErrorType["StringMinLength"] = 28] = "StringMinLength";
|
|
66
|
+
ValueErrorType[ValueErrorType["StringMaxLength"] = 29] = "StringMaxLength";
|
|
67
|
+
ValueErrorType[ValueErrorType["StringPattern"] = 30] = "StringPattern";
|
|
68
|
+
ValueErrorType[ValueErrorType["TupleZeroLength"] = 31] = "TupleZeroLength";
|
|
69
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 32] = "TupleLength";
|
|
70
|
+
ValueErrorType[ValueErrorType["Undefined"] = 33] = "Undefined";
|
|
71
|
+
ValueErrorType[ValueErrorType["Union"] = 34] = "Union";
|
|
72
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 35] = "Uint8Array";
|
|
73
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 36] = "Uint8ArrayMinByteLength";
|
|
74
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 37] = "Uint8ArrayMaxByteLength";
|
|
75
|
+
ValueErrorType[ValueErrorType["Void"] = 38] = "Void";
|
|
76
|
+
})(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
|
|
77
|
+
// -------------------------------------------------------------------
|
|
78
|
+
// ValueErrors
|
|
79
|
+
// -------------------------------------------------------------------
|
|
80
|
+
class ValueErrorsInvalidTypeError extends Error {
|
|
81
|
+
constructor(schema) {
|
|
82
|
+
super('ValueErrors: Invalid type');
|
|
83
|
+
this.schema = schema;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.ValueErrorsInvalidTypeError = ValueErrorsInvalidTypeError;
|
|
87
|
+
var ValueErrors;
|
|
88
|
+
(function (ValueErrors) {
|
|
89
|
+
function* Any(schema, references, path, value) { }
|
|
90
|
+
function* Array(schema, references, path, value) {
|
|
91
|
+
if (!globalThis.Array.isArray(value)) {
|
|
92
|
+
return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
|
|
93
|
+
}
|
|
94
|
+
if (schema.minItems !== undefined && !(value.length >= schema.minItems)) {
|
|
95
|
+
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
|
|
96
|
+
}
|
|
97
|
+
if (schema.maxItems !== undefined && !(value.length <= schema.maxItems)) {
|
|
98
|
+
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
|
|
99
|
+
}
|
|
100
|
+
if (schema.uniqueItems === true && !(new Set(value).size === value.length)) {
|
|
101
|
+
yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
|
|
102
|
+
}
|
|
103
|
+
for (let i = 0; i < value.length; i++) {
|
|
104
|
+
yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function* Boolean(schema, references, path, value) {
|
|
108
|
+
if (!(typeof value === 'boolean')) {
|
|
109
|
+
return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function* Constructor(schema, references, path, value) {
|
|
113
|
+
yield* Visit(schema.returns, references, path, value);
|
|
114
|
+
}
|
|
115
|
+
function* Function(schema, references, path, value) {
|
|
116
|
+
if (!(typeof value === 'function')) {
|
|
117
|
+
return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function* Integer(schema, references, path, value) {
|
|
121
|
+
if (!(typeof value === 'number')) {
|
|
122
|
+
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
123
|
+
}
|
|
124
|
+
if (!globalThis.Number.isInteger(value)) {
|
|
125
|
+
yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
|
|
126
|
+
}
|
|
127
|
+
if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
|
|
128
|
+
yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
|
|
129
|
+
}
|
|
130
|
+
if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
|
|
131
|
+
yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
|
|
132
|
+
}
|
|
133
|
+
if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
|
|
134
|
+
yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
|
|
135
|
+
}
|
|
136
|
+
if (schema.minimum && !(value >= schema.minimum)) {
|
|
137
|
+
yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
|
|
138
|
+
}
|
|
139
|
+
if (schema.maximum && !(value <= schema.maximum)) {
|
|
140
|
+
yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function* Literal(schema, references, path, value) {
|
|
144
|
+
if (!(value === schema.const)) {
|
|
145
|
+
const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
|
|
146
|
+
return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function* Null(schema, references, path, value) {
|
|
150
|
+
if (!(value === null)) {
|
|
151
|
+
return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function* Number(schema, references, path, value) {
|
|
155
|
+
if (!(typeof value === 'number')) {
|
|
156
|
+
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
157
|
+
}
|
|
158
|
+
if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
|
|
159
|
+
yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
|
|
160
|
+
}
|
|
161
|
+
if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
|
|
162
|
+
yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
|
|
163
|
+
}
|
|
164
|
+
if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
|
|
165
|
+
yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
|
|
166
|
+
}
|
|
167
|
+
if (schema.minimum && !(value >= schema.minimum)) {
|
|
168
|
+
yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
|
|
169
|
+
}
|
|
170
|
+
if (schema.maximum && !(value <= schema.maximum)) {
|
|
171
|
+
yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function* Object(schema, references, path, value) {
|
|
175
|
+
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
176
|
+
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
177
|
+
}
|
|
178
|
+
if (schema.minProperties !== undefined && !(globalThis.Object.keys(value).length >= schema.minProperties)) {
|
|
179
|
+
yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
|
|
180
|
+
}
|
|
181
|
+
if (schema.maxProperties !== undefined && !(globalThis.Object.keys(value).length <= schema.maxProperties)) {
|
|
182
|
+
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
|
|
183
|
+
}
|
|
184
|
+
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
185
|
+
if (schema.additionalProperties === false) {
|
|
186
|
+
for (const propKey of globalThis.Object.keys(value)) {
|
|
187
|
+
if (!propertyKeys.includes(propKey)) {
|
|
188
|
+
yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${propKey}`, value: value[propKey], message: 'Unexpected property' };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
for (const propertyKey of propertyKeys) {
|
|
193
|
+
const propertySchema = schema.properties[propertyKey];
|
|
194
|
+
if (schema.required && schema.required.includes(propertyKey)) {
|
|
195
|
+
yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
if (value[propertyKey] !== undefined) {
|
|
199
|
+
yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function* Promise(schema, references, path, value) {
|
|
205
|
+
if (!(typeof value === 'object' && typeof value.then === 'function')) {
|
|
206
|
+
yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function* Record(schema, references, path, value) {
|
|
210
|
+
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
211
|
+
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
212
|
+
}
|
|
213
|
+
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
214
|
+
const regex = new RegExp(keyPattern);
|
|
215
|
+
if (!globalThis.Object.keys(value).every((key) => regex.test(key))) {
|
|
216
|
+
const numeric = keyPattern === '^(0|[1-9][0-9]*)$';
|
|
217
|
+
const type = numeric ? ValueErrorType.RecordKeyNumeric : ValueErrorType.RecordKeyString;
|
|
218
|
+
const message = numeric ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings';
|
|
219
|
+
return yield { type, schema, path, value, message };
|
|
220
|
+
}
|
|
221
|
+
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
222
|
+
yield* Visit(valueSchema, references, `${path}/${propKey}`, propValue);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function* Ref(schema, references, path, value) {
|
|
226
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
227
|
+
if (reference === undefined)
|
|
228
|
+
throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
229
|
+
yield* Visit(reference, references, path, value);
|
|
230
|
+
}
|
|
231
|
+
function* Self(schema, references, path, value) {
|
|
232
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
233
|
+
if (reference === undefined)
|
|
234
|
+
throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
|
|
235
|
+
yield* Visit(reference, references, path, value);
|
|
236
|
+
}
|
|
237
|
+
function* String(schema, references, path, value) {
|
|
238
|
+
if (!(typeof value === 'string')) {
|
|
239
|
+
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
240
|
+
}
|
|
241
|
+
if (schema.minLength !== undefined && !(value.length >= schema.minLength)) {
|
|
242
|
+
yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
|
|
243
|
+
}
|
|
244
|
+
if (schema.maxLength !== undefined && !(value.length <= schema.maxLength)) {
|
|
245
|
+
yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
|
|
246
|
+
}
|
|
247
|
+
if (schema.pattern !== undefined) {
|
|
248
|
+
const regex = new RegExp(schema.pattern);
|
|
249
|
+
if (!regex.test(value)) {
|
|
250
|
+
yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function* Tuple(schema, references, path, value) {
|
|
255
|
+
if (!globalThis.Array.isArray(value)) {
|
|
256
|
+
return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
|
|
257
|
+
}
|
|
258
|
+
if (schema.items === undefined && !(value.length === 0)) {
|
|
259
|
+
return yield { type: ValueErrorType.TupleZeroLength, schema, path, value, message: 'Expected tuple to have 0 elements' };
|
|
260
|
+
}
|
|
261
|
+
if (!(value.length === schema.maxItems)) {
|
|
262
|
+
yield { type: ValueErrorType.TupleLength, schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
|
|
263
|
+
}
|
|
264
|
+
if (!schema.items) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
for (let i = 0; i < schema.items.length; i++) {
|
|
268
|
+
yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function* Undefined(schema, references, path, value) {
|
|
272
|
+
if (!(value === undefined)) {
|
|
273
|
+
yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function* Union(schema, references, path, value) {
|
|
277
|
+
const errors = [];
|
|
278
|
+
for (const inner of schema.anyOf) {
|
|
279
|
+
const variantErrors = [...Visit(inner, references, path, value)];
|
|
280
|
+
if (variantErrors.length === 0)
|
|
281
|
+
return;
|
|
282
|
+
errors.push(...variantErrors);
|
|
283
|
+
}
|
|
284
|
+
for (const error of errors) {
|
|
285
|
+
yield error;
|
|
286
|
+
}
|
|
287
|
+
if (errors.length > 0) {
|
|
288
|
+
yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function* Uint8Array(schema, references, path, value) {
|
|
292
|
+
if (!(value instanceof globalThis.Uint8Array)) {
|
|
293
|
+
return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
|
|
294
|
+
}
|
|
295
|
+
if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
|
|
296
|
+
yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
|
|
297
|
+
}
|
|
298
|
+
if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
|
|
299
|
+
yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function* Unknown(schema, references, path, value) { }
|
|
303
|
+
function* Void(schema, references, path, value) {
|
|
304
|
+
if (!(value === null)) {
|
|
305
|
+
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null` };
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function* Visit(schema, references, path, value) {
|
|
309
|
+
const refs = schema.$id === undefined ? references : [schema, ...references];
|
|
310
|
+
if (index_1.TypeGuard.TAny(schema)) {
|
|
311
|
+
return yield* Any(schema, refs, path, value);
|
|
312
|
+
}
|
|
313
|
+
else if (index_1.TypeGuard.TArray(schema)) {
|
|
314
|
+
return yield* Array(schema, refs, path, value);
|
|
315
|
+
}
|
|
316
|
+
else if (index_1.TypeGuard.TBoolean(schema)) {
|
|
317
|
+
return yield* Boolean(schema, refs, path, value);
|
|
318
|
+
}
|
|
319
|
+
else if (index_1.TypeGuard.TConstructor(schema)) {
|
|
320
|
+
return yield* Constructor(schema, refs, path, value);
|
|
321
|
+
}
|
|
322
|
+
else if (index_1.TypeGuard.TFunction(schema)) {
|
|
323
|
+
return yield* Function(schema, refs, path, value);
|
|
324
|
+
}
|
|
325
|
+
else if (index_1.TypeGuard.TInteger(schema)) {
|
|
326
|
+
return yield* Integer(schema, refs, path, value);
|
|
327
|
+
}
|
|
328
|
+
else if (index_1.TypeGuard.TLiteral(schema)) {
|
|
329
|
+
return yield* Literal(schema, refs, path, value);
|
|
330
|
+
}
|
|
331
|
+
else if (index_1.TypeGuard.TNull(schema)) {
|
|
332
|
+
return yield* Null(schema, refs, path, value);
|
|
333
|
+
}
|
|
334
|
+
else if (index_1.TypeGuard.TNumber(schema)) {
|
|
335
|
+
return yield* Number(schema, refs, path, value);
|
|
336
|
+
}
|
|
337
|
+
else if (index_1.TypeGuard.TObject(schema)) {
|
|
338
|
+
return yield* Object(schema, refs, path, value);
|
|
339
|
+
}
|
|
340
|
+
else if (index_1.TypeGuard.TPromise(schema)) {
|
|
341
|
+
return yield* Promise(schema, refs, path, value);
|
|
342
|
+
}
|
|
343
|
+
else if (index_1.TypeGuard.TRecord(schema)) {
|
|
344
|
+
return yield* Record(schema, refs, path, value);
|
|
345
|
+
}
|
|
346
|
+
else if (index_1.TypeGuard.TRef(schema)) {
|
|
347
|
+
return yield* Ref(schema, refs, path, value);
|
|
348
|
+
}
|
|
349
|
+
else if (index_1.TypeGuard.TSelf(schema)) {
|
|
350
|
+
return yield* Self(schema, refs, path, value);
|
|
351
|
+
}
|
|
352
|
+
else if (index_1.TypeGuard.TString(schema)) {
|
|
353
|
+
return yield* String(schema, refs, path, value);
|
|
354
|
+
}
|
|
355
|
+
else if (index_1.TypeGuard.TTuple(schema)) {
|
|
356
|
+
return yield* Tuple(schema, refs, path, value);
|
|
357
|
+
}
|
|
358
|
+
else if (index_1.TypeGuard.TUndefined(schema)) {
|
|
359
|
+
return yield* Undefined(schema, refs, path, value);
|
|
360
|
+
}
|
|
361
|
+
else if (index_1.TypeGuard.TUnion(schema)) {
|
|
362
|
+
return yield* Union(schema, refs, path, value);
|
|
363
|
+
}
|
|
364
|
+
else if (index_1.TypeGuard.TUint8Array(schema)) {
|
|
365
|
+
return yield* Uint8Array(schema, refs, path, value);
|
|
366
|
+
}
|
|
367
|
+
else if (index_1.TypeGuard.TUnknown(schema)) {
|
|
368
|
+
return yield* Unknown(schema, refs, path, value);
|
|
369
|
+
}
|
|
370
|
+
else if (index_1.TypeGuard.TVoid(schema)) {
|
|
371
|
+
return yield* Void(schema, refs, path, value);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
throw new ValueErrorsInvalidTypeError(schema);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
function* Errors(schema, references, value) {
|
|
378
|
+
yield* Visit(schema, references, '', value);
|
|
379
|
+
}
|
|
380
|
+
ValueErrors.Errors = Errors;
|
|
381
|
+
})(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './errors';
|
package/errors/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/errors
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
30
|
+
if (k2 === undefined) k2 = k;
|
|
31
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
32
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
33
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
34
|
+
}
|
|
35
|
+
Object.defineProperty(o, k2, desc);
|
|
36
|
+
}) : (function(o, m, k, k2) {
|
|
37
|
+
if (k2 === undefined) k2 = k;
|
|
38
|
+
o[k2] = m[k];
|
|
39
|
+
}));
|
|
40
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
41
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
__exportStar(require("./errors"), exports);
|
package/guard/guard.js
CHANGED
|
@@ -32,11 +32,41 @@ const Types = require("../typebox");
|
|
|
32
32
|
/** TypeGuard tests that values conform to a known TypeBox type specification */
|
|
33
33
|
var TypeGuard;
|
|
34
34
|
(function (TypeGuard) {
|
|
35
|
-
function IsObject(
|
|
36
|
-
return typeof
|
|
35
|
+
function IsObject(value) {
|
|
36
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
37
37
|
}
|
|
38
|
-
function IsArray(
|
|
39
|
-
return typeof
|
|
38
|
+
function IsArray(value) {
|
|
39
|
+
return typeof value === 'object' && value !== null && Array.isArray(value);
|
|
40
|
+
}
|
|
41
|
+
function IsPattern(value) {
|
|
42
|
+
try {
|
|
43
|
+
new RegExp(value);
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function IsString(value) {
|
|
51
|
+
return typeof value === 'string';
|
|
52
|
+
}
|
|
53
|
+
function IsNumber(value) {
|
|
54
|
+
return typeof value === 'number';
|
|
55
|
+
}
|
|
56
|
+
function IsBoolean(value) {
|
|
57
|
+
return typeof value === 'boolean';
|
|
58
|
+
}
|
|
59
|
+
function IsOptionalNumber(value) {
|
|
60
|
+
return value === undefined || (value !== undefined && IsNumber(value));
|
|
61
|
+
}
|
|
62
|
+
function IsOptionalBoolean(value) {
|
|
63
|
+
return value === undefined || (value !== undefined && IsBoolean(value));
|
|
64
|
+
}
|
|
65
|
+
function IsOptionalString(value) {
|
|
66
|
+
return value === undefined || (value !== undefined && IsString(value));
|
|
67
|
+
}
|
|
68
|
+
function IsOptionalPattern(value) {
|
|
69
|
+
return value === undefined || (value !== undefined && IsPattern(value));
|
|
40
70
|
}
|
|
41
71
|
/** Returns true if the given schema is TAny */
|
|
42
72
|
function TAny(schema) {
|
|
@@ -45,7 +75,12 @@ var TypeGuard;
|
|
|
45
75
|
TypeGuard.TAny = TAny;
|
|
46
76
|
/** Returns true if the given schema is TArray */
|
|
47
77
|
function TArray(schema) {
|
|
48
|
-
return IsObject(schema) &&
|
|
78
|
+
return (IsObject(schema) &&
|
|
79
|
+
schema[Types.Kind] === 'Array' &&
|
|
80
|
+
schema.type === 'array' &&
|
|
81
|
+
TSchema(schema.items) &&
|
|
82
|
+
IsOptionalNumber(schema.minItems) &&
|
|
83
|
+
IsOptionalNumber(schema.maxItems));
|
|
49
84
|
}
|
|
50
85
|
TypeGuard.TArray = TArray;
|
|
51
86
|
/** Returns true if the given schema is TBoolean */
|
|
@@ -79,12 +114,19 @@ var TypeGuard;
|
|
|
79
114
|
TypeGuard.TFunction = TFunction;
|
|
80
115
|
/** Returns true if the given schema is TInteger */
|
|
81
116
|
function TInteger(schema) {
|
|
82
|
-
return IsObject(schema) &&
|
|
117
|
+
return (IsObject(schema) &&
|
|
118
|
+
schema[Types.Kind] === 'Integer' &&
|
|
119
|
+
schema.type === 'integer' &&
|
|
120
|
+
IsOptionalNumber(schema.multipleOf) &&
|
|
121
|
+
IsOptionalNumber(schema.minimum) &&
|
|
122
|
+
IsOptionalNumber(schema.maximum) &&
|
|
123
|
+
IsOptionalNumber(schema.exclusiveMinimum) &&
|
|
124
|
+
IsOptionalNumber(schema.exclusiveMaximum));
|
|
83
125
|
}
|
|
84
126
|
TypeGuard.TInteger = TInteger;
|
|
85
127
|
/** Returns true if the given schema is TLiteral */
|
|
86
128
|
function TLiteral(schema) {
|
|
87
|
-
return IsObject(schema) && schema[Types.Kind] === 'Literal' && (
|
|
129
|
+
return IsObject(schema) && schema[Types.Kind] === 'Literal' && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
|
|
88
130
|
}
|
|
89
131
|
TypeGuard.TLiteral = TLiteral;
|
|
90
132
|
/** Returns true if the given schema is TNull */
|
|
@@ -94,12 +136,19 @@ var TypeGuard;
|
|
|
94
136
|
TypeGuard.TNull = TNull;
|
|
95
137
|
/** Returns true if the given schema is TNumber */
|
|
96
138
|
function TNumber(schema) {
|
|
97
|
-
return IsObject(schema) &&
|
|
139
|
+
return (IsObject(schema) &&
|
|
140
|
+
schema[Types.Kind] === 'Number' &&
|
|
141
|
+
schema.type === 'number' &&
|
|
142
|
+
IsOptionalNumber(schema.multipleOf) &&
|
|
143
|
+
IsOptionalNumber(schema.minimum) &&
|
|
144
|
+
IsOptionalNumber(schema.maximum) &&
|
|
145
|
+
IsOptionalNumber(schema.exclusiveMinimum) &&
|
|
146
|
+
IsOptionalNumber(schema.exclusiveMaximum));
|
|
98
147
|
}
|
|
99
148
|
TypeGuard.TNumber = TNumber;
|
|
100
149
|
/** Returns true if the given schema is TObject */
|
|
101
150
|
function TObject(schema) {
|
|
102
|
-
if (!(IsObject(schema) && schema[Types.Kind] === 'Object' && schema.type === 'object' && IsObject(schema.properties))) {
|
|
151
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Object' && schema.type === 'object' && IsObject(schema.properties) && IsOptionalBoolean(schema.additionalProperties))) {
|
|
103
152
|
return false;
|
|
104
153
|
}
|
|
105
154
|
for (const property of Object.values(schema.properties)) {
|
|
@@ -123,6 +172,9 @@ var TypeGuard;
|
|
|
123
172
|
if (keys.length !== 1) {
|
|
124
173
|
return false;
|
|
125
174
|
}
|
|
175
|
+
if (!IsPattern(keys[0])) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
126
178
|
if (!TSchema(schema.patternProperties[keys[0]])) {
|
|
127
179
|
return false;
|
|
128
180
|
}
|
|
@@ -131,22 +183,33 @@ var TypeGuard;
|
|
|
131
183
|
TypeGuard.TRecord = TRecord;
|
|
132
184
|
/** Returns true if the given schema is TSelf */
|
|
133
185
|
function TSelf(schema) {
|
|
134
|
-
return IsObject(schema) && schema[Types.Kind] === 'Self' &&
|
|
186
|
+
return IsObject(schema) && schema[Types.Kind] === 'Self' && IsString(schema.$ref);
|
|
135
187
|
}
|
|
136
188
|
TypeGuard.TSelf = TSelf;
|
|
137
189
|
/** Returns true if the given schema is TRef */
|
|
138
190
|
function TRef(schema) {
|
|
139
|
-
return IsObject(schema) && schema[Types.Kind] === 'Ref' &&
|
|
191
|
+
return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsString(schema.$ref);
|
|
140
192
|
}
|
|
141
193
|
TypeGuard.TRef = TRef;
|
|
142
194
|
/** Returns true if the given schema is TString */
|
|
143
195
|
function TString(schema) {
|
|
144
|
-
return IsObject(schema) &&
|
|
196
|
+
return (IsObject(schema) &&
|
|
197
|
+
schema[Types.Kind] === 'String' &&
|
|
198
|
+
schema.type === 'string' &&
|
|
199
|
+
IsOptionalNumber(schema.minLength) &&
|
|
200
|
+
IsOptionalNumber(schema.maxLength) &&
|
|
201
|
+
IsOptionalPattern(schema.pattern) &&
|
|
202
|
+
IsOptionalString(schema.format));
|
|
145
203
|
}
|
|
146
204
|
TypeGuard.TString = TString;
|
|
147
205
|
/** Returns true if the given schema is TTuple */
|
|
148
206
|
function TTuple(schema) {
|
|
149
|
-
if (!(IsObject(schema) &&
|
|
207
|
+
if (!(IsObject(schema) &&
|
|
208
|
+
schema[Types.Kind] === 'Tuple' &&
|
|
209
|
+
schema.type === 'array' &&
|
|
210
|
+
IsNumber(schema.minItems) &&
|
|
211
|
+
IsNumber(schema.maxItems) &&
|
|
212
|
+
schema.minItems === schema.maxItems)) {
|
|
150
213
|
return false;
|
|
151
214
|
}
|
|
152
215
|
if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
|
|
@@ -181,7 +244,12 @@ var TypeGuard;
|
|
|
181
244
|
TypeGuard.TUnion = TUnion;
|
|
182
245
|
/** Returns true if the given schema is TUint8Array */
|
|
183
246
|
function TUint8Array(schema) {
|
|
184
|
-
return IsObject(schema) &&
|
|
247
|
+
return (IsObject(schema) &&
|
|
248
|
+
schema[Types.Kind] === 'Uint8Array' &&
|
|
249
|
+
schema.type === 'object' &&
|
|
250
|
+
schema.specialized === 'Uint8Array' &&
|
|
251
|
+
IsOptionalNumber(schema.minByteLength) &&
|
|
252
|
+
IsOptionalNumber(schema.maxByteLength));
|
|
185
253
|
}
|
|
186
254
|
TypeGuard.TUint8Array = TUint8Array;
|
|
187
255
|
/** Returns true if the given schema is TUnknown */
|