@sinclair/typebox 0.25.15 → 0.25.16

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.
@@ -175,7 +175,12 @@ var TypeCompiler;
175
175
  yield `(${value} === null)`;
176
176
  }
177
177
  function* Number(schema, value) {
178
- yield `(typeof ${value} === 'number' && !isNaN(${value}))`;
178
+ if (index_2.TypeSystem.AllowNaN) {
179
+ yield `(typeof ${value} === 'number')`;
180
+ }
181
+ else {
182
+ yield `(typeof ${value} === 'number' && !isNaN(${value}))`;
183
+ }
179
184
  if (IsNumber(schema.multipleOf))
180
185
  yield `(${value} % ${schema.multipleOf} === 0)`;
181
186
  if (IsNumber(schema.exclusiveMinimum))
@@ -188,12 +193,12 @@ var TypeCompiler;
188
193
  yield `(${value} <= ${schema.maximum})`;
189
194
  }
190
195
  function* Object(schema, value) {
191
- if (index_2.TypeSystem.Kind === 'json-schema') {
192
- yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`;
193
- }
194
- else if (index_2.TypeSystem.Kind === 'structural') {
196
+ if (index_2.TypeSystem.AllowArrayObjects) {
195
197
  yield `(typeof ${value} === 'object' && ${value} !== null)`;
196
198
  }
199
+ else {
200
+ yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`;
201
+ }
197
202
  if (IsNumber(schema.minProperties))
198
203
  yield `(Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties})`;
199
204
  if (IsNumber(schema.maxProperties))
@@ -233,12 +238,12 @@ var TypeCompiler;
233
238
  yield `(typeof value === 'object' && typeof ${value}.then === 'function')`;
234
239
  }
