@airplane/views
Version:
A React library for building Airplane views. Views components are optimized in style and functionality to produce internal apps that are easy to build and maintain.
41 lines (40 loc) • 910 B
JavaScript
const REQUIRED_ERROR = "This field is required";
const runValidate = (value, {
validate,
required
}) => {
const errors = [];
if (required) {
if (value == null || isEmptyValue(value)) {
errors.push(REQUIRED_ERROR);
}
}
if (validate != null && value != null && !isEmptyValue(value)) {
const validateFns = unpackValidateProp(validate);
for (const validateFn of validateFns) {
const error = validateFn(value);
if (error) {
errors.push(error);
}
}
}
return errors;
};
const unpackValidateProp = (validateFn) => {
if (Array.isArray(validateFn)) {
return validateFn;
} else {
return [validateFn];
}
};
const isEmptyValue = (value) => {
if (typeof value === "string" || Array.isArray(value)) {
return value.length === 0;
}
return false;
};
export {
REQUIRED_ERROR,
runValidate
};
//# sourceMappingURL=runValidation.js.map