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
261 lines (208 loc) • 6.54 kB
JavaScript
import Bunyan from 'bunyan';
const log = Bunyan.createLogger({name: "FieldValidator"});
import { NONE, CLASS_O_MAT, FIELD, FIELD_VALIDATOR_O_MAT } from '../constants/types';
import {
getter,
setter,
getterSetter,
toggler,
swapper,
} from '../methods/field-functions';
class FieldValidatorOMat {
static TYPE = FIELD_VALIDATOR_O_MAT;
TYPE = FIELD_VALIDATOR_O_MAT;
constructor(field, key, cacheValidators) {
const validationsInfo = (cacheValidators === false)
? NONE
: { meta: false, type: NONE, conditions: ['+'] }
this._params = {
field,
key,
callback: NONE,
funktion: (value)=>true,
andMode: true,
message: 'Parameter Error',
meta : false,
doThrow: true,
isNot: false,
validationsInfo
}
}
connect(fieldOMat) {
this._params.field = fieldOMat;
}
$() {
const { callback, doThrow, funktion, key, message, meta, field } = this._params;
const _message = `${field._params.parent.key()} ${key}: ${message}`;
const validator = (meta === true)
? { meta: true }
: (doThrow === true && callback === NONE)
? (v)=>{
if (funktion(v)===true) return true;
throw new Error(_message);
}
: (callback === NONE)
? (v)=>{
if (funktion(v)===true) return true;
return false;
}
: (v)=>{
const isValid = funktion(v)===true;
callback(isValid, key, v);
return isValid;
}
return this._params.field._acceptValidator(
validator,
this.cacheValidators === true ? this : NONE
);
}
$$() { return this.$().$() }
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
meta() {
this._params.meta = true;
this._toValidationsInfo('meta')
return this;
}
and() {
this._params.andMode = true;
this._toValidationsInfo('&')
return this;
}
or() {
this._params.andMode = false;
this._toValidationsInfo('|');
return this;
}
is() {
this._params.isNot = false;
this._toValidationsInfo('==')
return this;
}
not() {
this._params.isNot = true;
this._toValidationsInfo('!=')
return this;
}
_toValidationsInfo(key, value=NONE) {
if (this._params.validationsInfo === NONE) return;
if (key === 'meta') {
this._params.validationsInfo.meta = true;
return
}
this._params.validationsInfo.conditions.push({ key, value });
}
with(funktion=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;
}
withFunction(funktion) {
this._placeFunktion(funktion);
return this;
}
_placeFunktion(_funktion, isType, key, value) {
const 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 =(v)=>funktion(v) !== true;
if (this._params.validationsInfo === NONE) return this;
if (isType === true ) {
this._params.validationsInfo.type = key;
return this;
}
this._params.validationsInfo.conditions.push({key, value});
return this;
}
_andFunktion(_funktion) {
const { funktion } = this._params;
return (v)=>funktion(v) && _funktion(v);
}
_orFunktion(_funktion) {
const { funktion } = this._params;
return (v)=>funktion(v) || _funktion(v);
}
bool() {
return this._placeFunktion((v)=>typeof(v)==='boolean', true, 'bool');
}
int() {
return this._placeFunktion((v)=>Number.isInteger(v), true, 'int');
}
uint() {
return this._placeFunktion((v)=>Number.isInteger(v) && v >=0, true, 'uint');
}
equal(comp) {
return this._placeFunktion((v)=> v === comp, false, 'equal', comp);
}
equalIsh(comp) {
return this._placeFunktion((v)=> v == comp, false, 'equalIsh', comp);
}
greaterThan(comp) {
return this._placeFunktion((v)=>v > comp, false, 'gt', comp);
}
gt(comp) { return this.greaterThan(comp) };
gte(comp) { return this.greaterThanOrEqual(comp) };
greaterThanOrEqual(comp) {
return this._placeFunktion((v)=> v >= comp, false, 'gte', comp);
}
lt(comp) { return this.lessThan(comp)}
lte(comp) { return this.lessThanOrEqual(comp)}
lessThan(comp) {
return this._placeFunktion((v)=>v < comp, false, 'lt', comp);
}
lessThanOrEqual(comp) {
return this._placeFunktion((v)=>v <= comp, false, 'lte', comp);
}
method() {
return this._placeFunktion((v)=>typeof v === 'function', true, 'method');
}
number() {
return this._placeFunktion((v)=>typeof v === 'number', true, 'number');
}
object() {
return this._placeFunktion((v)=>typeof v ==='object');
}
oMat() {
return this._placeFunktion((v)=>v.TYPE === CLASS_O_MAT, true, 'omat');
}
string() {
return this._placeFunktion((v)=>typeof v === 'string', true, 'string');
}
selectList(...list) {
return this._placeFunktion((v)=>list.indexOf(v) !== -1);
}
message(message) {
this._params.message = message;
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}
*/
validatorsInfo() {
return this._params.validatorsInfo;
}
warn() {
this._params.doThrow = false;
return this;
}
}
export default FieldValidatorOMat;