braintree-web
Version:
A suite of tools for integrating Braintree in the browser
45 lines (37 loc) • 1.3 kB
JavaScript
;
var BraintreeError = require("../../lib/braintree-error");
var errors = require("../shared/errors");
var allowedAttributes = require("../shared/constants").allowedAttributes;
function attributeValidationError(attribute, value) {
var err;
if (!allowedAttributes.hasOwnProperty(attribute)) {
err = new BraintreeError({
type: errors.HOSTED_FIELDS_ATTRIBUTE_NOT_SUPPORTED.type,
code: errors.HOSTED_FIELDS_ATTRIBUTE_NOT_SUPPORTED.code,
message:
'The "' + attribute + '" attribute is not supported in Hosted Fields.',
});
} else if (value != null && !_isValid(attribute, value)) {
err = new BraintreeError({
type: errors.HOSTED_FIELDS_ATTRIBUTE_VALUE_NOT_ALLOWED.type,
code: errors.HOSTED_FIELDS_ATTRIBUTE_VALUE_NOT_ALLOWED.code,
message:
'Value "' +
value +
'" is not allowed for "' +
attribute +
'" attribute.',
});
}
return err;
}
function _isValid(attribute, value) {
if (allowedAttributes[attribute] === "string") {
return typeof value === "string" || typeof value === "number";
}
if (allowedAttributes[attribute] === "boolean") {
return String(value) === "true" || String(value) === "false";
}
return false;
}
module.exports = attributeValidationError;