all files / src/ add-validator.js

100% Statements 21/21
100% Branches 20/20
100% Functions 4/4
100% Lines 14/14
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22       13× 13×   13×          
import messages from './messages'
import Validators from './index'
import { toObjectMsg, formatMsg, prepare, memoize } from './helpers'
 
 
export default function addValidator ({ validator, defaultMessage, defaultMsg }) {
  defaultMsg = toObjectMsg(defaultMsg || defaultMessage) || messages.invalid
 
  return memoize(function (options={}) {
    let msg = toObjectMsg(options.msg || options.message) || defaultMsg
 
    return prepare(options.if, options.unless, options.allowBlank, function (value, allValues) {
      let result = validator(options, value, allValues)
      if ('boolean' !== typeof result) {
        return result
      }
      if (!result) {
        return Validators.formatMessage(msg)
      }
    })
  })
}