Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added conditional properties and required field assignment in uniforms-bridge-json-schema (closing #919). #936

Merged
merged 7 commits into from
Apr 22, 2021
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