@shopify/react-form-state
Version:
Manage react forms tersely and type-safe with no magic.
112 lines (111 loc) • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var predicates_1 = require("@shopify/predicates");
var utilities_1 = require("./utilities");
function validateNested(validatorDictionary) {
return function (input, fields) {
var errors = utilities_1.mapObject(input, function (value, field) {
var validate = validatorDictionary[field];
if (validate == null) {
return null;
}
if (typeof validate === 'function') {
return validate(value, fields);
}
if (!Array.isArray(validate)) {
return;
}
var errors = validate
.map(function (validator) { return validator(value, fields); })
.filter(function (input) { return input != null; });
if (errors.length === 0) {
return;
}
return errors;
});
var anyErrors = Object.keys(errors)
.map(function (key) { return errors[key]; })
.some(function (value) { return value != null; });
if (anyErrors) {
return errors;
}
};
}
exports.validateNested = validateNested;
function validateList(validatorDictionary) {
var validateItem = validateNested(validatorDictionary);
return function (input, fields) {
var errors = input.map(function (item) { return validateItem(item, fields); });
if (errors.some(function (error) { return error != null; })) {
return errors;
}
};
}
exports.validateList = validateList;
function validateWithFields(matcher, errorContent) {
return validate(matcher, errorContent);
}
exports.validateWithFields = validateWithFields;
function validate(matcher, errorContent) {
return function (input, fields) {
var matches = matcher(input, fields);
/*
always mark empty fields valid to match Polaris guidelines
https://polaris.shopify.com/patterns/error-messages#section-form-validation
*/
if (predicates_1.isEmpty(input)) {
return;
}
if (matches) {
return;
}
if (typeof errorContent === 'function') {
return errorContent(toString(input));
}
return errorContent;
};
}
exports.validate = validate;
function validateRequired(matcher, errorContent) {
return function (input, fields) {
var matches = matcher(input, fields);
if (matches) {
return;
}
if (typeof errorContent === 'function') {
return errorContent(toString(input));
}
return errorContent;
};
}
exports.validateRequired = validateRequired;
var validators = {
lengthMoreThan: function (length, errorContent) {
return validate(predicates_1.lengthMoreThan(length), errorContent);
},
lengthLessThan: function (length, errorContent) {
return validate(predicates_1.lengthLessThan(length), errorContent);
},
numericString: function (errorContent) {
return validate(predicates_1.isNumericString, errorContent);
},
positiveNumericString: function (errorContent) {
return validate(predicates_1.isPositiveNumericString, errorContent);
},
nonNumericString: function (errorContent) {
return validate(predicates_1.notNumericString, errorContent);
},
requiredString: function (errorContent) {
return validateRequired(predicates_1.notEmptyString, errorContent);
},
required: function (errorContent) {
return validateRequired(predicates_1.notEmpty, errorContent);
},
};
function toString(obj) {
if (obj == null) {
return '';
}
return obj.toString();
}
exports.default = validators;