Skip to content

Commit

Permalink
Added conditional properties and required field assignment in uniform…
Browse files Browse the repository at this point in the history
…s-bridge-json-schema (closes #919).
  • Loading branch information
Monteth authored Apr 22, 2021
1 parent d5e4505 commit ea37655
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,40 @@ describe('JSONSchemaBridge', () => {
maxItems: 3,
minItems: 1,
},
nonObjectAnyOf: {
anyOf: [
{
const: 'alphabetic',
type: 'string',
},
{
enum: ['top', 'middle', 'bottom'],
type: 'string',
},
{
type: 'number',
minimum: 0,
},
],
},
nonObjectAnyOfRequired: {
anyOf: [
{
const: 'alphabetic',
type: 'string',
},
{
enum: ['top', 'middle', 'bottom'],
type: 'string',
},
{
type: 'number',
minimum: 0,
},
],
},
},
required: ['dateOfBirth'],
required: ['dateOfBirth', 'nonObjectAnyOfRequired'],
};

const validator = jest.fn();
Expand Down Expand Up @@ -712,6 +744,27 @@ describe('JSONSchemaBridge', () => {
});
});

it('works with anyOf for a non-object computed property (required default value)', () => {
expect(bridge.getProps('nonObjectAnyOf')).toHaveProperty(
'required',
false,
);
});

it('works with anyOf for a non-object computed property (required)', () => {
expect(bridge.getProps('nonObjectAnyOfRequired')).toHaveProperty(
'required',
true,
);
});

it('works with anyOf for a non-object computed property (properties not defined)', () => {
expect(bridge.getProps('nonObjectAnyOf')).toHaveProperty(
'properties',
undefined,
);
});

it('works with maxItems in props', () => {
expect(bridge.getProps('arrayWithAllOf')).toHaveProperty('maxCount', 3);
});
Expand Down Expand Up @@ -753,6 +806,8 @@ describe('JSONSchemaBridge', () => {
'passwordNumeric',
'recursive',
'arrayWithAllOf',
'nonObjectAnyOf',
'nonObjectAnyOfRequired',
]);
});

Expand Down
19 changes: 15 additions & 4 deletions packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,31 @@ export default class JSONSchemaBridge extends Bridge {
.filter(Boolean);

if (combinedPartials.length) {
_definition.properties = definition.properties ?? {};
_definition.required = definition.required ?? [];
const localProperties = definition.properties
? { ...definition.properties }
: {};
const localRequired = definition.required
? definition.required.slice()
: [];

combinedPartials.forEach(({ properties, required, type }) => {
if (properties) {
Object.assign(_definition.properties, properties);
Object.assign(localProperties, properties);
}
if (required) {
_definition.required.push(...required);
localRequired.push(...required);
}
if (type && !_definition.type) {
_definition.type = type;
}
});

if (Object.keys(localProperties).length > 0) {
_definition.properties = localProperties;
}
if (localRequired.length > 0) {
_definition.required = localRequired;
}
}

this._compiledSchema[_key] = Object.assign(_definition, { isRequired });
Expand Down

0 comments on commit ea37655

Please sign in to comment.