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

How to fire error if dropdown are not filled (validate tree) #977

Closed
FlorianRuen opened this issue Sep 5, 2023 · 10 comments · Fixed by #1034
Closed

How to fire error if dropdown are not filled (validate tree) #977

FlorianRuen opened this issue Sep 5, 2023 · 10 comments · Fixed by #1034
Milestone

Comments

@FlorianRuen
Copy link

FlorianRuen commented Sep 5, 2023

I'm using this library to generate a json logic that will be analyzed using golang library, but I got some concerns on validation

  • The validator seems to validate only values in fields
  • But If the value source if empty or field selector is empty, no validation fire an error

Capture d’écran du 2023-09-05 15-00-46
Capture d’écran du 2023-09-05 15-00-40
Capture d’écran du 2023-09-05 15-04-47

In all these cases, I want to fire an error message, because rules are incomplete.

What I tried:

  • using Utils.ValidateTree() always clean the tree, even if everything is filled (maybe a bug ?) and compare the tree before the validate function and after. If there is a difference, we can fire an error message
  • using Utils.TreeUtils.getTreeBadFields return an empty list
  • using Utils.TreeUtils.isEmptyTree can't work, because I want to allow empty tree, but not incomplete one

In this case, the function Utils.ValidateTree() remove the rule

Capture d’écran du 2023-09-05 15-03-23

@ukrbublik
There is a way to detect incomplete rules in a Tree, and fire an error if there is at least one ?

Thanks for your help!

@FlorianRuen
Copy link
Author

@ukrbublik Any answer on this issue ? This would really help me!

@ukrbublik
Copy link
Owner

@FlorianRuen Thanks for the feature request!

validateTree by default "sanitizes" the tree by removing incomplete rules.

You can trigger tree validation that will not remove incomplete rules, but validate rules and log warnings in the console. You can use a workaround to catch errors from console:

import { Utils } from "@react-awesome-query-builder/ui";
const { validateTree, ConfigUtils: {extendConfig} } = Utils;
const extendedConfig = extendConfig(config);
const origConsoleWarn = console.warn;
const warns: string[] = [];
console.warn = (...args) => warns.push(args.join(' '));
validateTree(tree, tree, extendedConfig, extendedConfig, false, false);
console.warn = origConsoleWarn;
// use warns

I should improve this function to return errors rather than logging. (and not forcing a developer to call extendConfig manually)

  1. What format of validation errors do you expect/suggest to return from validateTree?
  2. ValidateTree() always clean the tree, even if everything is filled (maybe a bug ?) - can you give more details, please? Video, or sample app?
  3. But If the value source if empty or field selector is empty, no validation fire an error - also should not be true. (Value source can't be empty, it should have default value. If field is empty, ValidateTree should remove that rule) What version do you use?

@FlorianRuen
Copy link
Author

@ukrbublik Thanks for your answer, I was working on a function to do this, but it wasn't optimized, so this workaround should be better

  1. If you can return something like { isError: true, group: 0, rule: 4, msg: 'empty' } or something similaire should be good enought. This will help us to display a message with the group / rule in error, and in the message (optional maybe), the error type (empty, wrong value ...)

For others dots, I will test using this workaround, maybe I create a strange behavior, I will test again, and add a new comment based on the tests

@FlorianRuen
Copy link
Author

@ukrbublik I test your workaround, but I think, I got the same error that i mentionned with But If the value source if empty or field selector is empty, no validation fire an error

Capture d’écran du 2023-10-26 11-12-07

In this case, If i use the workaround, the array warns is empty, not normal, because the rule isn't complete

Capture d’écran du 2023-10-26 11-12-20

Same for this rule, without the value, the array warns is also empty

I think validateTree works only if we define some validator in config, but if we don't (or can't) define some validator functions, the validateTree will not work anymore (for example, in the first image, I can't define a validator function on source)

@ukrbublik
Copy link
Owner

Oh, now I see, you're right.
In the last example, it should indicate that the rule is incomplete

@ukrbublik
Copy link
Owner

group: 0, rule: 4

Do you need just indexes? Like first group, 5th rule?
Or maybe rule/group id also would be helpful?

@ukrbublik
Copy link
Owner

I'll prioritize this issue

@ukrbublik ukrbublik added this to the Bugs milestone Oct 26, 2023
@ukrbublik
Copy link
Owner

Related #781

@FlorianRuen
Copy link
Author

group: 0, rule: 4

Do you need just indexes? Like first group, 5th rule? Or maybe rule/group id also would be helpful?

You can also add the rule/group id, it can help in some case. For me, it's more user fridently using group/rule number to fix the problem.

Ideally, all of the things that need to be validated:

  • All lists must be populated (with a value other than the default)
  • Values after operators are required
  • If a function is chosen, the parameters are all mandatory (in the case below, if I choose Sum, I must fill in both fields)
  • No empty rule (a click on add, without filling the rule should return an error)
  • All while taking into account the possibility of nesting rules

This function will be very useful when integrating into a form!

@FlorianRuen
Copy link
Author

@ukrbublik Do you have any update on this issue ? thanks a lot

@ukrbublik ukrbublik linked a pull request Mar 13, 2024 that will close this issue
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 a pull request may close this issue.

2 participants