vuelidate: Add function call signature to ValidationRule (#39206)

* Add function definition to ValidationRule

ValidationRule is a function that takes in parameters and then applies them to the inner validator function. The types are currently missing that allow this behavior. This is most useful when building composed functions out of the built-in validators, like so:

```typescript
export const greaterThanZero: ValidationRule = and(minValue(0), not((value: UserInput) => value == 0))
export const greaterThanOrEqualToZero: ValidationRule = minValue(0)
```

Vuelidate "withParams" reference that returns a ValidationRule function: https://github.com/vuelidate/vuelidate/blob/bb23f01ab4f14a813a25cff8defb7788cd8859ac/src/params.js#L36-L55

* unknown -> any for TS < 3.0

* Fix unrelated noImplictAny for TypeScript@next
This commit is contained in:
Brian Bugh
2019-10-22 15:25:02 -07:00
committed by Wesley Wigham
parent aa11bb97dc
commit e4da6e8797
2 changed files with 4 additions and 1 deletions
+1
View File
@@ -14,6 +14,7 @@ export interface ValidationParams {
// const ValidationRule
export interface ValidationRule {
(...params: any[]): boolean
$params(): ValidationParams
$pending(): boolean
}
+3 -1
View File
@@ -19,9 +19,11 @@ const contains = (param: string): CustomRule =>
const mustBeCool3 = helpers.withParams(
{ type: 'mustBeCool3' },
(value) => !helpers.req(value) || value.indexOf('cool') >= 0
(value: any) => !helpers.req(value) || value.indexOf('cool') >= 0
)
const mustBeCool3Result: boolean = mustBeCool3(50)
const mustBeCool4 = helpers.regex('mustBeCool4', /^.*cool.*$/)
const mustBeSame = (reference: string) => helpers.withParams(