react-awesome-query-builder
react-awesome-query-builder copied to clipboard
How to fire error if dropdown are not filled (validate tree)
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
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.getTreeBadFieldsreturn an empty list - using
Utils.TreeUtils.isEmptyTreecan't work, because I want to allow empty tree, but not incomplete one
In this case, the function Utils.ValidateTree() remove the rule
@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!
@ukrbublik Any answer on this issue ? This would really help me!
@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)
- What format of validation errors do you expect/suggest to return from
validateTree? ValidateTree() always clean the tree, even if everything is filled (maybe a bug ?)- can you give more details, please? Video, or sample app?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,ValidateTreeshould remove that rule) What version do you use?
@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
- 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
@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
In this case, If i use the workaround, the array warns is empty, not normal, because the rule isn't complete
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)
Oh, now I see, you're right. In the last example, it should indicate that the rule is incomplete
group: 0, rule: 4
Do you need just indexes? Like first group, 5th rule? Or maybe rule/group id also would be helpful?
I'll prioritize this issue
Related #781
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!
@ukrbublik Do you have any update on this issue ? thanks a lot