@sinclair/typebox 0.25.6 → 0.25.7
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.js +26 -25
- package/package.json +1 -1
- package/value/check.js +25 -22
package/compiler/compiler.js
CHANGED
|
@@ -105,6 +105,9 @@ exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
|
|
|
105
105
|
/** Compiles Types for Runtime Type Checking */
|
|
106
106
|
var TypeCompiler;
|
|
107
107
|
(function (TypeCompiler) {
|
|
108
|
+
function IsNumber(value) {
|
|
109
|
+
return typeof value === 'number';
|
|
110
|
+
}
|
|
108
111
|
// -------------------------------------------------------------------
|
|
109
112
|
// Types
|
|
110
113
|
// -------------------------------------------------------------------
|
|
@@ -113,11 +116,11 @@ var TypeCompiler;
|
|
|
113
116
|
}
|
|
114
117
|
function* Array(schema, value) {
|
|
115
118
|
const expression = CreateExpression(schema.items, 'value');
|
|
116
|
-
if (schema.minItems
|
|
119
|
+
if (IsNumber(schema.minItems))
|
|
117
120
|
yield `(${value}.length >= ${schema.minItems})`;
|
|
118
|
-
if (schema.maxItems
|
|
121
|
+
if (IsNumber(schema.maxItems))
|
|
119
122
|
yield `(${value}.length <= ${schema.maxItems})`;
|
|
120
|
-
if (schema.uniqueItems
|
|
123
|
+
if (schema.uniqueItems === true)
|
|
121
124
|
yield `(new Set(${value}).size === ${value}.length)`;
|
|
122
125
|
yield `(Array.isArray(${value}) && ${value}.every(value => ${expression}))`;
|
|
123
126
|
}
|
|
@@ -129,13 +132,13 @@ var TypeCompiler;
|
|
|
129
132
|
}
|
|
130
133
|
function* Date(schema, value) {
|
|
131
134
|
yield `(${value} instanceof Date)`;
|
|
132
|
-
if (schema.exclusiveMinimumTimestamp
|
|
135
|
+
if (IsNumber(schema.exclusiveMinimumTimestamp))
|
|
133
136
|
yield `(${value}.getTime() > ${schema.exclusiveMinimumTimestamp})`;
|
|
134
|
-
if (schema.exclusiveMaximumTimestamp
|
|
137
|
+
if (IsNumber(schema.exclusiveMaximumTimestamp))
|
|
135
138
|
yield `(${value}.getTime() < ${schema.exclusiveMaximumTimestamp})`;
|
|
136
|
-
if (schema.minimumTimestamp
|
|
139
|
+
if (IsNumber(schema.minimumTimestamp))
|
|
137
140
|
yield `(${value}.getTime() >= ${schema.minimumTimestamp})`;
|
|
138
|
-
if (schema.maximumTimestamp
|
|
141
|
+
if (IsNumber(schema.maximumTimestamp))
|
|
139
142
|
yield `(${value}.getTime() <= ${schema.maximumTimestamp})`;
|
|
140
143
|
}
|
|
141
144
|
function* Function(schema, value) {
|
|
@@ -143,15 +146,15 @@ var TypeCompiler;
|
|
|
143
146
|
}
|
|
144
147
|
function* Integer(schema, value) {
|
|
145
148
|
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
|
|
146
|
-
if (schema.multipleOf
|
|
149
|
+
if (IsNumber(schema.multipleOf))
|
|
147
150
|
yield `(${value} % ${schema.multipleOf} === 0)`;
|
|
148
|
-
if (schema.exclusiveMinimum
|
|
151
|
+
if (IsNumber(schema.exclusiveMinimum))
|
|
149
152
|
yield `(${value} > ${schema.exclusiveMinimum})`;
|
|
150
|
-
if (schema.exclusiveMaximum
|
|
153
|
+
if (IsNumber(schema.exclusiveMaximum))
|
|
151
154
|
yield `(${value} < ${schema.exclusiveMaximum})`;
|
|
152
|
-
if (schema.minimum
|
|
155
|
+
if (IsNumber(schema.minimum))
|
|
153
156
|
yield `(${value} >= ${schema.minimum})`;
|
|
154
|
-
if (schema.maximum
|
|
157
|
+
if (IsNumber(schema.maximum))
|
|
155
158
|
yield `(${value} <= ${schema.maximum})`;
|
|
156
159
|
}
|
|
157
160
|
function* Literal(schema, value) {
|
|
@@ -170,22 +173,22 @@ var TypeCompiler;
|
|
|
170
173
|
}
|
|
171
174
|
function* Number(schema, value) {
|
|
172
175
|
yield `(typeof ${value} === 'number')`;
|
|
173
|
-
if (schema.multipleOf
|
|
176
|
+
if (IsNumber(schema.multipleOf))
|
|
174
177
|
yield `(${value} % ${schema.multipleOf} === 0)`;
|
|
175
|
-
if (schema.exclusiveMinimum
|
|
178
|
+
if (IsNumber(schema.exclusiveMinimum))
|
|
176
179
|
yield `(${value} > ${schema.exclusiveMinimum})`;
|
|
177
|
-
if (schema.exclusiveMaximum
|
|
180
|
+
if (IsNumber(schema.exclusiveMaximum))
|
|
178
181
|
yield `(${value} < ${schema.exclusiveMaximum})`;
|
|
179
|
-
if (schema.minimum
|
|
182
|
+
if (IsNumber(schema.minimum))
|
|
180
183
|
yield `(${value} >= ${schema.minimum})`;
|
|
181
|
-
if (schema.maximum
|
|
184
|
+
if (IsNumber(schema.maximum))
|
|
182
185
|
yield `(${value} <= ${schema.maximum})`;
|
|
183
186
|
}
|
|
184
187
|
function* Object(schema, value) {
|
|
185
188
|
yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`;
|
|
186
|
-
if (schema.minProperties
|
|
189
|
+
if (IsNumber(schema.minProperties))
|
|
187
190
|
yield `(Object.keys(${value}).length >= ${schema.minProperties})`;
|
|
188
|
-
if (schema.maxProperties
|
|
191
|
+
if (IsNumber(schema.maxProperties))
|
|
189
192
|
yield `(Object.keys(${value}).length <= ${schema.maxProperties})`;
|
|
190
193
|
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
191
194
|
if (schema.additionalProperties === false) {
|
|
@@ -246,12 +249,10 @@ var TypeCompiler;
|
|
|
246
249
|
}
|
|
247
250
|
function* String(schema, value) {
|
|
248
251
|
yield `(typeof ${value} === 'string')`;
|
|
249
|
-
if (schema.minLength
|
|
252
|
+
if (IsNumber(schema.minLength))
|
|
250
253
|
yield `(${value}.length >= ${schema.minLength})`;
|
|
251
|
-
|
|
252
|
-
if (schema.maxLength !== undefined) {
|
|
254
|
+
if (IsNumber(schema.maxLength))
|
|
253
255
|
yield `(${value}.length <= ${schema.maxLength})`;
|
|
254
|
-
}
|
|
255
256
|
if (schema.pattern !== undefined) {
|
|
256
257
|
const local = PushLocal(`new RegExp(/${schema.pattern}/);`);
|
|
257
258
|
yield `(${local}.test(${value}))`;
|
|
@@ -279,9 +280,9 @@ var TypeCompiler;
|
|
|
279
280
|
}
|
|
280
281
|
function* Uint8Array(schema, value) {
|
|
281
282
|
yield `(${value} instanceof Uint8Array)`;
|
|
282
|
-
if (schema.maxByteLength)
|
|
283
|
+
if (IsNumber(schema.maxByteLength))
|
|
283
284
|
yield `(${value}.length <= ${schema.maxByteLength})`;
|
|
284
|
-
if (schema.minByteLength)
|
|
285
|
+
if (IsNumber(schema.minByteLength))
|
|
285
286
|
yield `(${value}.length >= ${schema.minByteLength})`;
|
|
286
287
|
}
|
|
287
288
|
function* Unknown(schema, value) {
|
package/package.json
CHANGED
package/value/check.js
CHANGED
|
@@ -39,6 +39,9 @@ class ValueCheckUnknownTypeError extends Error {
|
|
|
39
39
|
exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError;
|
|
40
40
|
var ValueCheck;
|
|
41
41
|
(function (ValueCheck) {
|
|
42
|
+
function IsNumber(value) {
|
|
43
|
+
return typeof value === 'number';
|
|
44
|
+
}
|
|
42
45
|
function Any(schema, references, value) {
|
|
43
46
|
return true;
|
|
44
47
|
}
|
|
@@ -46,10 +49,10 @@ var ValueCheck;
|
|
|
46
49
|
if (!globalThis.Array.isArray(value)) {
|
|
47
50
|
return false;
|
|
48
51
|
}
|
|
49
|
-
if (schema.minItems
|
|
52
|
+
if (IsNumber(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
50
53
|
return false;
|
|
51
54
|
}
|
|
52
|
-
if (schema.maxItems
|
|
55
|
+
if (IsNumber(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
53
56
|
return false;
|
|
54
57
|
}
|
|
55
58
|
if (schema.uniqueItems === true && !(new Set(value).size === value.length)) {
|
|
@@ -67,16 +70,16 @@ var ValueCheck;
|
|
|
67
70
|
if (!(value instanceof globalThis.Date)) {
|
|
68
71
|
return false;
|
|
69
72
|
}
|
|
70
|
-
if (schema.exclusiveMinimumTimestamp && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
73
|
+
if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
71
74
|
return false;
|
|
72
75
|
}
|
|
73
|
-
if (schema.exclusiveMaximumTimestamp && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
76
|
+
if (IsNumber(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
74
77
|
return false;
|
|
75
78
|
}
|
|
76
|
-
if (schema.minimumTimestamp && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
79
|
+
if (IsNumber(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
77
80
|
return false;
|
|
78
81
|
}
|
|
79
|
-
if (schema.maximumTimestamp && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
82
|
+
if (IsNumber(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
80
83
|
return false;
|
|
81
84
|
}
|
|
82
85
|
return true;
|
|
@@ -91,19 +94,19 @@ var ValueCheck;
|
|
|
91
94
|
if (!globalThis.Number.isInteger(value)) {
|
|
92
95
|
return false;
|
|
93
96
|
}
|
|
94
|
-
if (schema.multipleOf
|
|
97
|
+
if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
95
98
|
return false;
|
|
96
99
|
}
|
|
97
|
-
if (schema.exclusiveMinimum
|
|
100
|
+
if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
98
101
|
return false;
|
|
99
102
|
}
|
|
100
|
-
if (schema.exclusiveMaximum
|
|
103
|
+
if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
101
104
|
return false;
|
|
102
105
|
}
|
|
103
|
-
if (schema.minimum
|
|
106
|
+
if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
|
|
104
107
|
return false;
|
|
105
108
|
}
|
|
106
|
-
if (schema.maximum
|
|
109
|
+
if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
|
|
107
110
|
return false;
|
|
108
111
|
}
|
|
109
112
|
return true;
|
|
@@ -121,19 +124,19 @@ var ValueCheck;
|
|
|
121
124
|
if (!(typeof value === 'number')) {
|
|
122
125
|
return false;
|
|
123
126
|
}
|
|
124
|
-
if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
|
|
127
|
+
if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
125
128
|
return false;
|
|
126
129
|
}
|
|
127
|
-
if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
|
|
130
|
+
if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
128
131
|
return false;
|
|
129
132
|
}
|
|
130
|
-
if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
|
|
133
|
+
if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
131
134
|
return false;
|
|
132
135
|
}
|
|
133
|
-
if (schema.minimum && !(value >= schema.minimum)) {
|
|
136
|
+
if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
|
|
134
137
|
return false;
|
|
135
138
|
}
|
|
136
|
-
if (schema.maximum && !(value <= schema.maximum)) {
|
|
139
|
+
if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
|
|
137
140
|
return false;
|
|
138
141
|
}
|
|
139
142
|
return true;
|
|
@@ -142,10 +145,10 @@ var ValueCheck;
|
|
|
142
145
|
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
143
146
|
return false;
|
|
144
147
|
}
|
|
145
|
-
if (schema.minProperties
|
|
148
|
+
if (IsNumber(schema.minProperties) && !(globalThis.Object.keys(value).length >= schema.minProperties)) {
|
|
146
149
|
return false;
|
|
147
150
|
}
|
|
148
|
-
if (schema.maxProperties
|
|
151
|
+
if (IsNumber(schema.maxProperties) && !(globalThis.Object.keys(value).length <= schema.maxProperties)) {
|
|
149
152
|
return false;
|
|
150
153
|
}
|
|
151
154
|
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
@@ -223,11 +226,11 @@ var ValueCheck;
|
|
|
223
226
|
if (!(typeof value === 'string')) {
|
|
224
227
|
return false;
|
|
225
228
|
}
|
|
226
|
-
if (schema.minLength
|
|
229
|
+
if (IsNumber(schema.minLength)) {
|
|
227
230
|
if (!(value.length >= schema.minLength))
|
|
228
231
|
return false;
|
|
229
232
|
}
|
|
230
|
-
if (schema.maxLength
|
|
233
|
+
if (IsNumber(schema.maxLength)) {
|
|
231
234
|
if (!(value.length <= schema.maxLength))
|
|
232
235
|
return false;
|
|
233
236
|
}
|
|
@@ -273,10 +276,10 @@ var ValueCheck;
|
|
|
273
276
|
if (!(value instanceof globalThis.Uint8Array)) {
|
|
274
277
|
return false;
|
|
275
278
|
}
|
|
276
|
-
if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
|
|
279
|
+
if (IsNumber(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
277
280
|
return false;
|
|
278
281
|
}
|
|
279
|
-
if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
|
|
282
|
+
if (IsNumber(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
|
|
280
283
|
return false;
|
|
281
284
|
}
|
|
282
285
|
return true;
|