All files / Nodejs/lib/template object-validator.js

0% Statements 0/78
0% Branches 0/86
0% Functions 0/21
0% Lines 0/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                                                                                                                                                                                                                                                                                               
"use strict";
 
var extend = require("extend");
 
function validate(obj) {
	var results = {
		obj: extend(true, {}, obj),
		error: {},
		warn: {},
		info: {}
	};
	var verifyPath = function(field) {
		var o = field.split(".").reverse().reduce((o, f) => {
			return {
				[f]: o
			}
		}, null);
		results.obj = extend(true, {}, results.obj, o, results.obj);
	};
 
	var set = function(obj, field, index, data) {
		if (!(field in obj)) {
			obj[field] = ['', '', ''];
		}
		if (obj[field][index] == '') {
			obj[field][index] = data.message;
			verifyPath(field);
		}
		if (obj[field][2] == '' && data.color) {
			obj[field][2] = data.color || '';
		}
	};
	var value = function(field) {
		return field.split(".").reduce((o, f) => {
			return (o != undefined && o != null) ? o[f] : undefined
		}, obj);
	}
	var v = function(field) {
		var ret = null;
		var closed = false;
		var isRequired = false;
		if (field) {
			ret = {
				isRequired: function() {
					return isRequired;
				},
				isClosed: function() {
					return closed;
				},
				required: function(opts) {
					isRequired = true;
					if (!closed) {
						var v = value(field);
						var t = typeof v;
						if (t == "undefined" || v == null) {
							closed = true;
							opts = opts || {};
							set(results[opts.type || 'error'], field, 0, Object.assign({
								message: "Missing required parameter"
							}, opts));
						}
					}
					return this;
				},
				in : function(values, opts) {
					if (!closed) {
						var v = value(field);
						if (values.indexOf(v) == -1 && (isRequired || v != undefined)) {
							opts = opts || {};
							set(results[opts.type || 'error'], field, 1, Object.assign({
								message: `expected (${values.join(',')})`
							}, opts));
						}
					}
					return this;
				},
				ofType: function(type, opts) {
					if (!closed) {
						var v = value(field);
						if ((typeof v !== type || (type == "array" && Array.isArray(v))) && (isRequired || v != undefined)) {
							opts = opts || {};
							set(results[opts.type || 'error'], field, 1, Object.assign({
								message: `invalid type, expected ${type}`
							}, opts));
						}
					}
					return this;
				},
				type: function() {
					return this.ofType.apply(this, arguments);
				},
				notOfType: function(type, opts) {
					if (!closed) {
						var v = value(field);
						if ((typeof v === type)) {
							opts = opts || {};
							set(results[opts.type || 'error'], field, 1, Object.assign({
								message: `invalid type ${type}`
							}, opts));
						}
					}
					return this;
				},
				notType: function() {
					return this.notOfType.apply(this, arguments);
				},
				match: function(regex, opts) {
					if (!closed) {
						var v = value(field);
						if ((isRequired || v != undefined) && (v == null || !v.match || !v.match(regex))) {
							opts = opts || {};
							set(results[opts.type || 'error'], field, 1, Object.assign({
								message: `invalid value, should match ${regex}`
							}, opts));
						}
					}
					return this;
				},
				info: function(message, color, side) {
					if (!closed) {
						var v = value(field);
						set(results.info, field, side || 0, {
							message: message,
							color: color
						});
					}
					return this;
				},
				assert: function(callback, opts) {
					if (!closed) {
						var v = value(field);
						if ((isRequired || v != undefined) && !callback(v)) {
							opts = opts || {};
							set(results[opts.type || 'error'], field, 1, Object.assign({
								message: `invalid value`
							}, opts));
						}
					}
					return this;
				},
				value: function() {
					return value(field);
				}
			}
		} else {
			ret = {
				results: function() {
					return results;
				}
			}
		}
		return ret;
	};
	v.results = function() {
		return results;
	}
	return v;
}
 
module.exports = validate;