CoolFace
Apppublic

Umama-at-Bluchip/Quick-UI

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
validation-strategy.js103 linesDownload Raw Back to src
1/**2 * @filedescription Validation Strategy3 */4 5"use strict";6 7//-----------------------------------------------------------------------------8// Class9//-----------------------------------------------------------------------------10 11/**12 * Container class for several different validation strategies.13 */14class ValidationStrategy {15 16    /**17     * Validates that a value is an array.18     * @param {*} value The value to validate.19     * @returns {void}20     * @throws {TypeError} If the value is invalid. 21     */22    static array(value) {23        if (!Array.isArray(value)) {24            throw new TypeError("Expected an array.");25        }26    }27 28    /**29     * Validates that a value is a boolean.30     * @param {*} value The value to validate.31     * @returns {void}32     * @throws {TypeError} If the value is invalid. 33     */34    static boolean(value) {35        if (typeof value !== "boolean") {36            throw new TypeError("Expected a Boolean.");37        }38    }39 40    /**41     * Validates that a value is a number.42     * @param {*} value The value to validate.43     * @returns {void}44     * @throws {TypeError} If the value is invalid. 45     */46    static number(value) {47        if (typeof value !== "number") {48            throw new TypeError("Expected a number.");49        }50    }51 52    /**53     * Validates that a value is a object.54     * @param {*} value The value to validate.55     * @returns {void}56     * @throws {TypeError} If the value is invalid. 57     */58    static object(value) {59        if (!value || typeof value !== "object") {60            throw new TypeError("Expected an object.");61        }62    }63 64    /**65     * Validates that a value is a object or null.66     * @param {*} value The value to validate.67     * @returns {void}68     * @throws {TypeError} If the value is invalid. 69     */70    static "object?"(value) {71        if (typeof value !== "object") {72            throw new TypeError("Expected an object or null.");73        }74    }75 76    /**77     * Validates that a value is a string.78     * @param {*} value The value to validate.79     * @returns {void}80     * @throws {TypeError} If the value is invalid. 81     */82    static string(value) {83        if (typeof value !== "string") {84            throw new TypeError("Expected a string.");85        }86    }87 88    /**89     * Validates that a value is a non-empty string.90     * @param {*} value The value to validate.91     * @returns {void}92     * @throws {TypeError} If the value is invalid. 93     */94    static "string!"(value) {95        if (typeof value !== "string" || value.length === 0) {96            throw new TypeError("Expected a non-empty string.");97        }98    }99 100}101 102exports.ValidationStrategy = ValidationStrategy;103