All files / lib/validations index.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                      1x         1x   1x 16x   1x 16x     1x                                                                     1x            
/**
 * @name Validations
 * @description This module handles needed validations
 * @module validations
 **/
 
import {
  __,
  ifElse,
  keys,
  map,
  mapObjIndexed,
  pick,
  pipe,
  prop,
} from 'ramda'
 
import validations from './validate'
 
const pickExistentValidations = pipe(
  keys,
  pick
)
 
const validation = prop(__, validations)
 
const mapIfArray = func =>
  ifElse(Array.isArray, map(func), func)
 
const validateInput = (value, name) =>
  mapIfArray(validation(name))(value)
 
 
const applyValidations = mapObjIndexed(validateInput)
 
/**
 * This method validates the properties supplied in the object.
 *
 * @param {Object} [body] An object that contains all properties to
 *                        be validated.
 * @param {(String|String[]|Number|Number[])} [body.cnpj] A CNPF, or an array
 *                                                        of CNPFs, to be
 *                                                        validated.
 * @param {(String|String[]|Number|Number[])} [body.cpf] A CPF, or an array of
 *                                                       CPFs, to be
 *                                                       validated.
 * @param {(String|String[]|Number|Number[])} [body.ddd] A DDD, or an array of
 *                                                       DDDs, to be validated.
 * @param {(String|String[]|Number|Number[])} [body.zipcode] A zipcode, or an
 *                                                           array of zipcodes,
 *                                                           to be validated.
 * @param {(String|String[]|Number|Number[])} [body.phone] A phone number, or
 *                                                         an array of phones,
 *                                                         to be validated.
 *
 *
 * @param {Object|Object[]} [body.card] A card, or an array of cards, to be
 *                                      validated.
 * @param {String} [body.card.card_holder_name] The card's holder name.
 * @param {(String|Number)} [body.card.card_number] The card's number.
 * @param {(String|Number)} [body.card.card_cvv] The card's CVV.
 * @param {(String|Number)} [body.card.card_expiration_date] The card's
 *                                                           expiratation date.
 *
 * @returns {Object} An object that returns each of the supplied properties
 *                   with true or false, indicating if the supplied value is valid
 *                   or invalid.
 **/
const validate = pipe(
  pickExistentValidations(validations),
  applyValidations
)
 
export default validate