235
240
  function* Record(schema, value) {
236
- if (index_2.TypeSystem.Kind === 'json-schema') {
237
- yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date))`;
238
- }
239
- else if (index_2.TypeSystem.Kind === 'structural') {
241
+ if (index_2.TypeSystem.AllowArrayObjects) {
240
242
  yield `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date))`;
241
243
  }
244
+ else {
245
+ yield `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !Array.isArray(${value}))`;
246
+ }
242
247
  const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
243
248
  const local = PushLocal(`new RegExp(/${keyPattern}/)`);
244
249
  yield `(Object.getOwnPropertyNames(${value}).every(key => ${local}.test(key)))`;
package/errors/errors.js CHANGED
@@ -163,10 +163,7 @@ var ValueErrors;
163
163
  }
164
164
  function* Integer(schema, references, path, value) {
165
165
  if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
166
- return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
167
- }
168
- if (!globalThis.Number.isInteger(value)) {
169
- yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
166
+ return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
170
167
  }
171
168
  if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
172
169
  yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
@@ -199,8 +196,15 @@ var ValueErrors;
199
196
  }
200
197
  }
201
198
  function* Number(schema, references, path, value) {
202
- if (!(typeof value === 'number' && !isNaN(value))) {
203
- return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
199
+ if (index_1.TypeSystem.AllowNaN) {
200
+ if (!(typeof value === 'number')) {
201
+ return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
202
+ }
203
+ }
204
+ else {
205
+ if (!(typeof value === 'number' && !isNaN(value))) {
206
+ return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
207
+ }
204
208
  }
205
209
  if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
206
210
  yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
@@ -219,13 +223,13 @@ var ValueErrors;
219
223
  }
220
224
  }
221
225
  function* Object(schema, references, path, value) {
222
- if (index_1.TypeSystem.Kind === 'json-schema') {
223
- if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
226
+ if (index_1.TypeSystem.AllowArrayObjects) {
227
+ if (!(typeof value === 'object' && value !== null)) {
224
228
  return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
225
229
  }
226
230
  }
227
- else if (index_1.TypeSystem.Kind === 'structural') {
228
- if (!(typeof value === 'object' && value !== null)) {
231
+ else {
232
+ if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
229
233
  return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
230
234
  }
231
235
  }
@@ -276,13 +280,13 @@ var ValueErrors;
276
280
  }
277
281
  }
278
282
  function* Record(schema, references, path, value) {
279
- if (index_1.TypeSystem.Kind === 'json-schema') {
280
- if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value) && !(value instanceof globalThis.Date))) {
283
+ if (index_1.TypeSystem.AllowArrayObjects) {
284
+ if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
281
285
  return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
282
286
  }
283
287
  }
284
- else if (index_1.TypeSystem.Kind === 'structural') {
285
- if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
288
+ else {
289
+ if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date) && !globalThis.Array.isArray(value))) {
286
290
  return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
287
291
  }
288
292
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.25.15",
3
+ "version": "0.25.16",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
@@ -1,18 +1,10 @@
1
- export declare class InvalidTypeSystemError extends Error {
2
- constructor(typeSystem: string);
3
- }
4
- export type TypeSystemKind = 'json-schema' | 'structural';
5
- declare class TypeSystemSettings {
6
- private kind;
7
- constructor();
1
+ export declare namespace TypeSystem {
2
+ /**
3
+ * Sets whether arrays should be treated as kinds of objects. The default is `false`
4
+ */
5
+ let AllowArrayObjects: boolean;
8
6
  /**
9
- * `Experimental` Sets the type system kind used by TypeBox. By default TypeBox uses `json-schema` assertion
10
- * rules to verify JavaScript values. If setting the type system to `structural`, TypeBox will use TypeScript
11
- * structural checking rules enabling Arrays to be validated as Objects.
7
+ * Sets whether numeric checks should consider NaN a valid number type. The default is `false`
12
8
  */
13
- get Kind(): TypeSystemKind;
14
- set Kind(value: TypeSystemKind);
9
+ let AllowNaN: boolean;
15
10
  }
16
- /** TypeBox TypeSystem Settings */
17
- export declare const TypeSystem: TypeSystemSettings;
18
- export {};
package/system/system.js CHANGED
@@ -27,31 +27,15 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeSystem = exports.InvalidTypeSystemError = void 0;
31
- class InvalidTypeSystemError extends Error {
32
- constructor(typeSystem) {
33
- super(`TypeSystemSettings: Unknown TypeSystem '${typeSystem}'`);
34
- }
35
- }
36
- exports.InvalidTypeSystemError = InvalidTypeSystemError;
37
- class TypeSystemSettings {
38
- constructor() {
39
- this.kind = 'json-schema';
40
- }
30
+ exports.TypeSystem = void 0;
31
+ var TypeSystem;
32
+ (function (TypeSystem) {
41
33
  /**
42
- * `Experimental` Sets the type system kind used by TypeBox. By default TypeBox uses `json-schema` assertion
43
- * rules to verify JavaScript values. If setting the type system to `structural`, TypeBox will use TypeScript
44
- * structural checking rules enabling Arrays to be validated as Objects.
34
+ * Sets whether arrays should be treated as kinds of objects. The default is `false`
45
35
  */
46
- get Kind() {
47
- return this.kind;
48
- }
49
- set Kind(value) {
50
- if (!(value === 'json-schema' || value === 'structural')) {
51
- throw new InvalidTypeSystemError(value);
52
- }
53
- this.kind = value;
54
- }
55
- }
56
- /** TypeBox TypeSystem Settings */
57
- exports.TypeSystem = new TypeSystemSettings();
36
+ TypeSystem.AllowArrayObjects = false;
37
+ /**
38
+ * Sets whether numeric checks should consider NaN a valid number type. The default is `false`
39
+ */
40
+ TypeSystem.AllowNaN = false;
41
+ })(TypeSystem = exports.TypeSystem || (exports.TypeSystem = {}));
package/value/check.js CHANGED
@@ -103,9 +103,6 @@ var ValueCheck;
103
103
  if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
104
104
  return false;
105
105
  }
106
- if (!globalThis.Number.isInteger(value)) {
107
- return false;
108
- }
109
106
  if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
110
107
  return false;
111
108
  }
@@ -133,8 +130,15 @@ var ValueCheck;
133
130
  return value === null;
134
131
  }
135
132
  function Number(schema, references, value) {
136
- if (!(typeof value === 'number' && !isNaN(value))) {
137
- return false;
133
+ if (index_1.TypeSystem.AllowNaN) {
134
+ if (!(typeof value === 'number')) {
135
+ return false;
136
+ }
137
+ }
138
+ else {
139
+ if (!(typeof value === 'number' && !isNaN(value))) {
140
+ return false;
141
+ }
138
142
  }
139
143
  if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
140
144
  return false;
@@ -154,13 +158,13 @@ var ValueCheck;
154
158
  return true;
155
159
  }
156
160
  function Object(schema, references, value) {
157
- if (index_1.TypeSystem.Kind === 'json-schema') {
158
- if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
161
+ if (index_1.TypeSystem.AllowArrayObjects) {
162
+ if (!(typeof value === 'object' && value !== null)) {
159
163
  return false;
160
164
  }
161
165
  }
162
- else if (index_1.TypeSystem.Kind === 'structural') {
163
- if (!(typeof value === 'object' && value !== null)) {
166
+ else {
167
+ if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
164
168
  return false;
165
169
  }
166
170
  }
@@ -215,13 +219,13 @@ var ValueCheck;
215
219
  return typeof value === 'object' && typeof value.then === 'function';
216
220
  }
217
221
  function Record(schema, references, value) {
218
- if (index_1.TypeSystem.Kind === 'json-schema') {
219
- if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value) && !(value instanceof globalThis.Date))) {
222
+ if (index_1.TypeSystem.AllowArrayObjects) {
223
+ if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
220
224
  return false;
221
225
  }
222
226
  }
223
- else if (index_1.TypeSystem.Kind === 'structural') {
224
- if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
227
+ else {
228
+ if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date) && !globalThis.Array.isArray(value))) {
225
229
  return false;
226
230
  }
227
231
  }