2 * @filedescription Validation Strategy
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
12 * Container class for several different validation strategies.
14 class ValidationStrategy {
17 * Validates that a value is an array.
18 * @param {*} value The value to validate.
20 * @throws {TypeError} If the value is invalid.
23 if (!Array.isArray(value)) {
24 throw new TypeError("Expected an array.");
29 * Validates that a value is a boolean.
30 * @param {*} value The value to validate.
32 * @throws {TypeError} If the value is invalid.
34 static boolean(value) {
35 if (typeof value !== "boolean") {
36 throw new TypeError("Expected a Boolean.");
41 * Validates that a value is a number.
42 * @param {*} value The value to validate.
44 * @throws {TypeError} If the value is invalid.
46 static number(value) {
47 if (typeof value !== "number") {
48 throw new TypeError("Expected a number.");
53 * Validates that a value is a object.
54 * @param {*} value The value to validate.
56 * @throws {TypeError} If the value is invalid.
58 static object(value) {
59 if (!value || typeof value !== "object") {
60 throw new TypeError("Expected an object.");
65 * Validates that a value is a object or null.
66 * @param {*} value The value to validate.
68 * @throws {TypeError} If the value is invalid.
70 static "object?"(value) {
71 if (typeof value !== "object") {
72 throw new TypeError("Expected an object or null.");
77 * Validates that a value is a string.
78 * @param {*} value The value to validate.
80 * @throws {TypeError} If the value is invalid.
82 static string(value) {
83 if (typeof value !== "string") {
84 throw new TypeError("Expected a string.");
89 * Validates that a value is a non-empty string.
90 * @param {*} value The value to validate.
92 * @throws {TypeError} If the value is invalid.
94 static "string!"(value) {
95 if (typeof value !== "string" || value.length === 0) {
96 throw new TypeError("Expected a non-empty string.");
102 exports.ValidationStrategy = ValidationStrategy;