Skip to content

Commit

Permalink
toJsonSchema: Only disallow additionalProperties for strictObject
Browse files Browse the repository at this point in the history
`additionalProperties: true` was preventing validation of valid
intersected objects.

Fixes fabian-hiller#990.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Dec 27, 2024
1 parent 882f5f1 commit b136f40
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
9 changes: 0 additions & 9 deletions packages/to-json-schema/src/convertSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ describe('convertSchema', () => {
key4: { anyOf: [{ type: 'number' }, { type: 'null' }] },
},
required: ['key1', 'key3'],
additionalProperties: false,
});
});

Expand Down Expand Up @@ -669,7 +668,6 @@ describe('convertSchema', () => {
foo: { type: 'string' },
},
required: ['type', 'foo'],
additionalProperties: false,
},
{
type: 'object',
Expand All @@ -678,7 +676,6 @@ describe('convertSchema', () => {
bar: { type: 'number' },
},
required: ['type', 'bar'],
additionalProperties: false,
},
],
});
Expand All @@ -703,15 +700,13 @@ describe('convertSchema', () => {
foo: { type: 'string' },
},
required: ['foo'],
additionalProperties: false,
},
{
type: 'object',
properties: {
bar: { type: 'number' },
},
required: ['bar'],
additionalProperties: false,
},
],
});
Expand Down Expand Up @@ -772,15 +767,13 @@ describe('convertSchema', () => {
type: 'object',
properties: { node: { $ref: '#/$defs/1' } },
required: [],
additionalProperties: false,
});
expect(context).toStrictEqual({
definitions: {
'1': {
type: 'object',
properties: { node: { $ref: '#/$defs/1' } },
required: [],
additionalProperties: false,
},
},
referenceMap: new Map().set(nodeSchema, '1'),
Expand All @@ -807,7 +800,6 @@ describe('convertSchema', () => {
type: 'object',
properties: { node: { $ref: '#/$defs/2' } },
required: ['node'],
additionalProperties: false,
});
expect(context).toStrictEqual({
definitions: {
Expand All @@ -817,7 +809,6 @@ describe('convertSchema', () => {
type: 'object',
properties: { node: { $ref: '#/$defs/2' } },
required: ['node'],
additionalProperties: false,
},
{ type: 'null' },
],
Expand Down
7 changes: 5 additions & 2 deletions packages/to-json-schema/src/convertSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,11 @@ export function convertSchema(
config,
context
);
} else {
jsonSchema.additionalProperties = valibotSchema.type === 'loose_object';
} else if (valibotSchema.type === 'loose_object') {
// `true` has no validation effect, but might as well be explicit
jsonSchema.additionalProperties = true;
} else if (valibotSchema.type === 'strict_object') {
jsonSchema.additionalProperties = false;
}

break;
Expand Down
3 changes: 0 additions & 3 deletions packages/to-json-schema/src/toJsonSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('toJsonSchema', () => {
age: { type: 'number' },
},
required: ['name', 'email'],
additionalProperties: false,
description: 'foo',
},
},
Expand All @@ -65,7 +64,6 @@ describe('toJsonSchema', () => {
$ref: '#/$defs/ul',
$defs: {
ul: {
additionalProperties: false,
properties: {
children: {
items: { $ref: '#/$defs/li' },
Expand All @@ -77,7 +75,6 @@ describe('toJsonSchema', () => {
type: 'object',
},
li: {
additionalProperties: false,
properties: {
children: {
items: {
Expand Down

0 comments on commit b136f40

Please sign in to comment.