UNPKG

guarder

Version:

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

94 lines 3.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const NullGuard_1 = require("./guards/NullGuard"); const EmptyGuard_1 = require("./guards/EmptyGuard"); const FalsyGuard_1 = require("./guards/FalsyGuard"); const UndefinedGuard_1 = require("./guards/UndefinedGuard"); const GuardNotFoundError_1 = require("./errors/GuardNotFoundError"); /** * Guarder provides various guards which can be used for quick validation of data */ class Guarder { /** * Returns the property if the property is not null. Throws an error if the property is null. */ static null(property, message, error) { const emptyGuard = this.guards.get('null'); return emptyGuard.guard(property, message, error); } /** * Returns the property if the property is not undefined. Throws an error if the property is undefined. */ static undefined(property, message, error) { const emptyGuard = this.guards.get('undefined'); return emptyGuard.guard(property, message, error); } /** * Returns the property if the property is not an empty string, object, array, undefined or null. Throws an error if * the criteria is not met. */ static empty(property, message, error) { const emptyGuard = this.guards.get('empty'); return emptyGuard.guard(property, message, error); } /** * Returns the property if the property does not evaluate to false in a type coercion */ static falsy(property, message, error) { const falsyGuard = this.guards.get('falsy'); return falsyGuard.guard(property, message, error); } /** * Returns the property if the property passes the custom guards validation logic and will throw an Argument Error or * custom error if specified */ static custom(guardName, property, message, error) { const customGuard = this.guards.get(guardName); if (!customGuard) { throw new GuardNotFoundError_1.GuardNotFoundError(`Custom guard '${guardName}' not found in registered guards`); } return customGuard.guard(property, message, error); } /** * Returns the property if the property passes the custom guards validation logic and will throw an Argument Error or * custom error if specified */ static guard(guard, property, message, error) { const customGuard = new guard(); return customGuard.guard(property, message, error); } /** * Get all registered guards */ static getRegisteredGuards() { return [ ...this.guards.keys() ]; } /** * Remove a guard from the guard map */ static unregisterGuard(guardName) { if (this.guards.get(guardName)) { this.guards.delete(guardName); } } /** * Register custom unique guard in the guard map */ static registerGuard(guardName, guard) { if (!this.guards.get(guardName)) { this.guards.set(guardName, new guard()); } } } exports.Guarder = Guarder; /** * Default built-in guards */ Guarder.guards = new Map() .set('empty', new EmptyGuard_1.EmptyGuard()) .set('null', new NullGuard_1.NullGuard()) .set('undefined', new UndefinedGuard_1.UndefinedGuard()) .set('falsy', new FalsyGuard_1.FalsyGuard()); //# sourceMappingURL=Guarder.js.map