DavyJonesLocker/client_side_validations

View on GitHub
src/validators/local/format.js

Summary

Maintainability
A
0 mins
Test Coverage
import { isValuePresent } from '../../utils'

const isMatching = (value, regExpOptions) => {
  return new RegExp(regExpOptions.source, regExpOptions.options).test(value)
}

const hasValidFormat = (value, withOptions, withoutOptions) => {
  return (withOptions && isMatching(value, withOptions)) || (withoutOptions && !isMatching(value, withoutOptions))
}

export const formatLocalValidator = ($element, options) => {
  const element = $element[0]
  const value = element.value

  if (options.allow_blank && !isValuePresent(value)) {
    return
  }

  if (!hasValidFormat(value, options.with, options.without)) {
    return options.message
  }
}

export default {
  formatLocalValidator
}