Skip to content

Commit

Permalink
Add internalTests schema object to merge schemas method
Browse files Browse the repository at this point in the history
  • Loading branch information
vojnovicluka committed Apr 11, 2024
1 parent c3cd27b commit 6ce2e3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-bats-tell.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 6ce2e3b

Please sign in to comment.