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

Add test for conditional to show/hide dependent values #95

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RuslanZavacky
Copy link

@RuslanZavacky RuslanZavacky commented Oct 31, 2024

Thank you for the library! I am experimenting with it, and noticed that example at https://json-schema-form.vercel.app/?path=/story/demos-combinations--conditional-fields doesn't work when this library is used independently.

conditional_json_schema

I'd expect when calling handleValidation, for the fields object to get isVisible updated correctly.

I understand that formValuesToJsonValues is used to remove fields that are in conflict with conditionals, but the result of that function is not the one you would use to render form. I might be missing something there.

I've added test that shows that field isVisible property is in unexpected state.


To render form elements, I rely on fields and isVisible. And I assume it is not the same logic that is used to build out demo, as it is doing something else to derive, if field is visible or not.

@@ -479,3 +479,140 @@ describe('Conditional with a minimum value check', () => {
expect(handleValidation({ salary: 1000, reason: 'reason_one' }).formErrors).toEqual(undefined);
});
});

describe('Conditional with fields that depend on the previous value', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @RuslanZavacky , thank you a lot for this bug report and tests! Someone in our team will look into it later in a couple of days max 👀

Copy link
Collaborator

@sandrina-p sandrina-p Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @RuslanZavacky! I had the time now to dig into this. Explaining it is tricky, so I pushed a new commit with the data flow explanation. Look for the 📌 emoji in the code!

4871d63. Update: I amended the commit, here's the new version: d7f80b9

Spoiler: I did not fix the bug but provided alternatives. Let me know if it makes sense.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @sandrina-p - yes, makes sense :) I did notice on my tests, when I switch radio option twice - it would correctly re-calculate, now I see way :) We don't use React, but Google Lit. Are you looking to figure out how to do re-calculation internally? If not, I might try to see if I can pin point where it happens and how to fix it.

Copy link
Collaborator

@sandrina-p sandrina-p Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we don't plan to look into it in the next few days (or weeks). If you could give it a try, we'd highly appreciate. :)

But be aware that this problem doesn't get fixed by just doing a re-calculation. In this case we only have 1 condition that depends on another condition. (a chain of 2 conditions), so a re-calculation works. But given a JSON Schema with a chain of 3, 4, 5+ conditions, it means we'd need to do the re-calculation 3,4,5+ times.

I think the best solution is fixing the JSON Schema condition to account for the 2 dependent fields (has_pet and age) as shown in the unit test I created. Does that make sense to you?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense :) Thank you!

@RuslanZavacky
Copy link
Author

@sandrina-p do you think it is worth to merge this test case, or to cancel this PR? I feel like trying to solve the underlying recomputation for validation will need a separate ticket/pr, as it looks like quite a lot of effort might be needed :)

@@ -479,3 +509,348 @@ describe('Conditional with a minimum value check', () => {
expect(handleValidation({ salary: 1000, reason: 'reason_one' }).formErrors).toEqual(undefined);
});
});

describe('Conditional with fields that depend on the previous value', () => {
it('Should hide dependent field if value does not satisfy conditional anymore', () => {
Copy link
Collaborator

@sandrina-p sandrina-p Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RuslanZavacky, answering your question here (to keep it in a thread)

@sandrina-p do you think it is worth to merge this test case, or to cancel this PR? I feel like trying to solve the underlying recomputation for validation will need a separate ticket/pr, as it looks like quite a lot of effort might be needed :)

Good question.

Internally we are fixing the demo to avoid "chained conditionals". So the new schema is like this. Should be merged in a few hours, max tomorrow.

  if: {
    properties: {
+      has_pet: {
+        const: 'yes',
+      },
      pet_age: {
        minimum: 5,
      },
    },
    required: ['pet_age'],
  },

In this PR, I'm not sure, the tests are failing so it cannot be merged. Would you mind cleaning the tests to have 1 test only, alongside code comments explaining the "redundant validation"? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants