/**
* @module flitter-forms/Messages
*/
/**
* This is a function that, when provided the name of the validation criterion, the name of the field, and, if
* applicable, the requested value of the field, will return a human readable error message. If the provided
* validation criterion doesn't have an associated error message, the function will return an Error.
*
* @param {string} criterion - name of the validation criterion
* @param {string} field - name of the field being validated
* @param {string} [requested_value] - value requested by the validation criterion
* @returns {string|Error}
*/
module.exports = exports = function(criterion, field, requested_value = "") {
/*
* Yes this is ugly, but it is a dictionary for converting
* validator criteria to human-readable error messages.
*/
const messages = {
// PROVIDED BY validator
'contains' : `${field} must contain ${requested_value}.`,
'equals' : `${field} must be equal to ${requested_value}.`,
'isAfter' : `${field} must occur after ${requested_value}.`,
'isAlpha' : `${field} must contain only alphabetical characters.`,
'isAlphanumeric' : `${field} must contain only alpha-numeric character.`,
'isAscii' : `${field} must contain only ASCII characters.`,
'isBase64' : `${field} must be Base64 encoded.`,
'isBefore' : `${field} must occur before ${requested_value}`,
'isBoolean' : `${field} must be either true or false.`,
'isByteLength' : `${field} must be ${requested_value} bytes.`,
'isCreditCard' : `${field} must be a valid credit card.`,
'isCurrency' : `${field} must be a valid currency amount.`,
'isDataURI' : `${field} must be a valid data URI.`,
'isMagnetURI' : `${field} must be a valid magnet URI.`,
'isDecimal' : `${field} must be a decimal number ${requested_value}.`,
'isDivisibleBy' : `${field} must be divisible by ${requested_value}.`,
'isEmail' : `${field} must be a valid email address.`,
'isEmpty' : `${field} must be empty.`,
'isFQDN' : `${field} must be a fully-qualified domain name.`,
'isFloat' : `${field} must be a floating point number ${requested_value}.`,
'isFullWidth' : `${field} must contain a full-width char.`,
'isHalfWidth' : `${field} must contain a half-width char.`,
'isHash' : `${field} must be hashed data.`,
'isHexColor' : `${field} must be a hexadecimal color code.`,
'isHexadecimal' : `${field} must be a hexadecimal number.`,
'isIdentityCard' : `${field} must be a valid identity card code.`,
'isIP' : `${field} must be a valid IPv4 or IPv6 address.`,
'isIPRange' : `${field} must be an IPv4 range.`,
'isISBN' : `${field} must be an International Standard Book Number.`,
'isISSN' : `${field} must be an International Standard Serial Number.`,
'isISIN' : `${field} must be an International Securities Identification Number.`,
'isISO8601' : `${field} must be an ISO 8061-formatted date (yyyy-mm-dd).`,
'isRFC3339' : `${field} must be an RFC 3339-formatted date (yyyy-mm-ddThh:mmssZ).`,
'isISO31661Alpha2' : `${field} must be a valid ISO 3166-1 alpha-2 country code (e.g. US).`,
'isISO31661Alpha3' : `${field} must be a valid ISO 3166-1 alpha-3 country code (e.g. GRC).`,
'isISRC' : `${field} must be an International Standard Recording Code.`,
'isIn' : `${field} must be one of: ${requested_value}`,
'isInt' : `${field} must be an integer ${requested_value}.`,
'isJSON' : `${field} must be valid JSON.`,
'isJWT' : `${field} must be a JSON Web Token.`,
'isLatLong' : `${field} must be a latitude-longitude coordinate (e.g. lat,long).`,
'isLength' : `${field} must be length ${requested_value}.`,
'isLowercase' : `${field} must be lowercase.`,
'isMACAddress' : `${field} must be a MAC address.`,
'isMD5' : `${field} must be a MD5 hash.`,
'isMimeType' : `${field} must be a MIME-type.`,
'isMobilePhone' : `${field} must be a mobile phone number.`,
'isMongoId' : `${field} must be a hex-encoded MongoDB ObjectId.`,
'isMultibyte' : `${field} must contain at least one multibyte character.`,
'isNumeric' : `${field} must contain only numbers.`,
'isPort' : `${field} must be a valid port number (1-65535).`,
'isPostalCode' : `${field} must be a valid postal code (GB).`,
'isSurrogatePair' : `${field} must contain a surrogate pair character.`,
'isURL' : `${field} must be a valid URL.`,
'isUUID' : `${field} must be a valid UUID.`,
'isUppercase' : `${field} must contain only uppercase characters.`,
'isVariableWidth' : `${field} must contain full- or half-width characters.`,
'isWhitelisted' : `${field} must be in ${requested_value}`,
'matches' : `${field} does not match the required form (${requested_value}).`,
// CUSTOM CRITERIA
'required' : `${field} is required.`,
'verify' : `${field} verification does not match, or is missing.`,
}
/*
* If there isn't an error message for the provided critereon,
* return an error instance.
*/
if ( !(criterion in messages) ){
return new Error('Requested criterion message does not exist: '+criterion)
}
/*
* Otherwise, return the message.
*/
return messages[criterion]
}