UNPKG

@xapp/dynamo-service

Version:

A dynamo help class which will help maintain data integrity.

116 lines (115 loc) 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeItems = exports.subset = exports.throwIfContainsExtra = exports.throwIfDoesNotContain = exports.throwIfDoesContain = exports.objHasAttrs = void 0; function objHasAttrs(obj) { const testObj = obj || {}; return (Object.keys(testObj).length > 0 && testObj.constructor === Object); } exports.objHasAttrs = objHasAttrs; function defaultValidationErrorHandler(keys, error) { throw error; } function throwIfDoesContain(obj, bannedAttrs, onError = defaultValidationErrorHandler) { if (!obj || !bannedAttrs || bannedAttrs.length === 0) { return; } const sub = subset(obj, bannedAttrs); const keys = Object.keys(sub); if (keys.length > 0) { const error = new Error("Object can not contain keys: '" + keys.join(", ") + "'."); onError(keys, error); } } exports.throwIfDoesContain = throwIfDoesContain; function throwIfDoesNotContain(obj, requiredAttrs, undefinedPermitted, onError = defaultValidationErrorHandler) { if (!obj) { if (undefinedPermitted) { return; } else { throw new Error("Object can not be undefined."); } } if (!requiredAttrs || requiredAttrs.length === 0) { return; } const missingKeys = []; for (const requiredAttr of requiredAttrs) { const value = obj[requiredAttr]; if (value == null) { missingKeys.push(requiredAttr); } } if (missingKeys.length > 0) { onError(missingKeys, new Error("Object must contain keys: '" + missingKeys.join(", "))); } } exports.throwIfDoesNotContain = throwIfDoesNotContain; function throwIfContainsExtra(obj, restrictAttrs, undefinedPermitted, onError = defaultValidationErrorHandler) { if (!obj) { if (undefinedPermitted) { return; } else { throw new Error("Object can not be undefined."); } } if (!restrictAttrs || restrictAttrs.length === 0) { return; } const removed = removeItems(obj, restrictAttrs); const keys = Object.keys(removed); if (keys.length > 0) { const error = new Error("Object does not pass validation. Keys: '" + keys.join(", ") + "' are not permitted."); onError(keys, error); } } exports.throwIfContainsExtra = throwIfContainsExtra; function subset(obj, attrs) { if (!obj) { return obj; } if (!attrs || attrs.length === 0) { return {}; } if (Array.isArray(obj)) { return obj.reduce((last, i) => { if (attrs.indexOf(i) >= 0) { last.push(i); } return last; }, []); } return attrs.reduce((last, value) => { if (obj.hasOwnProperty(value)) { last[value] = obj[value]; } return last; }, {}); } exports.subset = subset; function removeItems(obj, attrs) { if (!obj) { return obj; } if (!attrs || attrs.length === 0) { return obj; } if (Array.isArray(obj)) { const copy = obj.slice(); for (let attr of attrs) { const index = copy.indexOf(attr); if (index >= 0) { copy.splice(index, 1); } } return copy; } return attrs.reduce((last, attr) => { if (last[attr]) { delete last[attr]; } return last; }, Object.assign({}, obj)); } exports.removeItems = removeItems;