@sinclair/typebox 0.29.5 → 0.30.0-dev-1
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 +7 -4
- package/compiler/compiler.js +208 -194
- package/errors/errors.d.ts +65 -60
- package/errors/errors.js +515 -492
- package/package.json +14 -2
- package/readme.md +161 -169
- package/system/system.js +0 -6
- package/typebox.d.ts +99 -52
- package/typebox.js +496 -573
- package/value/cast.d.ts +5 -4
- package/value/cast.js +255 -260
- package/value/check.d.ts +4 -3
- package/value/check.js +412 -397
- package/value/clone.d.ts +2 -3
- package/value/clone.js +62 -42
- package/value/convert.d.ts +5 -4
- package/value/convert.js +305 -318
- package/value/create.d.ts +6 -6
- package/value/create.js +388 -376
- package/value/delta.d.ts +2 -4
- package/value/delta.js +121 -130
- package/value/equal.d.ts +2 -3
- package/value/equal.js +48 -51
- package/value/guard.d.ts +44 -0
- package/value/guard.js +146 -0
- package/value/hash.d.ts +14 -3
- package/value/hash.js +124 -166
- package/value/index.d.ts +3 -4
- package/value/index.js +6 -20
- package/value/mutate.d.ts +2 -4
- package/value/mutate.js +75 -67
- package/value/pointer.js +8 -2
- package/value/value.d.ts +15 -15
- package/value/value.js +27 -27
- package/value/is.d.ts +0 -11
- package/value/is.js +0 -53
package/errors/errors.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ValueErrors = exports.ValueErrorsDereferenceError = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
4
2
|
/*--------------------------------------------------------------------------
|
|
5
3
|
|
|
6
4
|
@sinclair/typebox/errors
|
|
@@ -28,77 +26,85 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
28
26
|
THE SOFTWARE.
|
|
29
27
|
|
|
30
28
|
---------------------------------------------------------------------------*/
|
|
31
|
-
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.Errors = exports.ValueErrorsDereferenceError = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
32
31
|
const index_1 = require("../system/index");
|
|
33
|
-
const
|
|
34
|
-
|
|
32
|
+
const Types = require("../typebox");
|
|
33
|
+
const ValueHash = require("../value/hash");
|
|
34
|
+
const ValueGuard = require("../value/guard");
|
|
35
|
+
// --------------------------------------------------------------------------
|
|
35
36
|
// ValueErrorType
|
|
36
|
-
//
|
|
37
|
+
// --------------------------------------------------------------------------
|
|
37
38
|
var ValueErrorType;
|
|
38
39
|
(function (ValueErrorType) {
|
|
39
40
|
ValueErrorType[ValueErrorType["Array"] = 0] = "Array";
|
|
40
41
|
ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
|
|
41
42
|
ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
|
|
42
|
-
ValueErrorType[ValueErrorType["
|
|
43
|
-
ValueErrorType[ValueErrorType["
|
|
44
|
-
ValueErrorType[ValueErrorType["
|
|
45
|
-
ValueErrorType[ValueErrorType["
|
|
46
|
-
ValueErrorType[ValueErrorType["
|
|
47
|
-
ValueErrorType[ValueErrorType["
|
|
48
|
-
ValueErrorType[ValueErrorType["
|
|
49
|
-
ValueErrorType[ValueErrorType["
|
|
50
|
-
ValueErrorType[ValueErrorType["
|
|
51
|
-
ValueErrorType[ValueErrorType["
|
|
52
|
-
ValueErrorType[ValueErrorType["
|
|
53
|
-
ValueErrorType[ValueErrorType["
|
|
54
|
-
ValueErrorType[ValueErrorType["
|
|
55
|
-
ValueErrorType[ValueErrorType["
|
|
56
|
-
ValueErrorType[ValueErrorType["
|
|
57
|
-
ValueErrorType[ValueErrorType["
|
|
58
|
-
ValueErrorType[ValueErrorType["
|
|
59
|
-
ValueErrorType[ValueErrorType["
|
|
60
|
-
ValueErrorType[ValueErrorType["
|
|
61
|
-
ValueErrorType[ValueErrorType["
|
|
62
|
-
ValueErrorType[ValueErrorType["
|
|
63
|
-
ValueErrorType[ValueErrorType["
|
|
64
|
-
ValueErrorType[ValueErrorType["
|
|
65
|
-
ValueErrorType[ValueErrorType["
|
|
66
|
-
ValueErrorType[ValueErrorType["
|
|
67
|
-
ValueErrorType[ValueErrorType["
|
|
68
|
-
ValueErrorType[ValueErrorType["
|
|
69
|
-
ValueErrorType[ValueErrorType["
|
|
70
|
-
ValueErrorType[ValueErrorType["
|
|
71
|
-
ValueErrorType[ValueErrorType["
|
|
72
|
-
ValueErrorType[ValueErrorType["
|
|
73
|
-
ValueErrorType[ValueErrorType["
|
|
74
|
-
ValueErrorType[ValueErrorType["
|
|
75
|
-
ValueErrorType[ValueErrorType["
|
|
76
|
-
ValueErrorType[ValueErrorType["
|
|
77
|
-
ValueErrorType[ValueErrorType["
|
|
78
|
-
ValueErrorType[ValueErrorType["
|
|
79
|
-
ValueErrorType[ValueErrorType["
|
|
80
|
-
ValueErrorType[ValueErrorType["
|
|
81
|
-
ValueErrorType[ValueErrorType["
|
|
82
|
-
ValueErrorType[ValueErrorType["
|
|
83
|
-
ValueErrorType[ValueErrorType["
|
|
84
|
-
ValueErrorType[ValueErrorType["
|
|
85
|
-
ValueErrorType[ValueErrorType["
|
|
86
|
-
ValueErrorType[ValueErrorType["
|
|
87
|
-
ValueErrorType[ValueErrorType["
|
|
88
|
-
ValueErrorType[ValueErrorType["
|
|
89
|
-
ValueErrorType[ValueErrorType["
|
|
90
|
-
ValueErrorType[ValueErrorType["
|
|
91
|
-
ValueErrorType[ValueErrorType["
|
|
92
|
-
ValueErrorType[ValueErrorType["
|
|
93
|
-
ValueErrorType[ValueErrorType["
|
|
94
|
-
ValueErrorType[ValueErrorType["
|
|
95
|
-
ValueErrorType[ValueErrorType["
|
|
96
|
-
ValueErrorType[ValueErrorType["
|
|
97
|
-
ValueErrorType[ValueErrorType["
|
|
43
|
+
ValueErrorType[ValueErrorType["ArrayContains"] = 3] = "ArrayContains";
|
|
44
|
+
ValueErrorType[ValueErrorType["ArrayMinContains"] = 4] = "ArrayMinContains";
|
|
45
|
+
ValueErrorType[ValueErrorType["ArrayMaxContains"] = 5] = "ArrayMaxContains";
|
|
46
|
+
ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 6] = "ArrayUniqueItems";
|
|
47
|
+
ValueErrorType[ValueErrorType["AsyncIterator"] = 7] = "AsyncIterator";
|
|
48
|
+
ValueErrorType[ValueErrorType["BigInt"] = 8] = "BigInt";
|
|
49
|
+
ValueErrorType[ValueErrorType["BigIntMultipleOf"] = 9] = "BigIntMultipleOf";
|
|
50
|
+
ValueErrorType[ValueErrorType["BigIntExclusiveMinimum"] = 10] = "BigIntExclusiveMinimum";
|
|
51
|
+
ValueErrorType[ValueErrorType["BigIntExclusiveMaximum"] = 11] = "BigIntExclusiveMaximum";
|
|
52
|
+
ValueErrorType[ValueErrorType["BigIntMinimum"] = 12] = "BigIntMinimum";
|
|
53
|
+
ValueErrorType[ValueErrorType["BigIntMaximum"] = 13] = "BigIntMaximum";
|
|
54
|
+
ValueErrorType[ValueErrorType["Boolean"] = 14] = "Boolean";
|
|
55
|
+
ValueErrorType[ValueErrorType["Date"] = 15] = "Date";
|
|
56
|
+
ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 16] = "DateExclusiveMinimumTimestamp";
|
|
57
|
+
ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 17] = "DateExclusiveMaximumTimestamp";
|
|
58
|
+
ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 18] = "DateMinimumTimestamp";
|
|
59
|
+
ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 19] = "DateMaximumTimestamp";
|
|
60
|
+
ValueErrorType[ValueErrorType["Function"] = 20] = "Function";
|
|
61
|
+
ValueErrorType[ValueErrorType["Integer"] = 21] = "Integer";
|
|
62
|
+
ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 22] = "IntegerMultipleOf";
|
|
63
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 23] = "IntegerExclusiveMinimum";
|
|
64
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 24] = "IntegerExclusiveMaximum";
|
|
65
|
+
ValueErrorType[ValueErrorType["IntegerMinimum"] = 25] = "IntegerMinimum";
|
|
66
|
+
ValueErrorType[ValueErrorType["IntegerMaximum"] = 26] = "IntegerMaximum";
|
|
67
|
+
ValueErrorType[ValueErrorType["Intersect"] = 27] = "Intersect";
|
|
68
|
+
ValueErrorType[ValueErrorType["IntersectUnevaluatedProperties"] = 28] = "IntersectUnevaluatedProperties";
|
|
69
|
+
ValueErrorType[ValueErrorType["Iterator"] = 29] = "Iterator";
|
|
70
|
+
ValueErrorType[ValueErrorType["Literal"] = 30] = "Literal";
|
|
71
|
+
ValueErrorType[ValueErrorType["Never"] = 31] = "Never";
|
|
72
|
+
ValueErrorType[ValueErrorType["Not"] = 32] = "Not";
|
|
73
|
+
ValueErrorType[ValueErrorType["Null"] = 33] = "Null";
|
|
74
|
+
ValueErrorType[ValueErrorType["Number"] = 34] = "Number";
|
|
75
|
+
ValueErrorType[ValueErrorType["NumberMultipleOf"] = 35] = "NumberMultipleOf";
|
|
76
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 36] = "NumberExclusiveMinimum";
|
|
77
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 37] = "NumberExclusiveMaximum";
|
|
78
|
+
ValueErrorType[ValueErrorType["NumberMinimum"] = 38] = "NumberMinimum";
|
|
79
|
+
ValueErrorType[ValueErrorType["NumberMaximum"] = 39] = "NumberMaximum";
|
|
80
|
+
ValueErrorType[ValueErrorType["Object"] = 40] = "Object";
|
|
81
|
+
ValueErrorType[ValueErrorType["ObjectMinProperties"] = 41] = "ObjectMinProperties";
|
|
82
|
+
ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 42] = "ObjectMaxProperties";
|
|
83
|
+
ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 43] = "ObjectAdditionalProperties";
|
|
84
|
+
ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 44] = "ObjectRequiredProperties";
|
|
85
|
+
ValueErrorType[ValueErrorType["Promise"] = 45] = "Promise";
|
|
86
|
+
ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 46] = "RecordKeyNumeric";
|
|
87
|
+
ValueErrorType[ValueErrorType["RecordKeyString"] = 47] = "RecordKeyString";
|
|
88
|
+
ValueErrorType[ValueErrorType["String"] = 48] = "String";
|
|
89
|
+
ValueErrorType[ValueErrorType["StringMinLength"] = 49] = "StringMinLength";
|
|
90
|
+
ValueErrorType[ValueErrorType["StringMaxLength"] = 50] = "StringMaxLength";
|
|
91
|
+
ValueErrorType[ValueErrorType["StringPattern"] = 51] = "StringPattern";
|
|
92
|
+
ValueErrorType[ValueErrorType["StringFormatUnknown"] = 52] = "StringFormatUnknown";
|
|
93
|
+
ValueErrorType[ValueErrorType["StringFormat"] = 53] = "StringFormat";
|
|
94
|
+
ValueErrorType[ValueErrorType["Symbol"] = 54] = "Symbol";
|
|
95
|
+
ValueErrorType[ValueErrorType["TupleZeroLength"] = 55] = "TupleZeroLength";
|
|
96
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 56] = "TupleLength";
|
|
97
|
+
ValueErrorType[ValueErrorType["Undefined"] = 57] = "Undefined";
|
|
98
|
+
ValueErrorType[ValueErrorType["Union"] = 58] = "Union";
|
|
99
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 59] = "Uint8Array";
|
|
100
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 60] = "Uint8ArrayMinByteLength";
|
|
101
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 61] = "Uint8ArrayMaxByteLength";
|
|
102
|
+
ValueErrorType[ValueErrorType["Void"] = 62] = "Void";
|
|
103
|
+
ValueErrorType[ValueErrorType["Kind"] = 63] = "Kind";
|
|
98
104
|
})(ValueErrorType || (exports.ValueErrorType = ValueErrorType = {}));
|
|
99
|
-
//
|
|
105
|
+
// --------------------------------------------------------------------------
|
|
100
106
|
// ValueErrorIterator
|
|
101
|
-
//
|
|
107
|
+
// --------------------------------------------------------------------------
|
|
102
108
|
class ValueErrorIterator {
|
|
103
109
|
constructor(iterator) {
|
|
104
110
|
this.iterator = iterator;
|
|
@@ -113,9 +119,9 @@ class ValueErrorIterator {
|
|
|
113
119
|
}
|
|
114
120
|
}
|
|
115
121
|
exports.ValueErrorIterator = ValueErrorIterator;
|
|
116
|
-
//
|
|
122
|
+
// --------------------------------------------------------------------------
|
|
117
123
|
// ValueErrors
|
|
118
|
-
//
|
|
124
|
+
// --------------------------------------------------------------------------
|
|
119
125
|
class ValueErrorsUnknownTypeError extends Error {
|
|
120
126
|
constructor(schema) {
|
|
121
127
|
super('ValueErrors: Unknown type');
|
|
@@ -130,482 +136,499 @@ class ValueErrorsDereferenceError extends Error {
|
|
|
130
136
|
}
|
|
131
137
|
}
|
|
132
138
|
exports.ValueErrorsDereferenceError = ValueErrorsDereferenceError;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return
|
|
167
|
-
}
|
|
168
|
-
function IsVoid(value) {
|
|
169
|
-
const result = value === undefined;
|
|
170
|
-
return index_1.TypeSystem.AllowVoidNull ? result || value === null : result;
|
|
171
|
-
}
|
|
172
|
-
// ----------------------------------------------------------------------
|
|
173
|
-
// Types
|
|
174
|
-
// ----------------------------------------------------------------------
|
|
175
|
-
function* Any(schema, references, path, value) { }
|
|
176
|
-
function* Array(schema, references, path, value) {
|
|
177
|
-
if (!globalThis.Array.isArray(value)) {
|
|
178
|
-
return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
|
|
179
|
-
}
|
|
180
|
-
if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
181
|
-
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
|
|
182
|
-
}
|
|
183
|
-
if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
184
|
-
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
|
|
185
|
-
}
|
|
186
|
-
// prettier-ignore
|
|
187
|
-
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
188
|
-
const hashed = hash_1.ValueHash.Create(element);
|
|
189
|
-
if (set.has(hashed)) {
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
set.add(hashed);
|
|
194
|
-
}
|
|
195
|
-
} return true; })())) {
|
|
196
|
-
yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
|
|
197
|
-
}
|
|
198
|
-
for (let i = 0; i < value.length; i++) {
|
|
199
|
-
yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
|
|
200
|
-
}
|
|
139
|
+
// --------------------------------------------------------------------------
|
|
140
|
+
// Guards
|
|
141
|
+
// --------------------------------------------------------------------------
|
|
142
|
+
function IsDefined(value) {
|
|
143
|
+
return value !== undefined;
|
|
144
|
+
}
|
|
145
|
+
// --------------------------------------------------------------------------
|
|
146
|
+
// Policies
|
|
147
|
+
// --------------------------------------------------------------------------
|
|
148
|
+
function IsExactOptionalProperty(value, key) {
|
|
149
|
+
return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
|
|
150
|
+
}
|
|
151
|
+
function IsObject(value) {
|
|
152
|
+
const isObject = ValueGuard.IsObject(value);
|
|
153
|
+
return index_1.TypeSystem.AllowArrayObjects ? isObject : isObject && !ValueGuard.IsArray(value);
|
|
154
|
+
}
|
|
155
|
+
function IsRecordObject(value) {
|
|
156
|
+
return IsObject(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
|
|
157
|
+
}
|
|
158
|
+
function IsNumber(value) {
|
|
159
|
+
const isNumber = ValueGuard.IsNumber(value);
|
|
160
|
+
return index_1.TypeSystem.AllowNaN ? isNumber : isNumber && Number.isFinite(value);
|
|
161
|
+
}
|
|
162
|
+
function IsVoid(value) {
|
|
163
|
+
const isUndefined = ValueGuard.IsUndefined(value);
|
|
164
|
+
return index_1.TypeSystem.AllowVoidNull ? isUndefined || value === null : isUndefined;
|
|
165
|
+
}
|
|
166
|
+
// --------------------------------------------------------------------------
|
|
167
|
+
// Types
|
|
168
|
+
// --------------------------------------------------------------------------
|
|
169
|
+
function* TAny(schema, references, path, value) { }
|
|
170
|
+
function* TArray(schema, references, path, value) {
|
|
171
|
+
if (!ValueGuard.IsArray(value)) {
|
|
172
|
+
return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
|
|
201
173
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` };
|
|
205
|
-
}
|
|
206
|
-
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) {
|
|
207
|
-
yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` };
|
|
208
|
-
}
|
|
209
|
-
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
210
|
-
yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` };
|
|
211
|
-
}
|
|
212
|
-
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
213
|
-
yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` };
|
|
214
|
-
}
|
|
215
|
-
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
216
|
-
yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` };
|
|
217
|
-
}
|
|
218
|
-
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
219
|
-
yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` };
|
|
220
|
-
}
|
|
174
|
+
if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
175
|
+
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
|
|
221
176
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
|
|
225
|
-
}
|
|
177
|
+
if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
178
|
+
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
|
|
226
179
|
}
|
|
227
|
-
|
|
228
|
-
yield* Visit(schema.
|
|
180
|
+
for (let i = 0; i < value.length; i++) {
|
|
181
|
+
yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
|
|
229
182
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
|
|
236
|
-
}
|
|
237
|
-
if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
238
|
-
yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
|
|
239
|
-
}
|
|
240
|
-
if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
241
|
-
yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
|
|
242
|
-
}
|
|
243
|
-
if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
244
|
-
yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
|
|
183
|
+
// prettier-ignore
|
|
184
|
+
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
185
|
+
const hashed = ValueHash.Hash(element);
|
|
186
|
+
if (set.has(hashed)) {
|
|
187
|
+
return false;
|
|
245
188
|
}
|
|
246
|
-
|
|
247
|
-
|
|
189
|
+
else {
|
|
190
|
+
set.add(hashed);
|
|
248
191
|
}
|
|
192
|
+
} return true; })())) {
|
|
193
|
+
yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
|
|
249
194
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}
|
|
195
|
+
// contains
|
|
196
|
+
if (!(IsDefined(schema.contains) || IsNumber(schema.minContains) || IsNumber(schema.maxContains))) {
|
|
197
|
+
return;
|
|
254
198
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
259
|
-
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
260
|
-
yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
|
|
261
|
-
}
|
|
262
|
-
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
263
|
-
yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
|
|
264
|
-
}
|
|
265
|
-
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
266
|
-
yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
|
|
267
|
-
}
|
|
268
|
-
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
269
|
-
yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
|
|
270
|
-
}
|
|
271
|
-
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
272
|
-
yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
|
|
273
|
-
}
|
|
199
|
+
const containsSchema = IsDefined(schema.contains) ? schema.contains : Types.Type.Never();
|
|
200
|
+
const containsCount = value.reduce((acc, value, index) => (Visit(containsSchema, references, `${path}${index}`, value).next().done === true ? acc + 1 : acc), 0);
|
|
201
|
+
if (containsCount === 0) {
|
|
202
|
+
yield { type: ValueErrorType.ArrayContains, schema, path, value, message: `Expected array to contain at least one matching type` };
|
|
274
203
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
204
|
+
if (ValueGuard.IsNumber(schema.minContains) && containsCount < schema.minContains) {
|
|
205
|
+
yield { type: ValueErrorType.ArrayMinContains, schema, path, value, message: `Expected array to contain at least ${schema.minContains} matching types` };
|
|
206
|
+
}
|
|
207
|
+
if (ValueGuard.IsNumber(schema.maxContains) && containsCount > schema.maxContains) {
|
|
208
|
+
yield { type: ValueErrorType.ArrayMaxContains, schema, path, value, message: `Expected array to contain no more than ${schema.maxContains} matching types` };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function* TAsyncIterator(schema, references, path, value) {
|
|
212
|
+
if (!ValueGuard.IsAsyncIterator(value)) {
|
|
213
|
+
yield { type: ValueErrorType.AsyncIterator, schema, path, value, message: `Expected value to be an async iterator` };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function* TBigInt(schema, references, path, value) {
|
|
217
|
+
if (!ValueGuard.IsBigInt(value)) {
|
|
218
|
+
return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` };
|
|
219
|
+
}
|
|
220
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === BigInt(0))) {
|
|
221
|
+
yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` };
|
|
222
|
+
}
|
|
223
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
224
|
+
yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` };
|
|
225
|
+
}
|
|
226
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
227
|
+
yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` };
|
|
228
|
+
}
|
|
229
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
230
|
+
yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` };
|
|
231
|
+
}
|
|
232
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
233
|
+
yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function* TBoolean(schema, references, path, value) {
|
|
237
|
+
if (!ValueGuard.IsBoolean(value)) {
|
|
238
|
+
return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function* TConstructor(schema, references, path, value) {
|
|
242
|
+
yield* Visit(schema.returns, references, path, value.prototype);
|
|
243
|
+
}
|
|
244
|
+
function* TDate(schema, references, path, value) {
|
|
245
|
+
if (!ValueGuard.IsDate(value)) {
|
|
246
|
+
return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
|
|
247
|
+
}
|
|
248
|
+
if (!isFinite(value.getTime())) {
|
|
249
|
+
return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
|
|
250
|
+
}
|
|
251
|
+
if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
252
|
+
yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
|
|
253
|
+
}
|
|
254
|
+
if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
255
|
+
yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
|
|
256
|
+
}
|
|
257
|
+
if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
258
|
+
yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
|
|
259
|
+
}
|
|
260
|
+
if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
261
|
+
yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function* TFunction(schema, references, path, value) {
|
|
265
|
+
if (!ValueGuard.IsFunction(value)) {
|
|
266
|
+
return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function* TInteger(schema, references, path, value) {
|
|
270
|
+
if (!ValueGuard.IsInteger(value)) {
|
|
271
|
+
return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
|
|
272
|
+
}
|
|
273
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
274
|
+
yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
|
|
275
|
+
}
|
|
276
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
277
|
+
yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
|
|
278
|
+
}
|
|
279
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
280
|
+
yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
|
|
281
|
+
}
|
|
282
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
283
|
+
yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
|
|
284
|
+
}
|
|
285
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
286
|
+
yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function* TIntersect(schema, references, path, value) {
|
|
290
|
+
for (const inner of schema.allOf) {
|
|
291
|
+
const next = Visit(inner, references, path, value).next();
|
|
292
|
+
if (!next.done) {
|
|
293
|
+
yield next.value;
|
|
294
|
+
yield { type: ValueErrorType.Intersect, schema, path, value, message: `Expected all sub schemas to be valid` };
|
|
295
|
+
return;
|
|
283
296
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
}
|
|
297
|
+
}
|
|
298
|
+
if (schema.unevaluatedProperties === false) {
|
|
299
|
+
const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
300
|
+
for (const valueKey of Object.getOwnPropertyNames(value)) {
|
|
301
|
+
if (!keyCheck.test(valueKey)) {
|
|
302
|
+
yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Unexpected property` };
|
|
290
303
|
}
|
|
291
304
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
305
|
+
}
|
|
306
|
+
if (typeof schema.unevaluatedProperties === 'object') {
|
|
307
|
+
const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
308
|
+
for (const valueKey of Object.getOwnPropertyNames(value)) {
|
|
309
|
+
if (!keyCheck.test(valueKey)) {
|
|
310
|
+
const next = Visit(schema.unevaluatedProperties, references, `${path}/${valueKey}`, value[valueKey]).next();
|
|
311
|
+
if (!next.done) {
|
|
312
|
+
yield next.value;
|
|
313
|
+
yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Invalid additional property` };
|
|
314
|
+
return;
|
|
302
315
|
}
|
|
303
316
|
}
|
|
304
317
|
}
|
|
305
318
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
319
|
+
}
|
|
320
|
+
function* TIterator(schema, references, path, value) {
|
|
321
|
+
if (!(IsObject(value) && Symbol.iterator in value)) {
|
|
322
|
+
yield { type: ValueErrorType.Iterator, schema, path, value, message: `Expected value to be an iterator` };
|
|
311
323
|
}
|
|
312
|
-
|
|
313
|
-
|
|
324
|
+
}
|
|
325
|
+
function* TLiteral(schema, references, path, value) {
|
|
326
|
+
if (!(value === schema.const)) {
|
|
327
|
+
const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
|
|
328
|
+
return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
|
|
314
329
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
330
|
+
}
|
|
331
|
+
function* TNever(schema, references, path, value) {
|
|
332
|
+
yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
|
|
333
|
+
}
|
|
334
|
+
function* TNot(schema, references, path, value) {
|
|
335
|
+
if (Visit(schema.not, references, path, value).next().done === true) {
|
|
336
|
+
yield { type: ValueErrorType.Not, schema, path, value, message: `Value should not validate` };
|
|
319
337
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
338
|
+
}
|
|
339
|
+
function* TNull(schema, references, path, value) {
|
|
340
|
+
if (!ValueGuard.IsNull(value)) {
|
|
341
|
+
return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
|
|
324
342
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
330
|
-
yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
|
|
331
|
-
}
|
|
332
|
-
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
333
|
-
yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
|
|
334
|
-
}
|
|
335
|
-
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
336
|
-
yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
|
|
337
|
-
}
|
|
338
|
-
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
339
|
-
yield { type: ValueErrorType.NumberMinimum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
|
|
340
|
-
}
|
|
341
|
-
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
342
|
-
yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
343
|
-
}
|
|
343
|
+
}
|
|
344
|
+
function* TNumber(schema, references, path, value) {
|
|
345
|
+
if (!IsNumber(value)) {
|
|
346
|
+
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
344
347
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` };
|
|
381
|
-
}
|
|
348
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
349
|
+
yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
|
|
350
|
+
}
|
|
351
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
352
|
+
yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
|
|
353
|
+
}
|
|
354
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
355
|
+
yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
|
|
356
|
+
}
|
|
357
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
358
|
+
yield { type: ValueErrorType.NumberMinimum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
|
|
359
|
+
}
|
|
360
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
361
|
+
yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
function* TObject(schema, references, path, value) {
|
|
365
|
+
if (!IsObject(value)) {
|
|
366
|
+
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
367
|
+
}
|
|
368
|
+
if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
369
|
+
yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
|
|
370
|
+
}
|
|
371
|
+
if (IsDefined(schema.maxProperties) && !(Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
372
|
+
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have no more than ${schema.maxProperties} properties` };
|
|
373
|
+
}
|
|
374
|
+
const requiredKeys = Array.isArray(schema.required) ? schema.required : [];
|
|
375
|
+
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
376
|
+
const unknownKeys = Object.getOwnPropertyNames(value);
|
|
377
|
+
for (const knownKey of knownKeys) {
|
|
378
|
+
const property = schema.properties[knownKey];
|
|
379
|
+
if (schema.required && schema.required.includes(knownKey)) {
|
|
380
|
+
yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
|
|
381
|
+
if (Types.ExtendsUndefined.Check(schema) && !(knownKey in value)) {
|
|
382
|
+
yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${knownKey}`, value: undefined, message: `Expected required property` };
|
|
382
383
|
}
|
|
383
384
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
continue;
|
|
388
|
-
yield* Visit(schema.additionalProperties, references, `${path}/${valueKey}`, value[valueKey]);
|
|
385
|
+
else {
|
|
386
|
+
if (IsExactOptionalProperty(value, knownKey)) {
|
|
387
|
+
yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
|
|
389
388
|
}
|
|
390
389
|
}
|
|
391
390
|
}
|
|
392
|
-
|
|
393
|
-
if (
|
|
394
|
-
|
|
395
|
-
}
|
|
391
|
+
for (const requiredKey of requiredKeys) {
|
|
392
|
+
if (unknownKeys.includes(requiredKey))
|
|
393
|
+
continue;
|
|
394
|
+
yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
|
|
396
395
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
|
|
403
|
-
}
|
|
404
|
-
if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
405
|
-
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
|
|
396
|
+
if (schema.additionalProperties === false) {
|
|
397
|
+
for (const valueKey of unknownKeys) {
|
|
398
|
+
if (!knownKeys.includes(valueKey)) {
|
|
399
|
+
yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` };
|
|
400
|
+
}
|
|
406
401
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
for (const
|
|
410
|
-
if (
|
|
411
|
-
yield* Visit(patternSchema, references, `${path}/${propertyKey}`, propertyValue);
|
|
402
|
+
}
|
|
403
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
404
|
+
for (const valueKey of unknownKeys) {
|
|
405
|
+
if (knownKeys.includes(valueKey))
|
|
412
406
|
continue;
|
|
413
|
-
}
|
|
414
|
-
if (typeof schema.additionalProperties === 'object') {
|
|
415
|
-
yield* Visit(schema.additionalProperties, references, `${path}/${propertyKey}`, propertyValue);
|
|
416
|
-
}
|
|
417
|
-
if (schema.additionalProperties === false) {
|
|
418
|
-
const propertyPath = `${path}/${propertyKey}`;
|
|
419
|
-
const message = `Unexpected property '${propertyPath}'`;
|
|
420
|
-
return yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: propertyPath, value: propertyValue, message };
|
|
421
|
-
}
|
|
407
|
+
yield* Visit(schema.additionalProperties, references, `${path}/${valueKey}`, value[valueKey]);
|
|
422
408
|
}
|
|
423
409
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
const target = references[index];
|
|
429
|
-
yield* Visit(target, references, path, value);
|
|
410
|
+
}
|
|
411
|
+
function* TPromise(schema, references, path, value) {
|
|
412
|
+
if (!ValueGuard.IsPromise(value)) {
|
|
413
|
+
yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
|
|
430
414
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
415
|
+
}
|
|
416
|
+
function* TRecord(schema, references, path, value) {
|
|
417
|
+
if (!IsRecordObject(value)) {
|
|
418
|
+
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected record object` };
|
|
419
|
+
}
|
|
420
|
+
if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
421
|
+
yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
|
|
422
|
+
}
|
|
423
|
+
if (IsDefined(schema.maxProperties) && !(Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
424
|
+
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have no more than ${schema.maxProperties} properties` };
|
|
425
|
+
}
|
|
426
|
+
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
427
|
+
const regex = new RegExp(patternKey);
|
|
428
|
+
for (const [propertyKey, propertyValue] of Object.entries(value)) {
|
|
429
|
+
if (regex.test(propertyKey)) {
|
|
430
|
+
yield* Visit(patternSchema, references, `${path}/${propertyKey}`, propertyValue);
|
|
431
|
+
continue;
|
|
440
432
|
}
|
|
441
|
-
if (schema.
|
|
442
|
-
|
|
443
|
-
if (!regex.test(value)) {
|
|
444
|
-
yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
|
|
445
|
-
}
|
|
433
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
434
|
+
yield* Visit(schema.additionalProperties, references, `${path}/${propertyKey}`, propertyValue);
|
|
446
435
|
}
|
|
447
|
-
if (schema.
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
}
|
|
451
|
-
else {
|
|
452
|
-
const format = Types.FormatRegistry.Get(schema.format);
|
|
453
|
-
if (!format(value)) {
|
|
454
|
-
yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
|
|
455
|
-
}
|
|
456
|
-
}
|
|
436
|
+
if (schema.additionalProperties === false) {
|
|
437
|
+
const propertyPath = `${path}/${propertyKey}`;
|
|
438
|
+
const message = `Unexpected property '${propertyPath}'`;
|
|
439
|
+
return yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: propertyPath, value: propertyValue, message };
|
|
457
440
|
}
|
|
458
441
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
442
|
+
}
|
|
443
|
+
function* TRef(schema, references, path, value) {
|
|
444
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
445
|
+
if (index === -1)
|
|
446
|
+
throw new ValueErrorsDereferenceError(schema);
|
|
447
|
+
const target = references[index];
|
|
448
|
+
yield* Visit(target, references, path, value);
|
|
449
|
+
}
|
|
450
|
+
function* TString(schema, references, path, value) {
|
|
451
|
+
if (!ValueGuard.IsString(value)) {
|
|
452
|
+
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
463
453
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
454
|
+
if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
|
|
455
|
+
yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
|
|
456
|
+
}
|
|
457
|
+
if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) {
|
|
458
|
+
yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
|
|
459
|
+
}
|
|
460
|
+
if (ValueGuard.IsString(schema.pattern)) {
|
|
468
461
|
const regex = new RegExp(schema.pattern);
|
|
469
462
|
if (!regex.test(value)) {
|
|
470
463
|
yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
|
|
471
464
|
}
|
|
472
465
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
throw new ValueErrorsDereferenceError(schema);
|
|
477
|
-
const target = references[index];
|
|
478
|
-
yield* Visit(target, references, path, value);
|
|
479
|
-
}
|
|
480
|
-
function* Tuple(schema, references, path, value) {
|
|
481
|
-
if (!globalThis.Array.isArray(value)) {
|
|
482
|
-
return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
|
|
466
|
+
if (ValueGuard.IsString(schema.format)) {
|
|
467
|
+
if (!Types.FormatRegistry.Has(schema.format)) {
|
|
468
|
+
yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
|
|
483
469
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
470
|
+
else {
|
|
471
|
+
const format = Types.FormatRegistry.Get(schema.format);
|
|
472
|
+
if (!format(value)) {
|
|
473
|
+
yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
|
|
474
|
+
}
|
|
489
475
|
}
|
|
490
|
-
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
function* TSymbol(schema, references, path, value) {
|
|
479
|
+
if (!ValueGuard.IsSymbol(value)) {
|
|
480
|
+
return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
function* TTemplateLiteral(schema, references, path, value) {
|
|
484
|
+
if (!ValueGuard.IsString(value)) {
|
|
485
|
+
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
486
|
+
}
|
|
487
|
+
const regex = new RegExp(schema.pattern);
|
|
488
|
+
if (!regex.test(value)) {
|
|
489
|
+
yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function* TThis(schema, references, path, value) {
|
|
493
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
494
|
+
if (index === -1)
|
|
495
|
+
throw new ValueErrorsDereferenceError(schema);
|
|
496
|
+
const target = references[index];
|
|
497
|
+
yield* Visit(target, references, path, value);
|
|
498
|
+
}
|
|
499
|
+
function* TTuple(schema, references, path, value) {
|
|
500
|
+
if (!ValueGuard.IsArray(value)) {
|
|
501
|
+
return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
|
|
502
|
+
}
|
|
503
|
+
if (schema.items === undefined && !(value.length === 0)) {
|
|
504
|
+
return yield { type: ValueErrorType.TupleZeroLength, schema, path, value, message: 'Expected tuple to have 0 elements' };
|
|
505
|
+
}
|
|
506
|
+
if (!(value.length === schema.maxItems)) {
|
|
507
|
+
yield { type: ValueErrorType.TupleLength, schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
|
|
508
|
+
}
|
|
509
|
+
if (!schema.items) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
for (let i = 0; i < schema.items.length; i++) {
|
|
513
|
+
yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
function* TUndefined(schema, references, path, value) {
|
|
517
|
+
if (!(value === undefined)) {
|
|
518
|
+
yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function* TUnion(schema, references, path, value) {
|
|
522
|
+
const errors = [];
|
|
523
|
+
for (const inner of schema.anyOf) {
|
|
524
|
+
const variantErrors = [...Visit(inner, references, path, value)];
|
|
525
|
+
if (variantErrors.length === 0)
|
|
491
526
|
return;
|
|
492
|
-
|
|
493
|
-
for (let i = 0; i < schema.items.length; i++) {
|
|
494
|
-
yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
|
|
495
|
-
}
|
|
527
|
+
errors.push(...variantErrors);
|
|
496
528
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
|
|
500
|
-
}
|
|
529
|
+
if (errors.length > 0) {
|
|
530
|
+
yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
|
|
501
531
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
for (const inner of schema.anyOf) {
|
|
505
|
-
const variantErrors = [...Visit(inner, references, path, value)];
|
|
506
|
-
if (variantErrors.length === 0)
|
|
507
|
-
return;
|
|
508
|
-
errors.push(...variantErrors);
|
|
509
|
-
}
|
|
510
|
-
if (errors.length > 0) {
|
|
511
|
-
yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
|
|
512
|
-
}
|
|
513
|
-
for (const error of errors) {
|
|
514
|
-
yield error;
|
|
515
|
-
}
|
|
532
|
+
for (const error of errors) {
|
|
533
|
+
yield error;
|
|
516
534
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
}
|
|
521
|
-
if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
522
|
-
yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
|
|
523
|
-
}
|
|
524
|
-
if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
|
|
525
|
-
yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
|
|
526
|
-
}
|
|
535
|
+
}
|
|
536
|
+
function* TUint8Array(schema, references, path, value) {
|
|
537
|
+
if (!ValueGuard.IsUint8Array(value)) {
|
|
538
|
+
return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
|
|
527
539
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
if (!IsVoid(value)) {
|
|
531
|
-
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected void` };
|
|
532
|
-
}
|
|
540
|
+
if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
541
|
+
yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
|
|
533
542
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
if (!check(schema, value)) {
|
|
537
|
-
return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
|
|
538
|
-
}
|
|
543
|
+
if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
|
|
544
|
+
yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
|
|
539
545
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
return yield* Any(schema_, references_, path, value);
|
|
546
|
-
case 'Array':
|
|
547
|
-
return yield* Array(schema_, references_, path, value);
|
|
548
|
-
case 'BigInt':
|
|
549
|
-
return yield* BigInt(schema_, references_, path, value);
|
|
550
|
-
case 'Boolean':
|
|
551
|
-
return yield* Boolean(schema_, references_, path, value);
|
|
552
|
-
case 'Constructor':
|
|
553
|
-
return yield* Constructor(schema_, references_, path, value);
|
|
554
|
-
case 'Date':
|
|
555
|
-
return yield* Date(schema_, references_, path, value);
|
|
556
|
-
case 'Function':
|
|
557
|
-
return yield* Function(schema_, references_, path, value);
|
|
558
|
-
case 'Integer':
|
|
559
|
-
return yield* Integer(schema_, references_, path, value);
|
|
560
|
-
case 'Intersect':
|
|
561
|
-
return yield* Intersect(schema_, references_, path, value);
|
|
562
|
-
case 'Literal':
|
|
563
|
-
return yield* Literal(schema_, references_, path, value);
|
|
564
|
-
case 'Never':
|
|
565
|
-
return yield* Never(schema_, references_, path, value);
|
|
566
|
-
case 'Not':
|
|
567
|
-
return yield* Not(schema_, references_, path, value);
|
|
568
|
-
case 'Null':
|
|
569
|
-
return yield* Null(schema_, references_, path, value);
|
|
570
|
-
case 'Number':
|
|
571
|
-
return yield* Number(schema_, references_, path, value);
|
|
572
|
-
case 'Object':
|
|
573
|
-
return yield* Object(schema_, references_, path, value);
|
|
574
|
-
case 'Promise':
|
|
575
|
-
return yield* Promise(schema_, references_, path, value);
|
|
576
|
-
case 'Record':
|
|
577
|
-
return yield* Record(schema_, references_, path, value);
|
|
578
|
-
case 'Ref':
|
|
579
|
-
return yield* Ref(schema_, references_, path, value);
|
|
580
|
-
case 'String':
|
|
581
|
-
return yield* String(schema_, references_, path, value);
|
|
582
|
-
case 'Symbol':
|
|
583
|
-
return yield* Symbol(schema_, references_, path, value);
|
|
584
|
-
case 'TemplateLiteral':
|
|
585
|
-
return yield* TemplateLiteral(schema_, references_, path, value);
|
|
586
|
-
case 'This':
|
|
587
|
-
return yield* This(schema_, references_, path, value);
|
|
588
|
-
case 'Tuple':
|
|
589
|
-
return yield* Tuple(schema_, references_, path, value);
|
|
590
|
-
case 'Undefined':
|
|
591
|
-
return yield* Undefined(schema_, references_, path, value);
|
|
592
|
-
case 'Union':
|
|
593
|
-
return yield* Union(schema_, references_, path, value);
|
|
594
|
-
case 'Uint8Array':
|
|
595
|
-
return yield* Uint8Array(schema_, references_, path, value);
|
|
596
|
-
case 'Unknown':
|
|
597
|
-
return yield* Unknown(schema_, references_, path, value);
|
|
598
|
-
case 'Void':
|
|
599
|
-
return yield* Void(schema_, references_, path, value);
|
|
600
|
-
default:
|
|
601
|
-
if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
|
|
602
|
-
throw new ValueErrorsUnknownTypeError(schema);
|
|
603
|
-
return yield* UserDefined(schema_, references_, path, value);
|
|
604
|
-
}
|
|
546
|
+
}
|
|
547
|
+
function* TUnknown(schema, references, path, value) { }
|
|
548
|
+
function* TVoid(schema, references, path, value) {
|
|
549
|
+
if (!IsVoid(value)) {
|
|
550
|
+
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected void` };
|
|
605
551
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
552
|
+
}
|
|
553
|
+
function* TKind(schema, references, path, value) {
|
|
554
|
+
const check = Types.TypeRegistry.Get(schema[Types.Kind]);
|
|
555
|
+
if (!check(schema, value)) {
|
|
556
|
+
return yield { type: ValueErrorType.Kind, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
function* Visit(schema, references, path, value) {
|
|
560
|
+
const references_ = IsDefined(schema.$id) ? [...references, schema] : references;
|
|
561
|
+
const schema_ = schema;
|
|
562
|
+
switch (schema_[Types.Kind]) {
|
|
563
|
+
case 'Any':
|
|
564
|
+
return yield* TAny(schema_, references_, path, value);
|
|
565
|
+
case 'Array':
|
|
566
|
+
return yield* TArray(schema_, references_, path, value);
|
|
567
|
+
case 'AsyncIterator':
|
|
568
|
+
return yield* TAsyncIterator(schema_, references_, path, value);
|
|
569
|
+
case 'BigInt':
|
|
570
|
+
return yield* TBigInt(schema_, references_, path, value);
|
|
571
|
+
case 'Boolean':
|
|
572
|
+
return yield* TBoolean(schema_, references_, path, value);
|
|
573
|
+
case 'Constructor':
|
|
574
|
+
return yield* TConstructor(schema_, references_, path, value);
|
|
575
|
+
case 'Date':
|
|
576
|
+
return yield* TDate(schema_, references_, path, value);
|
|
577
|
+
case 'Function':
|
|
578
|
+
return yield* TFunction(schema_, references_, path, value);
|
|
579
|
+
case 'Integer':
|
|
580
|
+
return yield* TInteger(schema_, references_, path, value);
|
|
581
|
+
case 'Intersect':
|
|
582
|
+
return yield* TIntersect(schema_, references_, path, value);
|
|
583
|
+
case 'Iterator':
|
|
584
|
+
return yield* TIterator(schema_, references_, path, value);
|
|
585
|
+
case 'Literal':
|
|
586
|
+
return yield* TLiteral(schema_, references_, path, value);
|
|
587
|
+
case 'Never':
|
|
588
|
+
return yield* TNever(schema_, references_, path, value);
|
|
589
|
+
case 'Not':
|
|
590
|
+
return yield* TNot(schema_, references_, path, value);
|
|
591
|
+
case 'Null':
|
|
592
|
+
return yield* TNull(schema_, references_, path, value);
|
|
593
|
+
case 'Number':
|
|
594
|
+
return yield* TNumber(schema_, references_, path, value);
|
|
595
|
+
case 'Object':
|
|
596
|
+
return yield* TObject(schema_, references_, path, value);
|
|
597
|
+
case 'Promise':
|
|
598
|
+
return yield* TPromise(schema_, references_, path, value);
|
|
599
|
+
case 'Record':
|
|
600
|
+
return yield* TRecord(schema_, references_, path, value);
|
|
601
|
+
case 'Ref':
|
|
602
|
+
return yield* TRef(schema_, references_, path, value);
|
|
603
|
+
case 'String':
|
|
604
|
+
return yield* TString(schema_, references_, path, value);
|
|
605
|
+
case 'Symbol':
|
|
606
|
+
return yield* TSymbol(schema_, references_, path, value);
|
|
607
|
+
case 'TemplateLiteral':
|
|
608
|
+
return yield* TTemplateLiteral(schema_, references_, path, value);
|
|
609
|
+
case 'This':
|
|
610
|
+
return yield* TThis(schema_, references_, path, value);
|
|
611
|
+
case 'Tuple':
|
|
612
|
+
return yield* TTuple(schema_, references_, path, value);
|
|
613
|
+
case 'Undefined':
|
|
614
|
+
return yield* TUndefined(schema_, references_, path, value);
|
|
615
|
+
case 'Union':
|
|
616
|
+
return yield* TUnion(schema_, references_, path, value);
|
|
617
|
+
case 'Uint8Array':
|
|
618
|
+
return yield* TUint8Array(schema_, references_, path, value);
|
|
619
|
+
case 'Unknown':
|
|
620
|
+
return yield* TUnknown(schema_, references_, path, value);
|
|
621
|
+
case 'Void':
|
|
622
|
+
return yield* TVoid(schema_, references_, path, value);
|
|
623
|
+
default:
|
|
624
|
+
if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
|
|
625
|
+
throw new ValueErrorsUnknownTypeError(schema);
|
|
626
|
+
return yield* TKind(schema_, references_, path, value);
|
|
609
627
|
}
|
|
610
|
-
|
|
611
|
-
|
|
628
|
+
}
|
|
629
|
+
/** Returns an iterator for each error in this value. */
|
|
630
|
+
function Errors(...args) {
|
|
631
|
+
const iterator = args.length === 3 ? Visit(args[0], args[1], '', args[2]) : Visit(args[0], [], '', args[1]);
|
|
632
|
+
return new ValueErrorIterator(iterator);
|
|
633
|
+
}
|
|
634
|
+
exports.Errors = Errors;
|