Skip to content

Commit 442c301

Browse files
committed
Correctly validate object
1 parent c5bf125 commit 442c301

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

src/compileValueSchema.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
213213

214214
nodes.push(...compileNullableCheck(compiler, schema, value));
215215

216+
nodes.push(
217+
builders.ifStatement(
218+
builders.logicalExpression('||',
219+
builders.binaryExpression(
220+
'!==',
221+
builders.unaryExpression('typeof', value),
222+
builders.literal('object'),
223+
),
224+
builders.binaryExpression(
225+
'===',
226+
value,
227+
builders.identifier('null'),
228+
)),
229+
builders.blockStatement([builders.returnStatement(error('Expected an object'))]),
230+
),
231+
);
232+
216233
// Define a variable to be all the keys in `value`
217234
const keysIdentifier = builders.identifier('keys');
218235

@@ -318,20 +335,18 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
318335
),
319336
];
320337

321-
if (schema.additionalProperties) {
322-
// Remove the key from the keys set
323-
check.push(
324-
builders.expressionStatement(
325-
builders.callExpression(
326-
builders.memberExpression(
327-
keysIdentifier,
328-
builders.identifier('delete'),
329-
),
330-
[propNameLiteral],
338+
// Remove the key from the keys set
339+
check.push(
340+
builders.expressionStatement(
341+
builders.callExpression(
342+
builders.memberExpression(
343+
keysIdentifier,
344+
builders.identifier('delete'),
331345
),
346+
[propNameLiteral],
332347
),
333-
);
334-
}
348+
),
349+
);
335350

336351
nodes.push(
337352
builders.ifStatement(
@@ -461,13 +476,10 @@ function compileNumberSchema(
461476
nodes.push(...compileNullableCheck(compiler, schema, value));
462477
nodes.push(
463478
builders.ifStatement(
464-
builders.unaryExpression(
465-
'!',
466-
builders.binaryExpression(
467-
'===',
468-
builders.unaryExpression('typeof', value),
469-
builders.literal('number'),
470-
),
479+
builders.binaryExpression(
480+
'!==',
481+
builders.unaryExpression('typeof', value),
482+
builders.literal('number'),
471483
),
472484
builders.blockStatement([builders.returnStatement(error('Expected a number'))]),
473485
),
@@ -490,13 +502,10 @@ function compileStringSchema(compiler: Compiler, schema: OpenAPIStringSchema) {
490502
nodes.push(...compileNullableCheck(compiler, schema, value));
491503
nodes.push(
492504
builders.ifStatement(
493-
builders.unaryExpression(
494-
'!',
495-
builders.binaryExpression(
496-
'===',
497-
builders.unaryExpression('typeof', value),
498-
builders.literal('string'),
499-
),
505+
builders.binaryExpression(
506+
'!==',
507+
builders.unaryExpression('typeof', value),
508+
builders.literal('string'),
500509
),
501510
builders.blockStatement([builders.returnStatement(error('Expected a string'))]),
502511
),
@@ -584,13 +593,10 @@ function compileBooleanSchema(compiler: Compiler, schema: OpenAPIBooleanSchema)
584593
nodes.push(...compileNullableCheck(compiler, schema, value));
585594
nodes.push(
586595
builders.ifStatement(
587-
builders.unaryExpression(
588-
'!',
589-
builders.binaryExpression(
590-
'===',
591-
builders.unaryExpression('typeof', value),
592-
builders.literal('boolean'),
593-
),
596+
builders.binaryExpression(
597+
'!==',
598+
builders.unaryExpression('typeof', value),
599+
builders.literal('boolean'),
594600
),
595601
builders.blockStatement([builders.returnStatement(error('Expected a boolean'))]),
596602
),

0 commit comments

Comments
 (0)