@nodeswork/sbase
Version:
Basic REST api foundation from Nodeswork.
534 lines (532 loc) • 18.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("underscore");
const moment = require("moment-timezone");
const validator = require("validator");
const object_path_1 = require("object-path");
const params_1 = require("./params");
const required = (_target, _path, val) => val != null;
exports.required = required;
function contains(seed) {
const contains = (_target, _path, val) => val == null || validator.contains(val, seed);
return contains;
}
exports.contains = contains;
function equals(comparison) {
const equals = (_target, _path, val) => val == null || validator.equals(val, comparison);
return equals;
}
exports.equals = equals;
function isAfter(date) {
const isAfter = (_target, _path, val) => val == null || validator.isAfter(val, date);
return isAfter;
}
exports.isAfter = isAfter;
function isAlpha(locale) {
const isAlpha = (_target, _path, val) => val == null || validator.isAlpha(val, locale);
return isAlpha;
}
exports.isAlpha = isAlpha;
function isAlphanumeric(locale) {
const isAlphanumeric = (_target, _path, val) => val == null || validator.isAlphanumeric(val, locale);
return isAlphanumeric;
}
exports.isAlphanumeric = isAlphanumeric;
const isAscii = (_target, _path, val) => val == null || validator.isAscii(val);
exports.isAscii = isAscii;
const isBase64 = (_target, _path, val) => val == null || validator.isBase64(val);
exports.isBase64 = isBase64;
function isBefore(date) {
const isBefore = (_target, _path, val) => val == null || validator.isBefore(val, date);
return isBefore;
}
exports.isBefore = isBefore;
const isBoolean = (_target, _path, val) => val == null || validator.isBoolean(val);
exports.isBoolean = isBoolean;
function isByteLength(a, b) {
const isByteLength = (_target, _path, val) => val == null || validator.isByteLength(val, a, b);
return isByteLength;
}
exports.isByteLength = isByteLength;
const isCreditCard = (_target, _path, val) => validator.isCreditCard(val);
exports.isCreditCard = isCreditCard;
function isCurrency(options) {
const isCurrency = (_target, _path, val) => val == null || validator.isCurrency(val, options);
return isCurrency;
}
exports.isCurrency = isCurrency;
const isDataURI = (_target, _path, val) => val == null || validator.isDataURI(val);
exports.isDataURI = isDataURI;
function isDecimal(options) {
const isDecimal = (_target, _path, val) => val == null || validator.isDecimal(val, options);
return isDecimal;
}
exports.isDecimal = isDecimal;
function isDivisibleBy(number) {
const isDivisibleBy = (_target, _path, val) => val == null || validator.isDivisibleBy(val, number);
return isDivisibleBy;
}
exports.isDivisibleBy = isDivisibleBy;
function isEmail(options) {
const isEmail = (_target, _path, val) => val == null || validator.isEmail(val, options);
return isEmail;
}
exports.isEmail = isEmail;
const isEmpty = (_target, _path, val) => val == null || validator.isEmpty(val);
exports.isEmpty = isEmpty;
function isEnum(enumObject, from = 'value') {
const values = from === 'value' ? _.values(enumObject) : _.keys(enumObject);
const isEnum = (_target, _path, val) => val == null || _.indexOf(values, val) >= 0;
return isEnum;
}
exports.isEnum = isEnum;
function isFQDN(options) {
const isFQDN = (_target, _path, val) => val == null || validator.isFQDN(val, options);
return isFQDN;
}
exports.isFQDN = isFQDN;
function isFloat(options) {
const isFloat = (_target, _path, val) => val == null || validator.isFloat(val, options);
return isFloat;
}
exports.isFloat = isFloat;
const isFullWidth = (_target, _path, val) => val == null || validator.isFullWidth(val);
exports.isFullWidth = isFullWidth;
const isHalfWidth = (_target, _path, val) => val == null || validator.isHalfWidth(val);
exports.isHalfWidth = isHalfWidth;
function isHash(algorithm) {
const isHash = (_target, _path, val) => val == null || validator.isHash(val, algorithm);
return isHash;
}
exports.isHash = isHash;
const isHexColor = (_target, _path, val) => val == null || validator.isHexColor(val);
exports.isHexColor = isHexColor;
const isHexadecimal = (_target, _path, val) => val == null || validator.isHexadecimal(val);
exports.isHexadecimal = isHexadecimal;
function isIP(version) {
const isIP = (_target, _path, val) => val == null || validator.isIP(val, version);
return isIP;
}
exports.isIP = isIP;
function isISSN(options) {
const isISSN = (_target, _path, val) => val == null || validator.isISSN(val, options);
return isISSN;
}
exports.isISSN = isISSN;
const isISIN = (_target, _path, val) => val == null || validator.isISIN(val);
exports.isISIN = isISIN;
const isISO8601 = (_target, _path, val) => val == null || validator.isISO8601(val);
exports.isISO8601 = isISO8601;
const isISO31661Alpha2 = (_target, _path, val) => val == null || validator.isISO31661Alpha2(val);
exports.isISO31661Alpha2 = isISO31661Alpha2;
const isISRC = (_target, _path, val) => val == null || validator.isISRC(val);
exports.isISRC = isISRC;
function isIn(values) {
const isIn = (_target, _path, val) => val == null || validator.isIn(val, values);
return isIn;
}
exports.isIn = isIn;
function isInt(options) {
const isInt = (_target, _path, val) => val == null || validator.isInt(val, options);
return isInt;
}
exports.isInt = isInt;
const isJSON = (_target, _path, val) => val == null || validator.isJSON(val);
exports.isJSON = isJSON;
const isLatLong = (_target, _path, val) => val == null || validator.isLatLong(val);
exports.isLatLong = isLatLong;
function isLength(a, b) {
const isLength = (_target, _path, val) => {
if (val == null) {
return true;
}
else if (_.isArray(val)) {
return a <= val.length && (b == null || val.length <= b);
}
else {
return validator.isLength(val, a, b);
}
};
return isLength;
}
exports.isLength = isLength;
const isString = (_target, _path, val) => val == null || _.isString(val);
exports.isString = isString;
const isArray = (_target, _path, val) => val == null || _.isArray(val);
exports.isArray = isArray;
const isNumber = (_target, _path, val) => val == null || _.isNumber(val);
exports.isNumber = isNumber;
const isLowercase = (_target, _path, val) => val == null || validator.isLowercase(val);
exports.isLowercase = isLowercase;
const isMACAddress = (_target, _path, val) => val == null || validator.isMACAddress(val);
exports.isMACAddress = isMACAddress;
const isMD5 = (_target, _path, val) => val == null || validator.isMD5(val);
exports.isMD5 = isMD5;
const isMimeType = (_target, _path, val) => val == null || validator.isMimeType(val);
exports.isMimeType = isMimeType;
function isInRange(min, max) {
const isInRange = (_target, _path, val) => {
return val == null || (val >= min && (max == null || val <= max));
};
return isInRange;
}
exports.isInRange = isInRange;
function isMobilePhone(locale, options) {
const isMobilePhone = (_target, _path, val) => val == null || validator.isMobilePhone(val, locale, options);
return isMobilePhone;
}
exports.isMobilePhone = isMobilePhone;
const isMongoId = (_target, _path, val) => val == null || validator.isMongoId(val);
exports.isMongoId = isMongoId;
const isMultibyte = (_target, _path, val) => val == null || validator.isMultibyte(val);
exports.isMultibyte = isMultibyte;
const isNumeric = (_target, _path, val) => val == null || validator.isNumeric(val);
exports.isNumeric = isNumeric;
const isPort = (_target, _path, val) => val == null || validator.isPort(val);
exports.isPort = isPort;
function isPostalCode(locale) {
const isPostalCode = (_target, _path, val) => val == null || validator.isPostalCode(val, locale);
return isPostalCode;
}
exports.isPostalCode = isPostalCode;
const isSurrogatePair = (_target, _path, val) => val == null || validator.isSurrogatePair(val);
exports.isSurrogatePair = isSurrogatePair;
function isURL(options) {
const isURL = (_target, _path, val) => val == null || validator.isURL(val, options);
return isURL;
}
exports.isURL = isURL;
function isUUID(version) {
const isUUID = (_target, _path, val) => val == null || validator.isUUID(val, version);
return isUUID;
}
exports.isUUID = isUUID;
const isUppercase = (_target, _path, val) => val == null || validator.isUppercase(val);
exports.isUppercase = isUppercase;
const isVariableWidth = (_target, _path, val) => val == null || validator.isVariableWidth(val);
exports.isVariableWidth = isVariableWidth;
function isWhitelisted(chars) {
const isWhitelisted = (_target, _path, val) => val == null || validator.isWhitelisted(val, chars);
return isWhitelisted;
}
exports.isWhitelisted = isWhitelisted;
function matches(pattern, modifiers) {
const matches = (_target, _path, val) => val == null || validator.matches(val, pattern, modifiers);
return matches;
}
exports.matches = matches;
function isStartOf(unit, tz) {
const isStartOf = (_target, _path, val) => {
if (val == null) {
return true;
}
const time = tz ? moment(new Date(val)).tz(tz) : moment(val);
if (time.isSame(time.clone().startOf(unit))) {
return true;
}
return unit;
};
return isStartOf;
}
exports.isStartOf = isStartOf;
function isEndOf(unit, tz) {
const isEndOf = (_target, _path, val) => {
if (val == null) {
return true;
}
const time = tz ? moment(new Date(val)).tz(tz) : moment(val);
if (time.isSame(time.clone().endOf(unit))) {
return true;
}
return unit;
};
return isEndOf;
}
exports.isEndOf = isEndOf;
// --------------------------- Sanitizers ----------------------------------- //
function blacklist(chars) {
const blacklist = (target, path, val) => {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.blacklist(val, chars));
}
return true;
};
return blacklist;
}
exports.blacklist = blacklist;
function escape(target, path, val) {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.escape(val));
}
return true;
}
exports.escape = escape;
function unescape(target, path, val) {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.unescape(val));
}
return true;
}
exports.unescape = unescape;
function ltrim(chars) {
const ltrim = (target, path, val) => {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.ltrim(val, chars));
}
return true;
};
return ltrim;
}
exports.ltrim = ltrim;
function normalizeEmail(options) {
const normalizeEmail = (target, path, val) => {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.normalizeEmail(val, options));
}
return true;
};
return normalizeEmail;
}
exports.normalizeEmail = normalizeEmail;
function rtrim(chars) {
const rtrim = (target, path, val) => {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.rtrim(val, chars));
}
return true;
};
return rtrim;
}
exports.rtrim = rtrim;
function stripLow(keep_new_lines) {
const stripLow = (target, path, val) => {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.stripLow(val, keep_new_lines));
}
return true;
};
return stripLow;
}
exports.stripLow = stripLow;
function toJSON() {
const toJSON = (target, path, val) => {
if (val != null && _.isString(val)) {
let obj;
try {
obj = JSON.parse(val);
}
catch (error) {
return 'not a valid json';
}
object_path_1.withInheritedProps.set(target, path, obj);
}
return true;
};
return toJSON;
}
exports.toJSON = toJSON;
function toBoolean(strict) {
const toBoolean = (target, path, val) => {
if (val != null && !_.isBoolean(val)) {
object_path_1.withInheritedProps.set(target, path, validator.toBoolean(val, strict));
}
return true;
};
return toBoolean;
}
exports.toBoolean = toBoolean;
function toDate(target, path, val) {
if (val != null && !_.isDate(val)) {
object_path_1.withInheritedProps.set(target, path, validator.toDate(val));
}
return true;
}
exports.toDate = toDate;
function toSeconds() {
const toSeconds = (target, path, val) => {
if (val != null && val) {
if (!_.isDate(val)) {
object_path_1.withInheritedProps.set(target, path, Math.floor(validator.toDate(val).getTime() / 1000));
}
else if (_.isDate(val)) {
object_path_1.withInheritedProps.set(target, path, Math.floor(val.getTime() / 1000));
}
}
return true;
};
return toSeconds;
}
exports.toSeconds = toSeconds;
function toFloat(target, path, val) {
if (val != null && !_.isNumber(val)) {
object_path_1.withInheritedProps.set(target, path, validator.toFloat(val));
}
return true;
}
exports.toFloat = toFloat;
function toInt(radix) {
const toInt = (target, path, val) => {
if (val != null) {
const value = _.isNumber(val)
? Math.floor(val)
: validator.toInt(val, radix);
object_path_1.withInheritedProps.set(target, path, value);
}
return true;
};
return toInt;
}
exports.toInt = toInt;
function trim(chars) {
const trim = (target, path, val) => {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.trim(val, chars));
}
return true;
};
return trim;
}
exports.trim = trim;
function whitelist(chars) {
const whitelist = (target, path, val) => {
if (val != null) {
object_path_1.withInheritedProps.set(target, path, validator.whitelist(val, chars));
}
return true;
};
return whitelist;
}
exports.whitelist = whitelist;
function split(separator = ',', restricted = false) {
const split = (target, path, val) => {
if (val != null && !_.isString(val)) {
return !restricted;
}
if (val != null) {
object_path_1.withInheritedProps.set(target, path, val === '' ? [] : val.split(separator));
}
return true;
};
return split;
}
exports.split = split;
function withDefault(option, includeEmptyString = true) {
const withDefault = (target, path, val, root) => {
if (val == null || (val === '' && includeEmptyString)) {
val = _.isFunction(option) ? option(root) : option;
object_path_1.withInheritedProps.set(target, path, val);
}
return true;
};
return withDefault;
}
exports.withDefault = withDefault;
/**
* String.replace
*
* @param matcher
* @param replacer
*/
function replace(matcher, replacer) {
const replace = (target, path, val) => {
if (val != null && _.isString(val)) {
object_path_1.withInheritedProps.set(target, path, val.replace(matcher, replacer));
}
return true;
};
return replace;
}
exports.replace = replace;
function extractId(field = '_id') {
const extractId = (target, path, val) => {
if (val != null &&
!_.isString(val) &&
_.isObject(val) &&
_.isString(val[field])) {
object_path_1.withInheritedProps.set(target, path, val[field]);
}
return true;
};
return extractId;
}
exports.extractId = extractId;
function toNull(target, path, val) {
if (val === 'null') {
object_path_1.withInheritedProps.set(target, path, null);
}
return true;
}
exports.toNull = toNull;
function map(fn) {
const map = (target, path, val, root) => {
if (val != null) {
val = fn(val, root);
object_path_1.withInheritedProps.set(target, path, val);
}
return true;
};
return map;
}
exports.map = map;
function array(options) {
const selfValidators = _.isArray(options) || _.isFunction(options) ? _.flatten([options]) : [];
const mappedOptions = _.isArray(options) || _.isFunction(options)
? []
: _.map(options, (v, key) => {
const vs = _.chain([v])
.flatten()
.filter((x) => !!x)
.value();
if (key.startsWith('!')) {
vs.push(required);
key = key.substring(1);
}
return { key, validators: vs };
});
const array = (_target, _path, val, root) => {
if (val == null) {
return;
}
if (!_.isArray(val)) {
return 'not an array';
}
const errors = [];
_.each(val, (_v, index) => {
for (const fn of selfValidators) {
const uv = val[index];
const pass = fn(val, index, uv, root);
if (pass === false || _.isString(pass)) {
errors.push({
path: index,
value: uv,
failed: fn.name,
reason: pass || '',
});
}
if (_.isArray(pass)) {
for (const error of pass) {
errors.push({
path: index + '.' + error.path,
value: error.value,
failed: fn.name + '>' + error.failed,
reason: error.reason,
});
}
}
}
const nErrors = params_1.processValidators(val[index], mappedOptions, root);
for (const error of nErrors) {
errors.push({
path: index + '.' + error.path,
value: error.value,
failed: error.failed,
reason: error.reason,
});
}
});
return errors;
};
return array;
}
exports.array = array;
//# sourceMappingURL=validators.js.map