@sinclair/typebox 0.24.5 → 0.24.8

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/value/check.js CHANGED
@@ -27,206 +27,260 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.CheckValue = void 0;
30
+ exports.ValueCheck = void 0;
31
31
  const Types = require("../typebox");
32
- var CheckValue;
33
- (function (CheckValue) {
34
- const referenceMap = new Map();
35
- function Any(schema, value) {
32
+ var ValueCheck;
33
+ (function (ValueCheck) {
34
+ function Any(schema, references, value) {
36
35
  return true;
37
36
  }
38
- function Array(schema, value) {
39
- if (typeof value !== 'object' || !globalThis.Array.isArray(value))
37
+ function Array(schema, references, value) {
38
+ if (!globalThis.Array.isArray(value)) {
40
39
  return false;
41
- for (let i = 0; i < value.length; i++) {
42
- if (!Visit(schema.items, value[i]))
43
- return false;
44
40
  }
45
- return true;
41
+ return value.every((val) => Visit(schema.items, references, val));
46
42
  }
47
- function Boolean(schema, value) {
43
+ function Boolean(schema, references, value) {
48
44
  return typeof value === 'boolean';
49
45
  }
50
- function Constructor(schema, value) {
51
- return Visit(schema.returns, value.prototype); // check return type only (as an object)
52
- }
53
- function Enum(schema, value) {
54
- for (const subschema of schema.anyOf) {
55
- if (subschema.const === value)
56
- return true;
57
- }
58
- return false;
46
+ function Constructor(schema, references, value) {
47
+ return Visit(schema.returns, references, value);
59
48
  }
60
- function Function(schema, value) {
49
+ function Function(schema, references, value) {
61
50
  return typeof value === 'function';
62
51
  }
63
- function Integer(schema, value) {
64
- return typeof value === 'number' && globalThis.Number.isInteger(value);
65
- }
66
- function Intersect(schema, value) {
67
- return Object(schema, value);
52
+ function Integer(schema, references, value) {
53
+ if (!(typeof value === 'number')) {
54
+ return false;
55
+ }
56
+ if (!globalThis.Number.isInteger(value)) {
57
+ return false;
58
+ }
59
+ if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
60
+ return false;
61
+ }
62
+ if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
63
+ return false;
64
+ }
65
+ if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
66
+ return false;
67
+ }
68
+ if (schema.minimum && !(value >= schema.minimum)) {
69
+ return false;
70
+ }
71
+ if (schema.maximum && !(value <= schema.maximum)) {
72
+ return false;
73
+ }
74
+ return true;
68
75
  }
69
- function Literal(schema, value) {
70
- return schema.const === value;
76
+ function Literal(schema, references, value) {
77
+ return value === schema.const;
71
78
  }
72
- function Null(schema, value) {
79
+ function Null(schema, references, value) {
73
80
  return value === null;
74
81
  }
75
- function Number(schema, value) {
76
- return typeof value === 'number';
82
+ function Number(schema, references, value) {
83
+ if (!(typeof value === 'number')) {
84
+ return false;
85
+ }
86
+ if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
87
+ return false;
88
+ }
89
+ if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
90
+ return false;
91
+ }
92
+ if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
93
+ return false;
94
+ }
95
+ if (schema.minimum && !(value >= schema.minimum)) {
96
+ return false;
97
+ }
98
+ if (schema.maximum && !(value <= schema.maximum)) {
99
+ return false;
100
+ }
101
+ return true;
77
102
  }
78
- function Object(schema, value) {
79
- if (typeof value !== 'object' || value === null)
103
+ function Object(schema, references, value) {
104
+ if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
105
+ return false;
106
+ }
107
+ if (schema.minProperties !== undefined && !(globalThis.Object.keys(value).length >= schema.minProperties)) {
108
+ return false;
109
+ }
110
+ if (schema.maxProperties !== undefined && !(globalThis.Object.keys(value).length <= schema.maxProperties)) {
80
111
  return false;
112
+ }
81
113
  const propertyKeys = globalThis.Object.keys(schema.properties);
82
114
  if (schema.additionalProperties === false) {
83
- const valueKeys = globalThis.Object.keys(value);
84
- for (const valueKey of valueKeys) {
85
- if (!propertyKeys.includes(valueKey))
115
+ // optimization: If the property key length matches the required keys length
116
+ // then we only need check that the values property key length matches that
117
+ // of the property key length. This is because exhaustive testing for values
118
+ // will occur in subsequent property tests.
119
+ if (schema.required && schema.required.length === propertyKeys.length && !(globalThis.Object.keys(value).length === propertyKeys.length)) {
120
+ return false;
121
+ }
122
+ else {
123
+ if (!globalThis.Object.keys(value).every((key) => propertyKeys.includes(key))) {
86
124
  return false;
125
+ }
87
126
  }
88
127
  }
89
128
  for (const propertyKey of propertyKeys) {
90
129
  const propertySchema = schema.properties[propertyKey];
91
- const propertyValue = value[propertyKey];
92
- if (propertyValue === undefined && schema.required !== undefined && !schema.required.includes(propertyKey))
93
- return true;
94
- if (!Visit(propertySchema, propertyValue))
95
- return false;
130
+ if (schema.required && schema.required.includes(propertyKey)) {
131
+ if (!Visit(propertySchema, references, value[propertyKey])) {
132
+ return false;
133
+ }
134
+ }
135
+ else {
136
+ if (value[propertyKey] !== undefined) {
137
+ if (!Visit(propertySchema, references, value[propertyKey])) {
138
+ return false;
139
+ }
140
+ }
141
+ }
96
142
  }
97
143
  return true;
98
144
  }
99
- function Promise(schema, value) {
100
- return typeof value === 'object' && typeof value['then'] === 'function';
145
+ function Promise(schema, references, value) {
146
+ return typeof value === 'object' && typeof value.then === 'function';
101
147
  }
102
- function Record(schema, value) {
103
- if (typeof value !== 'object' || value === null)
148
+ function Record(schema, references, value) {
149
+ if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
104
150
  return false;
105
- const propertySchema = globalThis.Object.values(schema.patternProperties)[0];
106
- for (const key of globalThis.Object.keys(value)) {
107
- const propertyValue = value[key];
108
- if (!Visit(propertySchema, propertyValue))
151
+ }
152
+ const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
153
+ const regex = new RegExp(keyPattern);
154
+ if (!globalThis.Object.keys(value).every((key) => regex.test(key))) {
155
+ return false;
156
+ }
157
+ for (const propValue of globalThis.Object.values(value)) {
158
+ if (!Visit(valueSchema, references, propValue))
109
159
  return false;
110
160
  }
111
161
  return true;
112
162
  }
113
- function Recursive(schema, value) {
114
- throw new Error('Cannot typeof recursive types');
115
- }
116
- function Ref(schema, value) {
117
- throw new Error('Cannot typeof reference types');
163
+ function Ref(schema, references, value) {
164
+ const reference = references.find((reference) => reference.$id === schema.$ref);
165
+ if (reference === undefined)
166
+ throw new Error(`CheckValue.Ref: Cannot find schema with $id '${schema.$ref}'.`);
167
+ return Visit(reference, references, value);
118
168
  }
119
- function Self(schema, value) {
120
- if (!referenceMap.has(schema.$ref))
121
- throw new Error(`Check: Cannot locate schema with $id '${schema.$id}' for referenced type`);
122
- const referenced = referenceMap.get(schema.$ref);
123
- return Visit(referenced, value);
169
+ function Self(schema, references, value) {
170
+ const reference = references.find((reference) => reference.$id === schema.$ref);
171
+ if (reference === undefined)
172
+ throw new Error(`CheckValue.Self: Cannot find schema with $id '${schema.$ref}'.`);
173
+ return Visit(reference, references, value);
124
174
  }
125
- function String(schema, value) {
126
- if (typeof value !== 'string')
175
+ function String(schema, references, value) {
176
+ if (!(typeof value === 'string')) {
127
177
  return false;
178
+ }
128
179
  if (schema.pattern !== undefined) {
129
180
  const regex = new RegExp(schema.pattern);
130
- return value.match(regex) !== null;
181
+ if (!regex.test(value))
182
+ return false;
131
183
  }
132
184
  return true;
133
185
  }
134
- function Tuple(schema, value) {
135
- if (typeof value !== 'object' || !globalThis.Array.isArray(value))
186
+ function Tuple(schema, references, value) {
187
+ if (!global.Array.isArray(value)) {
136
188
  return false;
137
- if (schema.items === undefined && value.length === 0)
138
- return true;
139
- if (schema.items === undefined)
189
+ }
190
+ if (schema.items === undefined && !(value.length === 0)) {
140
191
  return false;
141
- if (value.length < schema.minItems || value.length > schema.maxItems)
192
+ }
193
+ if (!(value.length === schema.maxItems)) {
142
194
  return false;
195
+ }
196
+ if (!schema.items) {
197
+ return true;
198
+ }
143
199
  for (let i = 0; i < schema.items.length; i++) {
144
- if (!Visit(schema.items[i], value[i]))
200
+ if (!Visit(schema.items[i], references, value[i]))
145
201
  return false;
146
202
  }
147
203
  return true;
148
204
  }
149
- function Undefined(schema, value) {
205
+ function Undefined(schema, references, value) {
150
206
  return value === undefined;
151
207
  }
152
- function Union(schema, value) {
153
- for (let i = 0; i < schema.anyOf.length; i++) {
154
- if (Visit(schema.anyOf[i], value))
155
- return true;
156
- }
157
- return false;
208
+ function Union(schema, references, value) {
209
+ return schema.anyOf.some((inner) => Visit(inner, references, value));
158
210
  }
159
- function Uint8Array(schema, value) {
160
- return value instanceof globalThis.Uint8Array;
211
+ function Uint8Array(schema, references, value) {
212
+ if (!(value instanceof globalThis.Uint8Array)) {
213
+ return false;
214
+ }
215
+ if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
216
+ return false;
217
+ }
218
+ if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
219
+ return false;
220
+ }
221
+ return true;
161
222
  }
162
- function Unknown(schema, value) {
223
+ function Unknown(schema, references, value) {
163
224
  return true;
164
225
  }
165
- function Void(schema, value) {
226
+ function Void(schema, references, value) {
166
227
  return value === null;
167
228
  }
168
- function Visit(schema, value) {
169
- if (schema.$id !== undefined)
170
- referenceMap.set(schema.$id, schema);
229
+ function Visit(schema, references, value) {
230
+ const anyReferences = schema.$id === undefined ? references : [schema, ...references];
171
231
  const anySchema = schema;
172
232
  switch (anySchema[Types.Kind]) {
173
233
  case 'Any':
174
- return Any(anySchema, value);
234
+ return Any(anySchema, anyReferences, value);
175
235
  case 'Array':
176
- return Array(anySchema, value);
236
+ return Array(anySchema, anyReferences, value);
177
237
  case 'Boolean':
178
- return Boolean(anySchema, value);
238
+ return Boolean(anySchema, anyReferences, value);
179
239
  case 'Constructor':
180
- return Constructor(anySchema, value);
181
- case 'Enum':
182
- return Enum(anySchema, value);
240
+ return Constructor(anySchema, anyReferences, value);
183
241
  case 'Function':
184
- return Function(anySchema, value);
242
+ return Function(anySchema, anyReferences, value);
185
243
  case 'Integer':
186
- return Integer(anySchema, value);
187
- case 'Intersect':
188
- return Intersect(anySchema, value);
244
+ return Integer(anySchema, anyReferences, value);
189
245
  case 'Literal':
190
- return Literal(anySchema, value);
246
+ return Literal(anySchema, anyReferences, value);
191
247
  case 'Null':
192
- return Null(anySchema, value);
248
+ return Null(anySchema, anyReferences, value);
193
249
  case 'Number':
194
- return Number(anySchema, value);
250
+ return Number(anySchema, anyReferences, value);
195
251
  case 'Object':
196
- return Object(anySchema, value);
252
+ return Object(anySchema, anyReferences, value);
197
253
  case 'Promise':
198
- return Promise(anySchema, value);
254
+ return Promise(anySchema, anyReferences, value);
199
255
  case 'Record':
200
- return Record(anySchema, value);
201
- case 'Rec':
202
- return Recursive(anySchema, value);
256
+ return Record(anySchema, anyReferences, value);
203
257
  case 'Ref':
204
- return Ref(anySchema, value);
258
+ return Ref(anySchema, anyReferences, value);
205
259
  case 'Self':
206
- return Self(anySchema, value);
260
+ return Self(anySchema, anyReferences, value);
207
261
  case 'String':
208
- return String(anySchema, value);
262
+ return String(anySchema, anyReferences, value);
209
263
  case 'Tuple':
210
- return Tuple(anySchema, value);
264
+ return Tuple(anySchema, anyReferences, value);
211
265
  case 'Undefined':
212
- return Undefined(anySchema, value);
266
+ return Undefined(anySchema, anyReferences, value);
213
267
  case 'Union':
214
- return Union(anySchema, value);
268
+ return Union(anySchema, anyReferences, value);
215
269
  case 'Uint8Array':
216
- return Uint8Array(anySchema, value);
270
+ return Uint8Array(anySchema, anyReferences, value);
217
271
  case 'Unknown':
218
- return Unknown(anySchema, value);
272
+ return Unknown(anySchema, anyReferences, value);
219
273
  case 'Void':
220
- return Void(anySchema, value);
274
+ return Void(anySchema, anyReferences, value);
221
275
  default:
222
276
  throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
223
277
  }
224
278
  }
225
- CheckValue.Visit = Visit;
226
- function Check(schema, value) {
227
- if (referenceMap.size > 0)
228
- referenceMap.clear();
229
- return Visit(schema, value);
279
+ // -------------------------------------------------------------------------
280
+ // Check
281
+ // -------------------------------------------------------------------------
282
+ function Check(schema, references, value) {
283
+ return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value);
230
284
  }
231
- CheckValue.Check = Check;
232
- })(CheckValue = exports.CheckValue || (exports.CheckValue = {}));
285
+ ValueCheck.Check = Check;
286
+ })(ValueCheck = exports.ValueCheck || (exports.ValueCheck = {}));
package/value/create.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import * as Types from '../typebox';
2
- export declare namespace CreateValue {
2
+ export declare namespace ValueCreate {
3
3
  /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
4
- function Visit<T extends Types.TSchema>(schema: T): Types.Static<T>;
5
- /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
6
- function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
4
+ function Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
5
+ function Create<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R]): Types.Static<T>;
7
6
  }