@sinclair/typebox 0.25.5 → 0.25.6
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/errors/errors.js +25 -22
- package/package.json +1 -1
package/errors/errors.js
CHANGED
|
@@ -97,15 +97,18 @@ exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
|
|
|
97
97
|
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
98
98
|
var ValueErrors;
|
|
99
99
|
(function (ValueErrors) {
|
|
100
|
+
function IsNumber(value) {
|
|
101
|
+
return typeof value === 'number';
|
|
102
|
+
}
|
|
100
103
|
function* Any(schema, references, path, value) { }
|
|
101
104
|
function* Array(schema, references, path, value) {
|
|
102
105
|
if (!globalThis.Array.isArray(value)) {
|
|
103
106
|
return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
|
|
104
107
|
}
|
|
105
|
-
if (schema.minItems
|
|
108
|
+
if (IsNumber(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
106
109
|
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
|
|
107
110
|
}
|
|
108
|
-
if (schema.maxItems
|
|
111
|
+
if (IsNumber(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
109
112
|
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
|
|
110
113
|
}
|
|
111
114
|
if (schema.uniqueItems === true && !(new Set(value).size === value.length)) {
|
|
@@ -127,16 +130,16 @@ var ValueErrors;
|
|
|
127
130
|
if (!(value instanceof globalThis.Date)) {
|
|
128
131
|
return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
|
|
129
132
|
}
|
|
130
|
-
if (schema.exclusiveMinimumTimestamp && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
133
|
+
if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
131
134
|
yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
|
|
132
135
|
}
|
|
133
|
-
if (schema.exclusiveMaximumTimestamp && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
136
|
+
if (IsNumber(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
134
137
|
yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
|
|
135
138
|
}
|
|
136
|
-
if (schema.minimumTimestamp && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
139
|
+
if (IsNumber(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
137
140
|
yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
|
|
138
141
|
}
|
|
139
|
-
if (schema.maximumTimestamp && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
142
|
+
if (IsNumber(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
140
143
|
yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
|
|
141
144
|
}
|
|
142
145
|
}
|
|
@@ -152,19 +155,19 @@ var ValueErrors;
|
|
|
152
155
|
if (!globalThis.Number.isInteger(value)) {
|
|
153
156
|
yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
|
|
154
157
|
}
|
|
155
|
-
if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
|
|
158
|
+
if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
156
159
|
yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
|
|
157
160
|
}
|
|
158
|
-
if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
|
|
161
|
+
if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
159
162
|
yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
|
|
160
163
|
}
|
|
161
|
-
if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
|
|
164
|
+
if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
162
165
|
yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
|
|
163
166
|
}
|
|
164
|
-
if (schema.minimum && !(value >= schema.minimum)) {
|
|
167
|
+
if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
|
|
165
168
|
yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
|
|
166
169
|
}
|
|
167
|
-
if (schema.maximum && !(value <= schema.maximum)) {
|
|
170
|
+
if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
|
|
168
171
|
yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
|
|
169
172
|
}
|
|
170
173
|
}
|
|
@@ -186,19 +189,19 @@ var ValueErrors;
|
|
|
186
189
|
if (!(typeof value === 'number')) {
|
|
187
190
|
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
188
191
|
}
|
|
189
|
-
if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
|
|
192
|
+
if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
190
193
|
yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
|
|
191
194
|
}
|
|
192
|
-
if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
|
|
195
|
+
if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
193
196
|
yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
|
|
194
197
|
}
|
|
195
|
-
if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
|
|
198
|
+
if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
196
199
|
yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
|
|
197
200
|
}
|
|
198
|
-
if (schema.minimum && !(value >= schema.minimum)) {
|
|
201
|
+
if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
|
|
199
202
|
yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
|
|
200
203
|
}
|
|
201
|
-
if (schema.maximum && !(value <= schema.maximum)) {
|
|
204
|
+
if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
|
|
202
205
|
yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
203
206
|
}
|
|
204
207
|
}
|
|
@@ -206,10 +209,10 @@ var ValueErrors;
|
|
|
206
209
|
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
207
210
|
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
208
211
|
}
|
|
209
|
-
if (schema.minProperties
|
|
212
|
+
if (IsNumber(schema.minProperties) && !(globalThis.Object.keys(value).length >= schema.minProperties)) {
|
|
210
213
|
yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
|
|
211
214
|
}
|
|
212
|
-
if (schema.maxProperties
|
|
215
|
+
if (IsNumber(schema.maxProperties) && !(globalThis.Object.keys(value).length <= schema.maxProperties)) {
|
|
213
216
|
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
|
|
214
217
|
}
|
|
215
218
|
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
@@ -284,10 +287,10 @@ var ValueErrors;
|
|
|
284
287
|
if (!(typeof value === 'string')) {
|
|
285
288
|
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
286
289
|
}
|
|
287
|
-
if (schema.minLength
|
|
290
|
+
if (IsNumber(schema.minLength) && !(value.length >= schema.minLength)) {
|
|
288
291
|
yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
|
|
289
292
|
}
|
|
290
|
-
if (schema.maxLength
|
|
293
|
+
if (IsNumber(schema.maxLength) && !(value.length <= schema.maxLength)) {
|
|
291
294
|
yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
|
|
292
295
|
}
|
|
293
296
|
if (schema.pattern !== undefined) {
|
|
@@ -349,10 +352,10 @@ var ValueErrors;
|
|
|
349
352
|
if (!(value instanceof globalThis.Uint8Array)) {
|
|
350
353
|
return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
|
|
351
354
|
}
|
|
352
|
-
if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
|
|
355
|
+
if (IsNumber(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
353
356
|
yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
|
|
354
357
|
}
|
|
355
|
-
if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
|
|
358
|
+
if (IsNumber(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
|
|
356
359
|
yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
|
|
357
360
|
}
|
|
358
361
|
}
|