-
-
Notifications
You must be signed in to change notification settings - Fork 128
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 alternative to fluent rule API that enables creating rules from data #363
Comments
here's a workaround- let me know if it helps: const ruleDefinitions = [
{
propertyName: 'firstName',
displayName: 'First Name',
rules: [
{ name: 'required', args: [] },
{ name: 'minLength', args: [3] },
{ name: 'maxLength', args: [30] },
]
},
{
propertyName: 'lastName',
displayName: 'Last Name',
rules: [
{ name: 'required', args: [] },
{ name: 'minLength', args: [3] },
{ name: 'maxLength', args: [30] },
]
},
];
let ensureAPI: { ensure: (propertyName: string) => FluentRules<any, any> } = ValidationRules;
for (let ruleDefinition of ruleDefinitions) {
let ruleAPI: { satisfiesRule: (name: string, ...args: any[]) => FluentRuleCustomizer<any, any> };
ruleAPI = ensureAPI.ensure(ruleDefinition.propertyName)
.displayName(ruleDefinition.displayName);
for (let rule of ruleDefinition.rules) {
ensureAPI = ruleAPI = ruleAPI.satisfiesRule(rule.name, ...rule.args);
}
}
let rules = (<FluentEnsure<any>>ensureAPI).rules; |
Sorry, for the delay in responding....I haven't forgotten about this... |
no worries- let me know if it helps at all |
@jdanyow I've played around with your suggestion, above. I particularly wanted to see if my code could be any better using named rules. It is basically a minor improvement over the code I posted at the top (it mainly gets rid of the It is an interesting exercise, but I prefer the way I proposed. |
I had the same problem with trying to dynamically construct validation rules using the fluent API. I ended up using a 'void' rule to make sure the first call already resulted in a FluentRuleCustomizer instead of a FluentRule.
An interface with the overlap between the FluentRule and FluentRuleCustomizer would be nice to have. |
I am also interested in a better way to create Validation Rules from schema data. |
@dkent600 what's the status of this- I think you had done some prototyping but I lost track with the move, etc. |
My proposal is here: master...dkent600:Incrementally-add-rules-for-properties-#400 It introduces a |
Regarding the proposed code enhancement I referenced just above ^^^^ , what follows is an example of what the code enables you to do that accomplishes the task of the original use case given at the top of this issue. I will also note here that this change also, as an effortless side-effect, addresses #400, though I note that #400 was recently addressed separately. That code could be discarded. Here is an example of how one can currently accomplish the task at hand:
Here is how I am able to do it with the proposed code changes:
|
Use Case
Looping over a set of model properties to apply validation rules to each.
Problem
Currently, three different object classes are used in this scenario. This adds complexity to coding in TypeScript. Consider the following code:
Request
IFluentRules
(or whatever you want to name it) that includes all of the methods used here, or that might be used, includingensure
,displayName
,minLength
,required
, etc.......ValidationRules
, use a new factory method to create an empty FluentRules that implementsIFluentRules
.IFluentRules
to return anIFluentRules
The resulting code would look like this:
The text was updated successfully, but these errors were encountered: