validation
validation copied to clipboard
Proof of concept for using the keyof operator instead of a function parser
Calls to .ensure() allowed both a string and a function as argument. The function was never actually executed, but parsed to get the string property name. The same effect can be achieved by using the keyof operator. In this case, only strings can be provided that are a property of the given class.
Proof of concept, requiring the programmer to explicitly state the object class each time .ensure() is executed. This is because .ensure() is a static function, so the type cannot be determined yet.
Fixes #485.
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
TimVevida seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.
Note that this is a breaking change, the Aurelia Validation docs state:
The
ensure
method accepts one argument representing the property name. The argument can be a string or a simple property access expression. If you're using TypeScript you'll probably want to use a property access expression because you'll get the benefit of intellisense, refactoring and avoid using "magic strings" that can be a maintenance issue.
So the expression syntax is encouraged. This patch would change this and allow only string property names, which are then checked using the keyof
operator, so "benefit of intellisense and refactoring" is preserved. But nevertheless a breaking change.
@TimVevida awesome!
Would it be feasible to ship property: keyof TObject | PropertyAccessor<TObject, TValue>
first, then deprecate the prop accessor option in a future release? If possible it would be good to give folks a chance to incrementally migrate over to this nicer syntax.
That is a great idea.
cool- any chance you could amend the PR with that change?
I think using a string rather than a function for specifying the property is a great idea. However it should be noted that you loose a little bit of functionality: while the string value is checked by the transpiler against the list of properties on your object, the value is not updated if you use automatic refactoring to rename the property. When using a function, the rename can be automatically applied.
Perhaps the recommendation should be switched from function to string, but the function syntax not be deprecated?
EDIT: See also https://github.com/Microsoft/TypeScript/issues/11997