lib/mongoose-validator.js
function validate(options) {
  if (is.undef(options.validator)) {
    throw new Error('validator option undefined')
  }

  if (!is.function(options.validator) && !is.string(options.validator)) {
    throw new Error(
      `validator must be of type function or string, received ${typeof options.validator}`
    )
  }

  const validatorName = is.string(options.validator) ? options.validator : ''
  const validatorFn = getValidatorFn(options.validator)

  if (is.undef(validatorFn)) {
    throw new Error(
      `validator \`${validatorName}\` does not exist in validator.js or as a custom validator`
    )
  }

  const passIfEmpty = !!options.passIfEmpty
  const mongooseOpts = omit(
    options,
    'passIfEmpty',
    'message',
    'validator',
    'arguments'
  )
  const args = toArray(options.arguments)
  const messageStr = findFirstString(
    options.message,
    customErrorMessages[validatorName],
    defaultErrorMessages[validatorName],
    'Error'
  )
  const message = interpolateMessageWithArgs(messageStr, args)
  const validator = createValidator(validatorFn, args, passIfEmpty)

  return Object.assign(
    {
      validator,
      message,
    },
    mongooseOpts
  )
}
function validate(options) {
  if (is.undef(options.validator)) {
    throw new Error('validator option undefined')
  }

  if (!is.function(options.validator) && !is.string(options.validator)) {
    throw new Error(
      `validator must be of type function or string, received ${typeof options.validator}`
    )
  }

  const validatorName = is.string(options.validator) ? options.validator : ''
  const validatorFn = getValidatorFn(options.validator)

  if (is.undef(validatorFn)) {
    throw new Error(
      `validator \`${validatorName}\` does not exist in validator.js or as a custom validator`
    )
  }

  const passIfEmpty = !!options.passIfEmpty
  const mongooseOpts = omit(
    options,
    'passIfEmpty',
    'message',
    'validator',
    'arguments'
  )
  const args = toArray(options.arguments)
  const messageStr = findFirstString(
    options.message,
    customErrorMessages[validatorName],
    defaultErrorMessages[validatorName],
    'Error'
  )
  const message = interpolateMessageWithArgs(messageStr, args)
  const validator = createValidator(validatorFn, args, passIfEmpty)

  return Object.assign(
    {
      validator,
      message,
    },
    mongooseOpts
  )
}
function validate ( options ) { if ( is . undef ( options . validator ) ) { throw new Error ( 'validator option undefined' ) } if ( ! is . function ( options . validator ) && ! is . string ( options . validator ) ) { throw new Error ( ` ${ typeof options . validator } ` ) } const validatorName = is . string ( options . validator ) ? options . validator : '' const validatorFn = getValidatorFn ( options . validator ) if ( is . undef ( validatorFn ) ) { throw new Error ( ` \` ${ validatorName } \` ` ) } const passIfEmpty = ! ! options . passIfEmpty const mongooseOpts = omit ( options , 'passIfEmpty' , 'message' , 'validator' , 'arguments' ) const args = toArray ( options . arguments ) const messageStr = findFirstString ( options . message , customErrorMessages [ validatorName ] , defaultErrorMessages [ validatorName ] , 'Error' ) const message = interpolateMessageWithArgs ( messageStr , args ) const validator = createValidator ( validatorFn , args , passIfEmpty ) return Object . assign ( { validator , message , } , mongooseOpts ) }
Create a validator object

@alias module:mongoose-validator

@param {object} options Options object
@param {string} options.validator Validator name to use
@param {*} [options.arguments=[]] Arguments to pass to validator. If more than one argument is required an array must be used. Single arguments will internally be coerced into an array
@param {boolean} [options.passIfEmpty=false] Weather the validator should pass if the value being validated is empty
@param {string} [options.message=Error] Validator error message

@return {object} Returns validator compatible with mongoosejs

@throws If validator option property is not defined
@throws If validator option is not a function or string
@throws If validator option is a validator method (string) and method does not exist in validate.js or as a custom validator

@example
require('mongoose-validator').validate({ validator: 'isLength', arguments: [4, 40], passIfEmpty: true, message: 'Value should be between 4 and 40 characters' )
Create a validator object