From 49b1dd0d1b5d049aac043326aa2b1553dda98163 Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Wed, 21 Apr 2021 12:56:12 +0200 Subject: [PATCH 1/7] Added conditional properties and required field assignment in uniforms-bridge-json-schema. --- .../src/JSONSchemaBridge.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts index d0a55281a..184ba0a42 100644 --- a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts @@ -194,20 +194,27 @@ export default class JSONSchemaBridge extends Bridge { .filter(Boolean); if (combinedPartials.length) { - _definition.properties = definition.properties ?? {}; - _definition.required = definition.required ?? []; + const localProperties = definition.properties ?? {}; + const localRequired = definition.required ?? []; 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 }); From 5636c0ac1e717a73ab525987d33943baff42d273 Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Wed, 21 Apr 2021 16:35:38 +0200 Subject: [PATCH 2/7] Added nonObjectAnyOf test in uniforms-bridge-json-schema. --- .../__tests__/JSONSchemaBridge.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts index be6f3d55c..94a1431dd 100644 --- a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts @@ -135,6 +135,22 @@ describe('JSONSchemaBridge', () => { maxItems: 3, minItems: 1, }, + nonObjectAnyOf: { + anyOf: [ + { + const: 'alphabetic', + type: 'string', + }, + { + enum: ['top', 'middle', 'bottom'], + type: 'string', + }, + { + type: 'number', + minimum: 0, + }, + ], + }, }, required: ['dateOfBirth'], }; @@ -712,6 +728,13 @@ describe('JSONSchemaBridge', () => { }); }); + it('works with anyOf for a non-object computed property', () => { + expect(bridge.getProps('nonObjectAnyOf')).toHaveProperty( + 'required', + false, + ); + }); + it('works with maxItems in props', () => { expect(bridge.getProps('arrayWithAllOf')).toHaveProperty('maxCount', 3); }); @@ -753,6 +776,7 @@ describe('JSONSchemaBridge', () => { 'passwordNumeric', 'recursive', 'arrayWithAllOf', + 'nonObjectAnyOf', ]); }); From cde88192aa1ee33aeda3cc03281add58300bb80c Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Wed, 21 Apr 2021 17:02:01 +0200 Subject: [PATCH 3/7] Added nonObjectAnyOf test in uniforms-bridge-json-schema. --- .../__tests__/JSONSchemaBridge.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts index 94a1431dd..b4381209a 100644 --- a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts @@ -728,13 +728,20 @@ describe('JSONSchemaBridge', () => { }); }); - it('works with anyOf for a non-object computed property', () => { + 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 (properties not defined)', () => { + expect(bridge.getProps('nonObjectAnyOf')).toHaveProperty( + 'properties', + undefined, + ); + }); + it('works with maxItems in props', () => { expect(bridge.getProps('arrayWithAllOf')).toHaveProperty('maxCount', 3); }); From 943501f13da894f181fe0c2f9d44600e2e254990 Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Thu, 22 Apr 2021 10:55:26 +0200 Subject: [PATCH 4/7] Update packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Radosław Miernik --- packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts index 184ba0a42..d9f9037ca 100644 --- a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts @@ -194,8 +194,8 @@ export default class JSONSchemaBridge extends Bridge { .filter(Boolean); if (combinedPartials.length) { - const localProperties = definition.properties ?? {}; - const localRequired = definition.required ?? []; + const localProperties = definition.properties ? { ...definition.properties } : {}; + const localRequired = definition.required ? definition.required.slice() : []; combinedPartials.forEach(({ properties, required, type }) => { if (properties) { From 2c082274d5054aefdcbf725156a5b549034fa689 Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Thu, 22 Apr 2021 11:27:44 +0200 Subject: [PATCH 5/7] Fixed definition properties clonning in uniforms-bridge-json-schema. --- .../src/JSONSchemaBridge.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts index d9f9037ca..bdcdeab1d 100644 --- a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts @@ -194,8 +194,14 @@ export default class JSONSchemaBridge extends Bridge { .filter(Boolean); if (combinedPartials.length) { - const localProperties = definition.properties ? { ...definition.properties } : {}; - const localRequired = definition.required ? definition.required.slice() : []; + const localProperties = definition.properties + ? { ...definition.properties } + : {}; + const localRequired = definition.required + ? Array.isArray(definition.required) + ? definition.required.slice() + : definition.required + : []; combinedPartials.forEach(({ properties, required, type }) => { if (properties) { From f5c2b2d60607738980a768fd3fd81220f703cf2f Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Thu, 22 Apr 2021 11:29:25 +0200 Subject: [PATCH 6/7] Added nonObjectAnyOfRequired test in uniforms-bridge-json-schema. --- .../__tests__/JSONSchemaBridge.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts index b4381209a..a3a821e78 100644 --- a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts @@ -151,6 +151,23 @@ describe('JSONSchemaBridge', () => { }, ], }, + nonObjectAnyOfRequired: { + anyOf: [ + { + const: 'alphabetic', + type: 'string', + }, + { + enum: ['top', 'middle', 'bottom'], + type: 'string', + }, + { + type: 'number', + minimum: 0, + }, + ], + required: true, + }, }, required: ['dateOfBirth'], }; @@ -735,6 +752,13 @@ describe('JSONSchemaBridge', () => { ); }); + 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', @@ -784,6 +808,7 @@ describe('JSONSchemaBridge', () => { 'recursive', 'arrayWithAllOf', 'nonObjectAnyOf', + 'nonObjectAnyOfRequired', ]); }); From c2a9f407ccc24594f0ced4cc441f0157c88a7004 Mon Sep 17 00:00:00 2001 From: Konrad Bosak Date: Thu, 22 Apr 2021 13:05:41 +0200 Subject: [PATCH 7/7] Fixed 'required' prop in test and reverted its consequences in uniforms-bridge-json-schema. --- .../uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts | 3 +-- packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts index a3a821e78..6daadf18e 100644 --- a/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts @@ -166,10 +166,9 @@ describe('JSONSchemaBridge', () => { minimum: 0, }, ], - required: true, }, }, - required: ['dateOfBirth'], + required: ['dateOfBirth', 'nonObjectAnyOfRequired'], }; const validator = jest.fn(); diff --git a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts index bdcdeab1d..80eef19d4 100644 --- a/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts +++ b/packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts @@ -198,9 +198,7 @@ export default class JSONSchemaBridge extends Bridge { ? { ...definition.properties } : {}; const localRequired = definition.required - ? Array.isArray(definition.required) - ? definition.required.slice() - : definition.required + ? definition.required.slice() : []; combinedPartials.forEach(({ properties, required, type }) => {