From e4da6e8797899cf41ae2a1c3deb8fdf6ff75a772 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Tue, 22 Oct 2019 17:25:02 -0500 Subject: [PATCH] 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 --- types/vuelidate/lib/validators.d.ts | 1 + types/vuelidate/vuelidate-tests.ts | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/types/vuelidate/lib/validators.d.ts b/types/vuelidate/lib/validators.d.ts index f335c4da3b..3f8ea48797 100644 --- a/types/vuelidate/lib/validators.d.ts +++ b/types/vuelidate/lib/validators.d.ts @@ -14,6 +14,7 @@ export interface ValidationParams { // const ValidationRule export interface ValidationRule { + (...params: any[]): boolean $params(): ValidationParams $pending(): boolean } diff --git a/types/vuelidate/vuelidate-tests.ts b/types/vuelidate/vuelidate-tests.ts index 794c2473fe..556d816ae6 100644 --- a/types/vuelidate/vuelidate-tests.ts +++ b/types/vuelidate/vuelidate-tests.ts @@ -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(