UNPKG

flowjv

Version:

Flow based approach to JSON validation!

106 lines (105 loc) 4.64 kB
"use strict"; 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 __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."); }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateObjectProperties = exports.validateObjectType = void 0; var simple_1 = require("../simple"); var if_1 = require("../logic/if"); var switch_1 = require("../logic/switch"); var array_1 = require("./array"); function validateObjectType(schema, payload, config) { return validateObjectProperties(schema.properties, payload, config); } exports.validateObjectType = validateObjectType; function validateObjectProperties(properties, payload, config) { var e_1, _a; var errors = []; try { for (var properties_1 = __values(properties), properties_1_1 = properties_1.next(); !properties_1_1.done; properties_1_1 = properties_1.next()) { var property = properties_1_1.value; switch (property.type) { case "object": { var refPath = __spreadArray(__spreadArray([], __read(payload.refPath)), [property.key]); var result = validateObjectType(property, __assign(__assign({}, payload), { refPath: refPath }), config); errors = __spreadArray(__spreadArray([], __read(errors)), __read(result.errors)); break; } case "if": { var result = if_1.validateIfCondition(property, payload, config); errors = __spreadArray(__spreadArray([], __read(errors)), __read(result.errors)); break; } case "switch": { var result = switch_1.validateSwitchCondition(property, payload, config); errors = __spreadArray(__spreadArray([], __read(errors)), __read(result.errors)); break; } case "array": { var refPath = __spreadArray(__spreadArray([], __read(payload.refPath)), [property.key]); var result = array_1.validateArrayType(property, __assign(__assign({}, payload), { refPath: refPath }), config); errors = __spreadArray(__spreadArray([], __read(errors)), __read(result.errors)); break; } default: { var refPath = __spreadArray(__spreadArray([], __read(payload.refPath)), [property.key]); var result = simple_1.validateSimpleType(property, __assign(__assign({}, payload), { refPath: refPath }), config); !result.isValid && errors.push({ key: refPath, msgs: result.errors }); if (!config.aggressive) { break; } } } } } 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 { isValid: errors.length === 0, errors: errors, payload: payload }; } exports.validateObjectProperties = validateObjectProperties;