Custom error message on custom route validator


#1

I’ve written a custom validator used on a route and wanted to know how I can pass in a custom error message? My validator reads as follows

const duplicateUserSegmentFn = async (data, field, message, args, get) => {

    const value = get(data, field)

    if ( !value ) {

        /**
         * skip validation if value is not defined. `required` rule
         * should take care of it.
        */
        return
    }

    var zendesk = new Zendesk( Env.get('ZD_API'), Env.get('ZD_AUTH_HEADER') )
    let response = await zendesk.listUserSegments()
    for( i = 0; i < response.user_segments.length; i++ ) {

        if( response.user_segments[i].name == value ) {

            throw message
        }
    }
}

and I call the validator using

get rules () {

    return {
		'name':'required|string|duplicateUserSegment:name'
    }
}

Any advice is greatly appreciated


#2

Define it as messages property.

get messages () {
  return {
     
  }
}