UNPKG

guarder

Version:

Guarder provides simple validation logic to reduce clutter with inline guard statements

30 lines 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Guarder_1 = require("../Guarder"); const ArgumentError_1 = require("../errors/ArgumentError"); /** * Empty Guard ensures that the property is not null or undefined. A string should contain at least one character, an * array should contain at least one item, an object should contain at least one key */ class EmptyGuard { /** * @inheritDoc */ guard(property, errorMessage, error) { const message = errorMessage !== null && errorMessage !== void 0 ? errorMessage : 'Property not allowed to be empty'; Guarder_1.Guarder.null(property, message, error); Guarder_1.Guarder.undefined(property, message, error); const isEmptyString = typeof property === 'string' ? property === '' : false; const isArrayEmpty = Array.isArray(property) ? property.length === 0 : false; const isObjectEmpty = typeof property === 'object' ? Object.keys(property) .length === 0 : false; if (isEmptyString || isArrayEmpty || isObjectEmpty) { if (error) throw new error(message); throw new ArgumentError_1.ArgumentError(message); } return property; } } exports.EmptyGuard = EmptyGuard; //# sourceMappingURL=EmptyGuard.js.map