Skip to content

Commit 5fe7637

Browse files
committed
Fix validation of object with additional properties
1 parent d988e87 commit 5fe7637

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

src/compileValueSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
372372
});
373373

374374
// No additional properties are allowed
375-
if (!schema.additionalProperties && schema.properties) {
375+
if (schema.additionalProperties === false) {
376376
nodes.push(
377377
builders.ifStatement(
378378
builders.binaryExpression(
@@ -381,7 +381,7 @@ function compileObjectSchema(compiler: Compiler, schema: OpenAPIObjectSchema) {
381381
builders.literal(0),
382382
),
383383
builders.blockStatement([
384-
builders.returnStatement(error(`unexpected properties`)),
384+
builders.returnStatement(error(`unexpected additional properties`)),
385385
]),
386386
),
387387
);

src/tests/__snapshots__/compileValueSchema.test.ts.snap

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,6 @@ function obj0(path, value, context) {
298298
value['bar'] = result1;
299299
keys.delete('bar');
300300
}
301-
if (keys.size > 0) {
302-
return new ValidationError(path, 'unexpected properties');
303-
}
304301
return value;
305302
}"
306303
`;
@@ -358,9 +355,6 @@ function obj0(path, value, context) {
358355
} else {
359356
value['foo'] = 10;
360357
}
361-
if (keys.size > 0) {
362-
return new ValidationError(path, 'unexpected properties');
363-
}
364358
return value;
365359
}"
366360
`;
@@ -851,9 +845,6 @@ function obj1(path, value, context) {
851845
value['a'] = result0;
852846
keys.delete('a');
853847
}
854-
if (keys.size > 0) {
855-
return new ValidationError(path, 'unexpected properties');
856-
}
857848
return value;
858849
}
859850
function obj4(path, value, context) {
@@ -879,9 +870,6 @@ function obj3(path, value, context) {
879870
value['b'] = result0;
880871
keys.delete('b');
881872
}
882-
if (keys.size > 0) {
883-
return new ValidationError(path, 'unexpected properties');
884-
}
885873
return value;
886874
}
887875
function obj0(path, value, context) {

src/tests/__snapshots__/compiler.test.ts.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ function obj1(path, value, context) {
6969
value['bar'] = result1;
7070
keys.delete('bar');
7171
}
72-
if (keys.size > 0) {
73-
return new ValidationError(path, 'unexpected properties');
74-
}
7572
return value;
7673
}"
7774
`;
@@ -157,9 +154,6 @@ function obj1(path, value, context) {
157154
value['rec'] = result2;
158155
keys.delete('rec');
159156
}
160-
if (keys.size > 0) {
161-
return new ValidationError(path, 'unexpected properties');
162-
}
163157
return value;
164158
}"
165159
`;

tests/gitbook.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,41 @@ test('POST orgs/appleId/custom-fields', () => {
6565
query: {},
6666
body: undefined,
6767
});
68-
console.log(result);
6968
expect(result).toMatchObject({
7069
params: {
7170
organizationId: 'appleId',
7271
},
7372
});
7473
});
74+
75+
test('PUT orgs/apple/schemas/newType', () => {
76+
const result = validateRequest({
77+
path: '/orgs/apple/schemas/newType',
78+
method: 'put',
79+
headers: {
80+
'content-type': 'application/json',
81+
},
82+
query: {},
83+
body: {
84+
type: 'newType',
85+
title: {
86+
singular: 'New type',
87+
plural: 'New types',
88+
},
89+
properties: [
90+
{
91+
name: 'title',
92+
type: 'text',
93+
title: 'Title',
94+
},
95+
],
96+
},
97+
});
98+
expect(result).toMatchObject({
99+
operationId: 'setEntitySchema',
100+
params: {
101+
organizationId: 'apple',
102+
entityType: 'newType',
103+
},
104+
});
105+
});

0 commit comments

Comments
 (0)