From ea37655aa1e25bbe820af6c3d192b1e1e5fb7dd2 Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Thu, 22 Apr 2021 13:17:59 +0200 Subject: [PATCH] Added conditional properties and required field assignment in uniforms-bridge-json-schema (closes #919). --- .../__tests__/JSONSchemaBridge.ts | 57 ++++++++++++++++++- .../src/JSONSchemaBridge.ts | 19 +++++-- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts index be6f3d55c..6daadf18e 100644 --- a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts @@ -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(); @@ -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); }); @@ -753,6 +806,8 @@ describe('JSONSchemaBridge', () => { 'passwordNumeric', 'recursive', 'arrayWithAllOf', + 'nonObjectAnyOf', + 'nonObjectAnyOfRequired', ]); }); diff --git a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts index d0a55281a..80eef19d4 100644 --- a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts @@ -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 });