UNPKG

nope-validator

Version:
118 lines (113 loc) 4.8 kB
define(['exports', './_virtual/_tslib', './NopeReference', './utils'], (function (exports, _tslib, NopeReference, utils) { 'use strict'; var NopePrimitive = /** @class */ (function () { function NopePrimitive() { this.validationRules = []; this._type = 'undefined'; } NopePrimitive.prototype.getType = function () { return this._type; }; NopePrimitive.prototype.isEmpty = function (entry) { return utils.isNil(entry); }; NopePrimitive.prototype.required = function (message) { var _this = this; if (message === void 0) { message = 'This field is required'; } var rule = function (entry) { if (_this.isEmpty(entry)) { return message; } }; return this.test(rule); }; NopePrimitive.prototype.notAllowed = function (message) { var _this = this; if (message === void 0) { message = 'Field is not allowed'; } var rule = function (entry) { if (!_this.isEmpty(entry)) { return message; } }; return this.test(rule); }; NopePrimitive.prototype.when = function (keys, conditionObject) { var ctxKeys = Array.isArray(keys) ? keys : [keys]; var rule = function (_, context) { var resolvedConditionValues = utils.resolveNopeRefsFromKeys(ctxKeys, context); var values = _tslib.__spreadArray([], resolvedConditionValues); var condIs = conditionObject.is; var result = typeof condIs === 'function' ? condIs.apply(void 0, values) : resolvedConditionValues.every(function (val) { return val === condIs; }); return result ? conditionObject.then : conditionObject.otherwise; }; return this.test(rule); }; NopePrimitive.prototype.oneOf = function (options, message) { if (message === void 0) { message = 'Invalid option'; } var rule = function (entry, context) { if (entry === undefined) { return; } var resolved; if (options instanceof NopeReference.NopeReference) { resolved = utils.resolveNopeRef(options, context); } else { resolved = options.map(function (option) { return utils.resolveNopeRef(option, context); }); } if (resolved.indexOf(entry) === -1) { return message; } }; return this.test(rule); }; NopePrimitive.prototype.notOneOf = function (options, message) { if (message === void 0) { message = 'Invalid Option'; } var rule = function (entry, context) { var resolvedOptions = options.map(function (option) { return utils.resolveNopeRef(option, context); }); if (resolvedOptions.indexOf(entry) !== -1) { return message; } }; return this.test(rule); }; NopePrimitive.prototype.test = function (rule) { this.validationRules.push(rule); return this; }; /** * @param entry - The value to be validated * @param context - Used for internal reference resolving. Do not pass this. */ NopePrimitive.prototype.validate = function (entry, context) { this._entry = entry; for (var _i = 0, _a = this.validationRules; _i < _a.length; _i++) { var rule = _a[_i]; var error = rule(this._entry, context); if (error instanceof NopePrimitive) { return error.validate(this._entry, context); } else if (error) { return error; } } }; NopePrimitive.prototype.validateAsync = function (entry, context) { var _this = this; var _a; return utils.runValidators(this.validationRules, (_a = this._entry) !== null && _a !== void 0 ? _a : entry, context).then(function (error) { var _a; if (error instanceof NopePrimitive) { return error.validateAsync((_a = _this._entry) !== null && _a !== void 0 ? _a : entry, context); } else if (error) { return error; } }); }; return NopePrimitive; }()); exports.NopePrimitive = NopePrimitive; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=NopePrimitive.js.map