DavyJonesLocker/client_side_validations

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

Summary

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

const isInList = (value, otherValues) => {
  const normalizedOtherValues = []

  for (const otherValueIndex in otherValues) {
    normalizedOtherValues.push(otherValues[otherValueIndex].toString())
  }

  return arrayHasValue(value, normalizedOtherValues)
}

const isInRange = (value, range) => {
  return value >= range[0] && value <= range[1]
}

const isIncluded = (value, options, allowBlank) => {
  if ((options.allow_blank && !isValuePresent(value)) === allowBlank) {
    return true
  }

  return (options.in && isInList(value, options.in)) || (options.range && isInRange(value, options.range))
}

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

  if (isIncluded(value, options, false) || (!options.allow_blank && !isValuePresent(value))) {
    return options.message
  }
}

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

  if (!isIncluded(value, options, true)) {
    return options.message
  }
}

export default {
  exclusionLocalValidator,
  inclusionLocalValidator
}