[Vuejs]-How to use Vuelidate Ref in custom validator using Typescript

0๐Ÿ‘

โœ…

I solved this by creating my own ref function outside of Vuelidate:


const ref: (reference: string | ((vm: any) => any), vm: any) => any = (reference, vm) => {
  return typeof reference === "function" ? reference(vm) : vm[reference];
};

This is used inside a custom validation rule as such:

(value: Date, vm: any) => {
  const fieldValue = ref(field, vm);
  return isAfter(value, fieldValue);
}

and used in the form validators object as such:

isDateAfter: isDateAfter((vm) => vm.startDate)

Leave a comment