@ents24/be-sure
Version:
NPM module for terse, 'throwy' validation
81 lines (80 loc) • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCustomValidator = exports.setCustomValidators = exports.getValidator = void 0;
const maxSignedInt32 = 2147483647;
const isInt = (value) => typeof value == 'number' && Number.isInteger(value);
const isInt32 = (value) => isInt(value) && value <= maxSignedInt32;
const isPositiveInt32 = (value) => isInt32(value) && value > 0;
const isNonEmptyString = (value) => typeof value === 'string' && value.length > 0;
const isStringWithWordCharacters = (value) => typeof value === 'string' && !!value.match(/\w/);
const isObject = (value) => {
const isNotArray = Array.isArray(value) === false;
const isNotNull = value !== null;
return typeof value === 'object' && isNotArray && isNotNull;
};
/**
* Permissive level of email validation enforces the structure _@_._ (with a single "@"" symbol) only
* @param {String} value
* @returns Boolean true if `value` matches the permissive email regex
*/
const isEmailPermissive = (value) => {
if (!isNonEmptyString(value)) {
return false;
}
if (!value.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/i)) {
return false;
}
const [username] = value.split('@');
if ((username === null || username === void 0 ? void 0 : username.startsWith('.')) || (username === null || username === void 0 ? void 0 : username.endsWith('.'))) {
return false;
}
return true;
};
/**
* A set of validator functions accessible by key. Each returns Boolean true
* .. if the passed value is valid, false otherwise. Note that these
* .. defaults are opinionated: the "int+" validator also requires the value
* .. to fit within 32 bit range. Validators can be added or overridden to
* .. match an app's requirements
*/
const coreValidators = {
'array': (value) => Array.isArray(value),
'array-not-empty': (value) => Array.isArray(value) && value.length > 0,
'bool': (value) => typeof value === "boolean",
'email': isEmailPermissive,
'id': isPositiveInt32,
'int': isInt32,
'int+': isPositiveInt32,
'name': isStringWithWordCharacters,
'defined': (value) => typeof value !== 'undefined',
'object': (value) => isObject(value),
'object-not-empty': (value) => isObject(value) && Object.keys(value).length > 0,
'page': isPositiveInt32,
'slug': (value) => isNonEmptyString(value) && !!value.match(/^[a-z0-9\-]+$/),
'slug-mixed': (value) => isNonEmptyString(value) && !!value.match(/^[a-zA-Z0-9\-]+$/),
'string': (value) => typeof value === 'string',
'string-not-empty': isNonEmptyString,
};
function getValidator(key) {
return coreValidators[key];
}
exports.getValidator = getValidator;
let customValidators = {};
/**
*
* @param {string} key the key used to refer to the new validator
* @param {BeSureValidator} func the validator function
*/
function setCustomValidators(cvs) {
customValidators = cvs;
}
exports.setCustomValidators = setCustomValidators;
/**
*
* @param {string} key the key to seek in coreValidators and customValidators
* @returns {BeSureValidator} function if a match is found
*/
function getCustomValidator(key) {
return customValidators[key];
}
exports.getCustomValidator = getCustomValidator;