-
Notifications
You must be signed in to change notification settings - Fork 3
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
Argument parsing issue on different functions. #70
Comments
@ozziest I initially removed the spread on #67 would fix it in 2.x without introducing a breaking change. At least I didn't found a way how I could differentiate between const isRuleValid = await rule.callback(
check.value,
- ...[...rule.params, context]
+ ['in', 'notIn', 'anotherRule'].includes(rule.name)
+ ? [...rule.params, context]
+ : { ...[...rule.params, context] }
); Sidenote: This wouldn't solve callback-syntax with custom-registered functions, unless you add another option to the |
@christoph-kluge great feedback. I've implemented a solution like this: https://github.com/axe-api/validator/pull/71/files Basically, every rule function should have the following parameter structure: export default (value: any, p1: any, p2: any, extra?: IContext): boolean => {
} The first parameter should be the value that will be tested. The last parameter might be (doesn't have to be) The other parameters between the I'm open to suggestions. ☺ |
For v2 either #67 or #71 would be totally fine. At least #71 would cover the context, which was missing in #67 👍 For v3 I'd personally prefer a clean common-callback interface. As an example: // isBetween.ts
export default (value: any, options: any, extra: IContext): boolean => {
const [min, max] = ...options
return value >= min && value <= max // simplified example
}
// isIn.ts
export default (value: any, options: any, extra: IContext): boolean => {
return options.includes(value) // simplified example
} Which would change the callback to const isRuleValid = await rule.callback(
check.value,
rule.params,
context
); This way each rule could decide on it's own how they interpret the given options. Context would be always given and doesn't require any additional parsing, because it's anyway the last parameter. |
Another idea could be simple rule-helper function for v2? Giving custom rules an easy way to utilize it? // isIn.tsx
export default (value: any, ...args: any[]): boolean => {
const {options, context} = parse(args)
return options.includes(value)
} What do you think? |
@christoph-kluge , both ideas are great. Thanks for the contribution. ☺ I've checked all the current rules: https://validator.axe-api.com/rules.html Usually, there are only one or two parameters in the rules. The exceptions are export default (value: any, params: any[], context: IContext): boolean => {
// check
} It's similar to your suggestions. ☺ We can push every parameter we can parse to the |
Story
This issue is related with this PR: #67
The problem is, some rules functions expect parameters one by one, but others expect an array. For example;
That doesn't make sense. Parsing parameters can't cover both scenarios.
The text was updated successfully, but these errors were encountered: