UNPKG

flowjv

Version:

Flow based approach to JSON validation!

88 lines (87 loc) 3.36 kB
"use strict"; 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.switchLogic = exports.ifLogic = void 0; var jsonexpression_1 = require("../../../jsonexpression"); var immutable_1 = require("../../../helper/immutable"); var ifLogic = function (config, data, options) { var _a; var cond = !!jsonexpression_1.execJSONExpression(config.cond, data); if (options === null || options === void 0 ? void 0 : options.enforceSchema) { if (cond) { // delete false fields. (_a = config.false) === null || _a === void 0 ? void 0 : _a.forEach(function (v) { if (!config.true.find(function (v) { return v.key === v.key; })) { data.data = immutable_1.unset(data.data, __spread(data.refPath, [v.key])); } }); } else { // delete true fields. config.true.forEach(function (v) { if (!config.true.find(function (v) { return v.key === v.key; })) { data.data = immutable_1.unset(data.data, __spread(data.refPath, [v.key])); } }); } } var flow = cond ? config.true : config.false; return flow; }; exports.ifLogic = ifLogic; var switchLogic = function (config, data, options) { var e_1, _a; var cond = jsonexpression_1.execJSONExpression(config.cond, data); if (options === null || options === void 0 ? void 0 : options.enforceSchema) { try { // Delete keys of other cases. for (var _b = __values(Object.keys(config.cases)), _c = _b.next(); !_c.done; _c = _b.next()) { var v = _c.value; if (v !== cond) { config.cases[v].forEach(function (prop) { return immutable_1.unset(data.data, __spread(data.refPath, [prop.key])); }); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } } return config.cases[cond]; }; exports.switchLogic = switchLogic;