flowjv
Version:
Flow based approach to JSON validation!
118 lines (117 loc) • 4.84 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.execObjectFlow = void 0;
var flowatoms_1 = require("../flowatoms");
var logic_1 = require("./logic");
var execObjectFlow = function (objectFlow, data, options) {
var e_1, _a;
var properties = objectFlow.properties;
var returnValue = { errors: [], isValid: true };
var updateResult = function (result) {
var _a;
returnValue.isValid = !result.isValid ? false : returnValue.isValid;
(_a = returnValue.errors).push.apply(_a, __spread(result.errors));
return returnValue;
};
var shouldReturnResult = function (result) {
return !result.isValid && !(options === null || options === void 0 ? void 0 : options.aggressive);
};
try {
for (var properties_1 = __values(properties), properties_1_1 = properties_1.next(); !properties_1_1.done; properties_1_1 = properties_1.next()) {
var config = properties_1_1.value;
switch (config.type) {
case "switch": {
var flow = logic_1.switchLogic(config, data, options);
if (flow) {
var result = exports.execObjectFlow({ type: "object", properties: flow }, data, options);
if (shouldReturnResult(updateResult(result))) {
return returnValue;
}
}
break;
}
case "if": {
var flow = logic_1.ifLogic(config, data, options);
if (flow) {
var result = exports.execObjectFlow({ type: "object", properties: flow }, data, options);
if (shouldReturnResult(updateResult(result))) {
return returnValue;
}
}
break;
}
default: {
var key = config.key;
var newRefPath = __spread(data.refPath, [key]);
switch (config.type) {
case "object": {
var result = exports.execObjectFlow(config, __assign(__assign({}, data), { refPath: newRefPath }), options);
if (shouldReturnResult(updateResult(result))) {
return returnValue;
}
break;
}
// Default specifies a primitive value type!
default: {
var result = flowatoms_1.execPrimitiveFlow(config, __assign(__assign({}, data), { refPath: newRefPath }), options);
if (shouldReturnResult(updateResult(result))) {
return returnValue;
}
}
}
}
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (properties_1_1 && !properties_1_1.done && (_a = properties_1.return)) _a.call(properties_1);
}
finally { if (e_1) throw e_1.error; }
}
return returnValue;
};
exports.execObjectFlow = execObjectFlow;