George Alvis
George Alvis

Reputation: 151

Extend vuelidate with custom validators

I am trying to extend Vuelidate object in my Vue app. Currently I have following setup in my Vue Component.

import {required} from "vuelidate/lib/validators";

export default {
   validations: {
    ...
  }
}

I have created a new file custom-validator.js

import { helpers } from "vuelidate/lib/validators";
export const OnlyDgitis = helpers.regex('onlyDigits', /^\d+$/);

I want to import OnlyDgitis along with predefined vuelidate validators from custom-validator.js

End Result

import {required, OnlyDgitis} from 'custom-validators.js'

What I have tried

import { helpers } from "vuelidate/lib/validators";
   import * as Validators from 'vuelidate/lib/validators'
   
   export const OnlyDgitis = helpers.regex('onlyDigits', /^\d+$/);
   export default Object.assign({}, Validators, {
     OnlyDgitis       
   });

Upvotes: 2

Views: 1830

Answers (1)

George Alvis
George Alvis

Reputation: 151

Following @EstusFlask suggestions from comments Here's how finally got the desired results

// custom-validators.js

import { helpers } from "vuelidate/lib/validators";

export const OnlyDgitis = helpers.regex('onlyDigits', /^\d+$/);

export * from 'vuelidate/lib/validators'

Now I can easily import like so,

// My component.js
import {required, OnlyDgitis} from 'custom-validators.js'

export default {
   validations: {
    ...
  }
}

Upvotes: 2

Related Questions