question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Examples/documentation for using vuelidate with TypeScript

See original GitHub issue

Hello!

First off, thank you to the Vuelidate creators for making this amazing little validation library. Second, thank you to @mrellipse for contributing type definitions in #47.

I have been trying to figure out how to use Vuelidate with TypeScript from the PR history, by perusing the PR code, and by just generally futzing around. I see that the changes should extend the Vue interface declaration with enough information to know that the validations field is an available option.

However, I can’t for the life of me figure out what I need to do to make this all play nicely. I tried using the @Component syntax from the tests (component.spec.ts) but get this error:

ERROR in […]\src\calculator\Calculator.vue.ts (96,5): error TS2345: Argument of type ‘{ mixins: any[]; name: string; template: string; validations: any; }’ is not assignable to parameter of type ‘VueClass’. Object literal may only specify known properties, and ‘mixins’ does not exist in type ‘VueClass’.

corresponding to this code (mostly borrowed from the test file, and embedded in a .vue single file component):

<script lang="ts">

import Vue = require('vue');
import Component from 'vue-class-component';
import { IVuelidate, ValidationRuleset, Vuelidate, validationMixin } from 'vuelidate';

interface IComplexProp {
    value1: string;
    value2?: number;
}

type SimpleProp = string;

interface IComponentData {
    simpleProp: SimpleProp;
    complexProp: IComplexProp;
    simpleCollection: Array<SimpleProp>;
    complexCollection: Array<IComplexProp>;
    validationGroup: string[];
};

function predicate(value: any): boolean {
    return value ? true : false;
}

function predicateFactory(param: number): (value: any) => boolean {
    return (value) => predicate(value);
}

let validations: ValidationRuleset<IComponentData> = {
    simpleProp: {
        predicate
    },
    complexProp: {
        value1: {
            predicate,
            funcGen: predicateFactory(1)
        },
        value2: {
            predicate,
            funcGen: predicateFactory(1)
        }
    },
    simpleCollection: {
        $each: {
            predicate
        }
    },
    complexCollection: {
        $each: {
            value1: {
                predicate,
                funcGen: predicateFactory(1)
            }
        }
    },
    validationGroup: ['simpleProp', 'complexProp.value2']
};

@Component({
    mixins: [validationMixin],
    name: 'RulesetTest',
    template: '<div></div>',
    validations: validations
})
class TestComponent extends Vue implements IVuelidate<IComponentData> {

    constructor() {
        super();
    }

    simpleProp: string = 'true';

    complexProp: IComplexProp = {
        value1: '',
        value2: 1
    }

    simpleCollection: SimpleProp[] = ['A', 'B', 'C'];

    complexCollection: IComplexProp[] = [
        { value1: 'A' },
        { value1: 'B' },
        { value1: 'C' }
    ];

    simpleMethod(): boolean {
        return this.$v.complexProp.$dirty;
    }

    $v: Vuelidate<IComponentData>
}

</script>

Is there a guide for how to use Vuelidate with Vue Single File Components written in TypeScript? I’ve been trying to piece it together from the documentation embedded with the .ts code, but as I’m new to TypeScript and ES6, this has been a bit of a challenge for me.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:44
  • Comments:72 (10 by maintainers)

github_iconTop GitHub Comments

25reactions
victorgarciaesgicommented, Jul 15, 2019

@jacek-jablonski With vue-property-decorator you can use the method registerHooks to do that

In your main.ts

import { Component } from 'vue-property-decorator'

Component.registerHooks(['validations'])

Then on your components

import { Component, Vue } from 'vue-property-decorator';

@Component
export default class Blank extends Vue {
    validations() {
       return {
        ...
       }
    }
  }
}
17reactions
ozguruysalcommented, Jan 2, 2018

Looking forward to official typescript support!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Examples/documentation for using vuelidate with TypeScript
Examples/documentation for using vuelidate with TypeScript.
Read more >
vuelidate with vue 2 typescript - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
Form Validation in Vue3 with TypeScript along with vuelidate ...
Compiles and hot-reloads for development · Compiles and minifies for production · Lints and fixes files · Customize configuration · Vuelidate ...
Read more >
How To Solve Input String Was Not In Correct Format In ...
Ensure prior validation is valid before executing the next <h4>Vuelidate: Conditional ... Examples/documentation for using vuelidate with TypeScript hot 82.
Read more >
Does Vuelidate support typescript in VueJs? - Stack Overflow
If yes, how to use? I am able to use it with JavaScript. javascript · typescript · vue.js · vuejs2 · vuelidate.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found