type ValidationFunction = (x: any) => string | undefined; export const validateArgs = function ( args: {value: any, name: string, func: ValidationFunction}[], ): string | undefined { for (const arg of args) { const errmsg = arg.func(arg.value); if (errmsg) return `${arg.name}`+errmsg; } } export const validateString = function(s: any): string | undefined { if (s === null || s === undefined || s.constructor !== String) { return `' should be a String`; } if (s.length === 0) return `' cannot be an empty String.`; } export const validateStringArray = function(a: any): string | undefined { if (a === null || a === undefined || a.constructor !== Array) { return `' should be a String[].`; } if (a.length === 0) return `' cannot be an empty Array.`; for (let i = 0; i < a.length; i++) { let v = a[i]; if (v === null || v === undefined || v.constructor !== String) { return `[${i}]' should be a String.`; } } } export const validateObject = function(o: any): string | undefined { if (o === null || o === undefined || o.constructor !== Object) { return `' should be an Object.`; } } const saneCheckDate = new Date('2022'); // Sane check for date value but also a check for numeric value producing a valid date. export const validateDate = function(date: any): string | undefined { if (!(new Date(date) > saneCheckDate)) return `date' is invalid or too small. Received: '${date}'.`; }