Could you change your documentation on writing Custom Validators so that it's more of a step-by-step, instead of breaking down how built-in validators are created? I think it would help myself and other end-users better understand how to make custom validators using Vuelidate.
Thanks!
For more clarity into my lack of understanding, my validations object:
{
prefix: {
required
},
first_name: {
required,
minLength: minLength(2)
},
last_name: {
required,
minLength: minLength(2)
},
email: {
required,
email
},
dob: {
required,
dob18
}
}
At the top of that component's definition, I import my own custom validator: import dob18 from '@/modules/validation/dob18'.
dob18.js looks like this:
import Moment from 'moment'
export default dob => Moment().diff(dob, 'years') >= 18
When validating, though, I'm presented with this error: TypeError: Cannot read property 'type' of null. Not sure why. Use of withParams versus not doesn't seem clear, so maybe I'm needing withParams, but even so, how to use that function is unclear.
Thanks for your help!
Figured this bit out, too. dob18.js now looks like:
import { withParams } from 'vuelidate/lib'
import Moment from 'moment'
export default withParams({ type: 'dob18' }, dob => Moment().diff(dob, 'years') >= 18)
For those who wonder how to use other built in validators in your custom validator:
import {minLength, maxLength, alphaNum} from 'vuelidate/lib/validators'
import {withParams} from 'vuelidate/lib'
const customValidator = withParams({type: 'customValidator'}, value => {
return minLength(5)(value) && maxLength(10)(value) && alphaNum(value)
})
export {customValidator}
Documentaion is still vague and it requires digging into code and issues tab to get better understanding of custom validators. This issue shouldn't be closed.
Most helpful comment
Documentaion is still vague and it requires digging into code and issues tab to get better understanding of custom validators. This issue shouldn't be closed.