From 1a8e6368f6165ba14733ebc272188d6a7d192b03 Mon Sep 17 00:00:00 2001 From: "jan@pidgin" Date: Tue, 20 Nov 2018 21:41:55 +0100 Subject: [PATCH] Fixing dynamic validation schema and typing. --- types/vuelidate/vue.d.ts | 15 ++++----- types/vuelidate/vuelidate-tests.ts | 53 +++++++++++++++--------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/types/vuelidate/vue.d.ts b/types/vuelidate/vue.d.ts index d1ef01e7bc..8232d96038 100644 --- a/types/vuelidate/vue.d.ts +++ b/types/vuelidate/vue.d.ts @@ -5,20 +5,18 @@ import { ValidationRule } from './lib/validators' import { Validation } from './vuelidate' declare module 'vue/types/vue' { - type ValidationProperties = { - [P in keyof V]?: Validation + type ValidationProperties = { + [P in keyof V]?: Validation & ValidationProperties } - interface ValidationGroups { - [groupName: string]: ValidationProperties + interface ValidationGroups { + [groupName: string]: Validation & ValidationProperties } interface Vue { - $v: ValidationProperties & ValidationGroups & Validation + $v: ValidationProperties & ValidationGroups & Validation delayTouch(v: Validation): void - - validations(): any // unknown } } @@ -35,11 +33,12 @@ declare module 'vue/types/options' { type GroupDecl = string[] type AsyncDecl = (...args: any[]) => boolean | Promise type NestedDecl = RuleDecl + type DynamicDecl = () => RuleDecl interface RuleDecl { [rule: string]: ValidationDecl | GroupDecl | AsyncDecl | NestedDecl } interface ComponentOptions { - validations?: RuleDecl + validations?: RuleDecl | DynamicDecl } } diff --git a/types/vuelidate/vuelidate-tests.ts b/types/vuelidate/vuelidate-tests.ts index 20e320b3a7..43cf5f5648 100644 --- a/types/vuelidate/vuelidate-tests.ts +++ b/types/vuelidate/vuelidate-tests.ts @@ -116,19 +116,23 @@ export class ValidComponent extends Vue { myField = '' touchMap = new WeakMap() - delayTouch($v: Validation) { - $v.$reset() - if (this.touchMap.has($v)) { - clearTimeout(this.touchMap.get($v)) + delayTouch(v: Validation) { + v.$reset() + if (this.touchMap.has(v)) { + clearTimeout(this.touchMap.get(v)) } - this.touchMap.set($v, setTimeout($v.$touch, 1000)) + this.touchMap.set(v, setTimeout(v.$touch, 1000)) } accessValidatorParams() { - console.log(this.$v.$params.username) - if (this.$v.username) { - console.log(this.$v.username.$params) + console.log(this.$v.$params.form) + + if (this.$v.form) { + console.log(this.$v.form.$params) + console.log(this.$v.form.$params.nestedA) + console.log(this.$v.form.nestedA) } + console.log(this.$v.validationGroup.$params) } accessGroups() { @@ -140,25 +144,22 @@ export class ValidComponent extends Vue { } hasDescription = false - validations() { - if (!this.hasDescription) { - return { - name: { - required - } - } - } else { - return { - name: { - required - }, - description: { - required - } - } - } - } + get isRepoValid() { return !this.$v.$invalid } } + +@Component({ + validations() { + const self = this as Valid2Component // assert + return self.myToggle + ? { myField1: required, myField2: contains('maybe') } + : { myField1: contains('maybe'), myField2: required } + } +}) +export class Valid2Component extends Vue { + myToggle = false + myField1 = '' + myField2 = '' +}