@shopify/react-form-state
Version:
Manage React forms tersely and type-safely with no magic
136 lines (107 loc) • 3.22 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var predicates = require('@shopify/predicates');
var utilities = require('./utilities.js');
function validateNested(validatorDictionary) {
return (input, fields) => {
const errors = utilities.mapObject(input, (value, field) => {
const validate = validatorDictionary[field];
if (validate == null) {
return null;
}
if (typeof validate === 'function') {
return validate(value, fields);
}
if (!Array.isArray(validate)) {
return;
}
const errors = validate.map(validator => validator(value, fields)).filter(input => input != null);
if (errors.length === 0) {
return;
}
return errors;
});
const anyErrors = Object.keys(errors).map(key => errors[key]).some(value => value != null);
if (anyErrors) {
return errors;
}
};
}
function validateList(validatorDictionary) {
const validateItem = validateNested(validatorDictionary);
return (input, fields) => {
const errors = input.map(item => validateItem(item, fields));
if (errors.some(error => error != null)) {
return errors;
}
};
}
function validateWithFields(matcher, errorContent) {
return validate(matcher, errorContent);
}
function validate(matcher, errorContent) {
return (input, fields) => {
const 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.isEmpty(input)) {
return;
}
if (matches) {
return;
}
if (typeof errorContent === 'function') {
return errorContent(toString(input));
}
return errorContent;
};
}
function validateRequired(matcher, errorContent) {
return (input, fields) => {
const matches = matcher(input, fields);
if (matches) {
return;
}
if (typeof errorContent === 'function') {
return errorContent(toString(input));
}
return errorContent;
};
}
const validators = {
lengthMoreThan(length, errorContent) {
return validate(predicates.lengthMoreThan(length), errorContent);
},
lengthLessThan(length, errorContent) {
return validate(predicates.lengthLessThan(length), errorContent);
},
numericString(errorContent) {
return validate(predicates.isNumericString, errorContent);
},
positiveNumericString(errorContent) {
return validate(predicates.isPositiveNumericString, errorContent);
},
nonNumericString(errorContent) {
return validate(predicates.notNumericString, errorContent);
},
requiredString(errorContent) {
return validateRequired(predicates.notEmptyString, errorContent);
},
required(errorContent) {
return validateRequired(predicates.notEmpty, errorContent);
}
};
function toString(obj) {
if (obj == null) {
return '';
}
return obj.toString();
}
exports.default = validators;
exports.validate = validate;
exports.validateList = validateList;
exports.validateNested = validateNested;
exports.validateRequired = validateRequired;
exports.validateWithFields = validateWithFields;