flowjv
Version:
Flow based approach to JSON validation!
206 lines (205 loc) • 8.97 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 __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
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.compileSchema = void 0;
var jsonexpression_1 = require("../jsonexpression");
function compileSchema(schema) {
var blocks = {};
function compile(schema, condPath, dataPath, deps) {
var e_1, _a;
var _b, _c, _d;
function compileProperty(prop, condPath, dataPath, deps) {
var e_2, _a;
var _b, _c, _d, _e, _f, _g;
switch (prop.type) {
case "if": {
compile(prop, condPath, dataPath, deps);
break;
}
case "switch": {
compile(prop, condPath, dataPath, deps);
break;
}
case "object": {
var path = __spreadArray(__spreadArray([], __read(dataPath)), [prop.key]).join(".");
if (!blocks[path]) {
blocks[path] = {
items: [],
deps: [],
};
}
(_c = (_b = blocks[path]) === null || _b === void 0 ? void 0 : _b.items) === null || _c === void 0 ? void 0 : _c.push({
condPath: condPath,
schema: prop,
deps: deps,
});
try {
for (var _h = __values(prop.properties), _j = _h.next(); !_j.done; _j = _h.next()) {
var p = _j.value;
compileProperty(p, condPath, __spreadArray(__spreadArray([], __read(dataPath)), [prop.key]), deps);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_j && !_j.done && (_a = _h.return)) _a.call(_h);
}
finally { if (e_2) throw e_2.error; }
}
break;
}
case "array": {
{
var path = __spreadArray(__spreadArray([], __read(dataPath)), [prop.key]).join(".");
if (!blocks[path]) {
blocks[path] = {
items: [],
deps: [],
};
}
(_e = (_d = blocks[path]) === null || _d === void 0 ? void 0 : _d.items) === null || _e === void 0 ? void 0 : _e.push({
condPath: condPath,
schema: prop,
deps: deps,
});
}
// Item Compilation.
{
var path = __spreadArray(__spreadArray([], __read(dataPath)), [prop.key, "$"]).join(".");
if (!blocks[path]) {
blocks[path] = {
items: [],
deps: [],
};
}
(_g = (_f = blocks[path]) === null || _f === void 0 ? void 0 : _f.items) === null || _g === void 0 ? void 0 : _g.push({
condPath: condPath,
schema: prop.itemSchema,
deps: deps,
});
compile(prop.itemSchema, condPath, __spreadArray(__spreadArray([], __read(dataPath)), [prop.key, "$"]), deps);
}
break;
}
default: {
compile(prop, condPath, __spreadArray(__spreadArray([], __read(dataPath)), [prop.key]), deps);
break;
}
}
}
switch (schema.type) {
case "object": {
try {
for (var _e = __values(schema.properties), _f = _e.next(); !_f.done; _f = _e.next()) {
var prop = _f.value;
compileProperty(prop, condPath, dataPath, deps);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
break;
}
case "if": {
schema.true.forEach(function (prop) {
return compileProperty(prop, __spreadArray(__spreadArray([], __read(condPath)), [
{
expr: schema.cond,
value: true,
},
]), dataPath, jsonexpression_1.combineDependencies(deps, jsonexpression_1.getDependencies(schema.cond)));
});
(_b = schema.false) === null || _b === void 0 ? void 0 : _b.forEach(function (prop) {
return compileProperty(prop, __spreadArray(__spreadArray([], __read(condPath)), [
{
expr: schema.cond,
value: false,
},
]), dataPath, jsonexpression_1.combineDependencies(deps, jsonexpression_1.getDependencies(schema.cond)));
});
break;
}
case "switch": {
Object.entries(schema.cases).forEach(function (_a) {
var _b = __read(_a, 2), key = _b[0], value = _b[1];
value.forEach(function (prop) {
compile(prop, __spreadArray(__spreadArray([], __read(condPath)), [{ expr: schema.switch, value: key }]), dataPath, jsonexpression_1.combineDependencies(deps, jsonexpression_1.getDependencies(schema.switch)));
});
});
break;
}
case "array": {
compile(schema.itemSchema, condPath, __spreadArray(__spreadArray([], __read(dataPath)), ["$"]), deps);
break;
}
default: {
var path = dataPath.join(".");
if (!blocks[path]) {
blocks[path] = {
items: [],
deps: [],
};
}
(_d = (_c = blocks[path]) === null || _c === void 0 ? void 0 : _c.items) === null || _d === void 0 ? void 0 : _d.push({ condPath: condPath, schema: schema, deps: deps });
break;
}
}
}
compile(schema, [], [], []);
return Object.entries(blocks).reduce(function (agg, _a) {
var _b;
var _c;
var _d = __read(_a, 2), key = _d[0], value = _d[1];
return (__assign(__assign({}, agg), (_b = {}, _b[key] = {
items: value === null || value === void 0 ? void 0 : value.items,
deps: (_c = value === null || value === void 0 ? void 0 : value.items) === null || _c === void 0 ? void 0 : _c.reduce(function (agg, v) { return jsonexpression_1.combineDependencies(agg, v.deps); }, []),
}, _b)));
}, {});
}
exports.compileSchema = compileSchema;