@alexkeating/common-utilities
Version:
Our **Common Utilities** package is a set of helper tools and utilities that are used throughout our libraries and apps. This includes things like our constants, types that are commonly shared across packages, and various utilities and helper functions.
38 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleBaseUnits = exports.ignoreEmpty = exports.ignoreEmptyVal = exports.ValidateField = exports.ValErrMsgs = void 0;
const typeguards_1 = require("./typeguards");
const units_1 = require("./units");
exports.ValErrMsgs = {
number: 'Field must be a number',
boolean: 'Field must be a boolean',
array: 'Field must be an array',
ethAddress: 'Field must be an Ethereum address',
url: 'Field must be a valid URL',
email: 'Field must be a valid email',
percent: 'Field must be a valid percentage',
};
exports.ValidateField = {
number: (val) => ((0, typeguards_1.isNumberish)(val) ? true : exports.ValErrMsgs.number),
boolean: (val) => ((0, typeguards_1.isBoolean)(val) ? true : exports.ValErrMsgs.boolean),
array: (val) => ((0, typeguards_1.isArray)(val) ? true : exports.ValErrMsgs.array),
ethAddress: (val) => (0, typeguards_1.isEthAddress)(val) ? true : exports.ValErrMsgs.ethAddress,
url: (val) => (0, typeguards_1.isString)(val) &&
/[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/.test(val)
? true
: exports.ValErrMsgs.url,
email: (val) => (0, typeguards_1.isString)(val) && /^[^@]+@[^@]+\.[^@]+$/.test(val) ? true : exports.ValErrMsgs.email,
percent: (val, range = [0, 100]) => (0, typeguards_1.isNumberish)(val) && Number(val) >= range[0] && Number(val) <= range[1]
? true
: exports.ValErrMsgs.percent,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ignoreEmptyVal = (val, validator) => val === '' ? true : validator(val);
exports.ignoreEmptyVal = ignoreEmptyVal;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ignoreEmpty = (val, validator) => val === '' ? true : validator(val);
exports.ignoreEmpty = ignoreEmpty;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleBaseUnits = (val, decimals = 18) => exports.ValidateField.number(val) === true ? (0, units_1.toBaseUnits)(val, decimals) : val;
exports.handleBaseUnits = handleBaseUnits;
//# sourceMappingURL=validation.js.map