all files / src/ add-validator.js

100% Statements 17/17
100% Branches 18/18
100% Functions 4/4
100% Lines 11/11
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18       12× 12×   12×        
import React from 'react'
import { formatMessage, prepare, memoize } from './helpers'
import { INVALID_MSG } from './format'
 
 
export default function addValidator ({ validator, defaultMessage, defaultMsg }) {
  defaultMsg = formatMessage(defaultMsg || defaultMessage) || INVALID_MSG
 
  return memoize(function (options={}) {
    let msg = formatMessage(options.msg || options.message) || defaultMsg
 
    return prepare(options.if, options.unless, options.allowBlank, function (value, allValues) {
      if (!validator(options, value, allValues)) {
        return msg
      }
    })
  })
}