class-o-mat
Version:
ALPHA VERSION ONLY: class-o-mats will help to create getter-setter functionality with validations that can be attached to a container class for easier coding. Some basic validations are included. class-o-mats also has the ability to store params as bloc
366 lines (333 loc) • 10.5 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _class, _temp;
var _bunyan = require('bunyan');
var _bunyan2 = _interopRequireDefault(_bunyan);
var _types = require('../constants/types');
var _fieldFunctions = require('../methods/field-functions');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var log = _bunyan2.default.createLogger({ name: "FieldValidator" });
var FieldValidatorOMat = (_temp = _class = function () {
function FieldValidatorOMat(field, key, cacheValidators) {
_classCallCheck(this, FieldValidatorOMat);
this.TYPE = _types.FIELD_VALIDATOR_O_MAT;
var validationsInfo = cacheValidators === false ? _types.NONE : { meta: false, type: _types.NONE, conditions: ['+'] };
this._params = {
field: field,
key: key,
callback: _types.NONE,
funktion: function funktion(value) {
return true;
},
andMode: true,
message: 'Parameter Error',
meta: false,
doThrow: true,
isNot: false,
validationsInfo: validationsInfo
};
}
_createClass(FieldValidatorOMat, [{
key: 'connect',
value: function connect(fieldOMat) {
this._params.field = fieldOMat;
}
}, {
key: '$',
value: function $() {
var _params = this._params,
callback = _params.callback,
doThrow = _params.doThrow,
funktion = _params.funktion,
key = _params.key,
message = _params.message,
meta = _params.meta,
field = _params.field;
var _message = field._params.parent.key() + ' ' + key + ': ' + message;
var validator = meta === true ? { meta: true } : doThrow === true && callback === _types.NONE ? function (v) {
if (funktion(v) === true) return true;
throw new Error(_message);
} : callback === _types.NONE ? function (v) {
if (funktion(v) === true) return true;
return false;
} : function (v) {
var isValid = funktion(v) === true;
callback(isValid, key, v);
return isValid;
};
return this._params.field._acceptValidator(validator, this.cacheValidators === true ? this : _types.NONE);
}
}, {
key: '$$',
value: function $$() {
return this.$().$();
}
}, {
key: 'callback',
value: function callback(_callback) {
this._params.callback = _callback;
return this;
}
// if this is true, ignore validations...
// this will be used later as a way to note the expected values
// without them actually be run as validators at runtime.
// .. right now it just means ignore this field validator.
// later it could be used in warn or error messages
}, {
key: 'meta',
value: function meta() {
this._params.meta = true;
this._toValidationsInfo('meta');
return this;
}
}, {
key: 'and',
value: function and() {
this._params.andMode = true;
this._toValidationsInfo('&');
return this;
}
}, {
key: 'or',
value: function or() {
this._params.andMode = false;
this._toValidationsInfo('|');
return this;
}
}, {
key: 'is',
value: function is() {
this._params.isNot = false;
this._toValidationsInfo('==');
return this;
}
}, {
key: 'not',
value: function not() {
this._params.isNot = true;
this._toValidationsInfo('!=');
return this;
}
}, {
key: '_toValidationsInfo',
value: function _toValidationsInfo(key) {
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _types.NONE;
if (this._params.validationsInfo === _types.NONE) return;
if (key === 'meta') {
this._params.validationsInfo.meta = true;
return;
}
this._params.validationsInfo.conditions.push({ key: key, value: value });
}
}, {
key: 'with',
value: function _with() {
var funktion = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _types.NONE;
if (typeof funktion !== 'function') {
log.info(' WARN validator.with, invalid function', this._params.field._params.key, this._params.field._params.parent.key());
}
this._placeFunktion(funktion);
return this;
}
}, {
key: 'withFunction',
value: function withFunction(funktion) {
this._placeFunktion(funktion);
return this;
}
}, {
key: '_placeFunktion',
value: function _placeFunktion(_funktion, isType, key, value) {
var funktion = this._params.andMode === true ? this._andFunktion(_funktion) : this._orFunktion(_funktion);
if (this._params.isNot === false) {
this._params.funktion = funktion;
return this;
}
this._params.funktion = function (v) {
return funktion(v) !== true;
};
if (this._params.validationsInfo === _types.NONE) return this;
if (isType === true) {
this._params.validationsInfo.type = key;
return this;
}
this._params.validationsInfo.conditions.push({ key: key, value: value });
return this;
}
}, {
key: '_andFunktion',
value: function _andFunktion(_funktion) {
var funktion = this._params.funktion;
return function (v) {
return funktion(v) && _funktion(v);
};
}
}, {
key: '_orFunktion',
value: function _orFunktion(_funktion) {
var funktion = this._params.funktion;
return function (v) {
return funktion(v) || _funktion(v);
};
}
}, {
key: 'bool',
value: function bool() {
return this._placeFunktion(function (v) {
return typeof v === 'boolean';
}, true, 'bool');
}
}, {
key: 'int',
value: function int() {
return this._placeFunktion(function (v) {
return Number.isInteger(v);
}, true, 'int');
}
}, {
key: 'uint',
value: function uint() {
return this._placeFunktion(function (v) {
return Number.isInteger(v) && v >= 0;
}, true, 'uint');
}
}, {
key: 'equal',
value: function equal(comp) {
return this._placeFunktion(function (v) {
return v === comp;
}, false, 'equal', comp);
}
}, {
key: 'equalIsh',
value: function equalIsh(comp) {
return this._placeFunktion(function (v) {
return v == comp;
}, false, 'equalIsh', comp);
}
}, {
key: 'greaterThan',
value: function greaterThan(comp) {
return this._placeFunktion(function (v) {
return v > comp;
}, false, 'gt', comp);
}
}, {
key: 'gt',
value: function gt(comp) {
return this.greaterThan(comp);
}
}, {
key: 'gte',
value: function gte(comp) {
return this.greaterThanOrEqual(comp);
}
}, {
key: 'greaterThanOrEqual',
value: function greaterThanOrEqual(comp) {
return this._placeFunktion(function (v) {
return v >= comp;
}, false, 'gte', comp);
}
}, {
key: 'lt',
value: function lt(comp) {
return this.lessThan(comp);
}
}, {
key: 'lte',
value: function lte(comp) {
return this.lessThanOrEqual(comp);
}
}, {
key: 'lessThan',
value: function lessThan(comp) {
return this._placeFunktion(function (v) {
return v < comp;
}, false, 'lt', comp);
}
}, {
key: 'lessThanOrEqual',
value: function lessThanOrEqual(comp) {
return this._placeFunktion(function (v) {
return v <= comp;
}, false, 'lte', comp);
}
}, {
key: 'method',
value: function method() {
return this._placeFunktion(function (v) {
return typeof v === 'function';
}, true, 'method');
}
}, {
key: 'number',
value: function number() {
return this._placeFunktion(function (v) {
return typeof v === 'number';
}, true, 'number');
}
}, {
key: 'object',
value: function object() {
return this._placeFunktion(function (v) {
return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object';
});
}
}, {
key: 'oMat',
value: function oMat() {
return this._placeFunktion(function (v) {
return v.TYPE === _types.CLASS_O_MAT;
}, true, 'omat');
}
}, {
key: 'string',
value: function string() {
return this._placeFunktion(function (v) {
return typeof v === 'string';
}, true, 'string');
}
}, {
key: 'selectList',
value: function selectList() {
for (var _len = arguments.length, list = Array(_len), _key = 0; _key < _len; _key++) {
list[_key] = arguments[_key];
}
return this._placeFunktion(function (v) {
return list.indexOf(v) !== -1;
});
}
}, {
key: 'message',
value: function message(_message2) {
this._params.message = _message2;
return this;
}
/**
* If cacheValidators === true, this will return the
* validators info. This is to be used for subclasses only
* and is here for convenience
* @return {object}
*/
}, {
key: 'validatorsInfo',
value: function validatorsInfo() {
return this._params.validatorsInfo;
}
}, {
key: 'warn',
value: function warn() {
this._params.doThrow = false;
return this;
}
}]);
return FieldValidatorOMat;
}(), _class.TYPE = _types.FIELD_VALIDATOR_O_MAT, _temp);
exports.default = FieldValidatorOMat;
//# sourceMappingURL=field-validator-o-mat.js.map