globalstorage
Version:
Global Storage is a Global Distributed Data Warehouse
154 lines (122 loc) • 5.95 kB
JavaScript
;
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var _require = require('metaschema'),
ValidationError = _require.errors.ValidationError,
extractDecorator = _require.extractDecorator;
var checkLink = function checkLink(value, path) {
var valueClass = value.constructor.name;
return valueClass === 'Uint64' || valueClass === 'String' ? [] : [new ValidationError('invalidClass', path, {
expected: ['Uint64', 'String'],
actual: valueClass
})];
};
var validateLink = function validateLink(ms, property, instance, options) {
var path = options.path;
var type = extractDecorator(property);
if (type === 'Include') {
var _type = _typeof(instance);
if (_type !== 'object') {
return [new ValidationError('invalidType', path, {
expected: 'object',
actual: _type
})];
}
options.path += '.';
var error = ms.validate('category', property, instance, options);
return error ? error.errors : [];
} else if (type !== 'Many') {
return checkLink(instance, path);
} else if (!Array.isArray(instance)) {
return [new ValidationError('invalidType', "".concat(path), {
expected: 'Array',
actual: _typeof(instance)
})];
} else {
return instance.reduce(function (acc, cur, idx) {
return acc.concat(checkLink(cur, "".concat(path, "[").concat(idx, "]")));
}, []);
}
};
var validate = function validate(ms, schema, instance) {
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var _options$path = options.path,
path = _options$path === void 0 ? '' : _options$path,
_options$patch = options.patch,
patch = _options$patch === void 0 ? false : _options$patch;
var errors = [];
var schemaProps = new Set(Object.keys(schema));
var objectProps = new Set(Object.keys(instance));
var props = new Set([].concat(_toConsumableArray(schemaProps), _toConsumableArray(objectProps)));
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = props[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var prop = _step.value;
var isSchemaProp = schemaProps.has(prop);
var isObjectProp = objectProps.has(prop);
if (isObjectProp && !isSchemaProp) {
if (prop !== 'Id') {
errors.push(new ValidationError('unresolvedProperty', "".concat(path).concat(prop)));
}
continue;
}
var property = schema[prop];
if (extractDecorator(property) === 'Validate' && !patch) {
if (!property.validate(instance)) {
errors.push(new ValidationError('validation', "".concat(path).concat(prop)));
}
continue;
}
if (property.readOnly && patch) {
errors.push(new ValidationError('immutable', "".concat(path).concat(prop)));
continue;
}
if (!isObjectProp) {
if (property.required && !patch && property.default === undefined) {
errors.push(new ValidationError('missingProperty', "".concat(path).concat(prop)));
}
continue;
}
var value = instance[prop];
if (value === undefined || value === null) {
if (property.required) {
errors.push(new ValidationError('emptyValue', "".concat(path).concat(prop)));
}
continue;
}
var opts = _objectSpread({}, options);
opts.path = "".concat(path).concat(prop);
if (property.domain) {
var error = ms.validate('domains', property.domain, value, opts);
if (error) errors.push.apply(errors, _toConsumableArray(error.errors));
} else if (property.category) {
errors.push.apply(errors, _toConsumableArray(validateLink(ms, property, value, opts)));
}
if (property.validate && !property.validate(value)) {
errors.push(new ValidationError('propValidation', "".concat(path).concat(prop)));
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return errors;
};
module.exports = validate;