diff --git a/.changeset/short-bats-tell.md b/.changeset/short-bats-tell.md new file mode 100644 index 0000000..a344eb3 --- /dev/null +++ b/.changeset/short-bats-tell.md @@ -0,0 +1,5 @@ +--- +"@croz/nrich-form-configuration-core": patch +--- + +An enhancement has been made to the schema merger function to address its previous limitations when using "yup" required() validator function. diff --git a/libs/form-configuration/core/src/converter/FormConfigurationValidationConverter.ts b/libs/form-configuration/core/src/converter/FormConfigurationValidationConverter.ts index c73c90a..853e6b9 100644 --- a/libs/form-configuration/core/src/converter/FormConfigurationValidationConverter.ts +++ b/libs/form-configuration/core/src/converter/FormConfigurationValidationConverter.ts @@ -116,6 +116,7 @@ export class FormConfigurationValidationConverter { if (obj1[key].type === "object" && obj2[key].type === "object") { merged[key] = this.mergeSchemas(obj1[key], obj2[key]); merged[key].spec = _mergeWith(obj1[key].spec, obj2[key].spec, (field1, field2) => (typeof field1 === "boolean" ? field1 && field2 : field1 ?? field2)); + merged[key].internalTests = { ...obj1[key].internalTests, ...obj2[key].internalTests }; } else if (obj1[key].type === "array" && obj2[key].type === "array") { if (obj1[key].innerType.type === "object" && obj2[key].innerType.type === "object") { diff --git a/libs/form-configuration/core/test/converter/FormConfigurationValidationConverter.test.ts b/libs/form-configuration/core/test/converter/FormConfigurationValidationConverter.test.ts index 6d8d890..a5f0a7e 100644 --- a/libs/form-configuration/core/test/converter/FormConfigurationValidationConverter.test.ts +++ b/libs/form-configuration/core/test/converter/FormConfigurationValidationConverter.test.ts @@ -271,3 +271,18 @@ it.each([ // then expect(mergedSchema.describe()).toEqual(expectedResult.expectedResult.describe()); }); + +it("merge schemas method should merge internalTests from both schema objects", () => { + // given + const converter = new FormConfigurationValidationConverter(); + const schema1 = yup.object().shape({ obj1: yup.object().required() }); + const schema2 = yup.object().shape({ obj1: yup.object() }); + + // when + const result = converter.mergeSchemas(schema1, schema2); + + // then + expect(result.isValidSync({ obj1: undefined })).toBe(false); + expect(result.isValidSync({ obj1: null })).toBe(false); + expect(result.isValidSync({ obj1: {} })).toBe(true); +});