2020-04-08 22:45:21 +02:00
|
|
|
import {parseInteger} from "../utils/strings";
|
|
|
|
import Logger from "../logger";
|
2022-07-28 13:16:13 +02:00
|
|
|
import {isArray, isBoolean, isNumber, isObject, isRegExp, isString, isUndefined} from "../types";
|
2020-04-08 22:45:21 +02:00
|
|
|
|
2020-04-09 01:41:45 +02:00
|
|
|
export interface Constraint {
|
2020-04-08 22:45:21 +02:00
|
|
|
type: string,
|
|
|
|
|
2020-04-09 01:41:45 +02:00
|
|
|
default?: any,
|
|
|
|
|
2020-04-08 22:45:21 +02:00
|
|
|
optional?: boolean,
|
|
|
|
|
|
|
|
allowed?: string[],
|
|
|
|
|
|
|
|
min?: number,
|
|
|
|
max?: number,
|
|
|
|
|
|
|
|
regex?: RegExp,
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
export type Constraints = { [key: string]: Constraint };
|
|
|
|
export type Values = { [key: string]: any };
|
2020-04-09 01:41:45 +02:00
|
|
|
|
|
|
|
export function isConstraint(val: any): val is Constraint {
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!isObject(val)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
const constraint = val as { [key: string]: any };
|
2020-04-09 01:41:45 +02:00
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!("type" in constraint) || !isString(constraint.type)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("optional" in constraint
|
2022-07-28 13:16:13 +02:00
|
|
|
&& !isUndefined(constraint.optional)
|
|
|
|
&& !isBoolean(constraint.optional)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("allowed" in constraint
|
2022-07-28 13:16:13 +02:00
|
|
|
&& !isUndefined(constraint.allowed)
|
|
|
|
&& !isArray(constraint.allowed, isString)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("min" in constraint
|
2022-07-28 13:16:13 +02:00
|
|
|
&& !isUndefined(constraint.min)
|
|
|
|
&& !isNumber(constraint.min)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("max" in constraint
|
2022-07-28 13:16:13 +02:00
|
|
|
&& !isUndefined(constraint.max)
|
|
|
|
&& !isNumber(constraint.max)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// noinspection RedundantIfStatementJS
|
|
|
|
if ("regex" in constraint
|
2022-07-28 13:16:13 +02:00
|
|
|
&& !isUndefined(constraint.regex)
|
|
|
|
&& !isRegExp(constraint.regex)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isConstraints(constraints: any): constraints is Constraints {
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!isObject(constraints)) {
|
2020-04-09 01:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
return Object.entries(constraints).every(([key, constraint]) => isString(key) && isConstraint(constraint));
|
2020-04-09 01:41:45 +02:00
|
|
|
}
|
2020-04-08 22:45:21 +02:00
|
|
|
|
|
|
|
// TODO: sanitize input for further processing as specified by constraints (correct types, trimming, etc.)
|
|
|
|
|
|
|
|
function isValidBoolean(value: any): boolean {
|
2022-07-28 13:16:13 +02:00
|
|
|
return isBoolean(value) || value === 'true' || value === 'false';
|
2020-04-08 22:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isValidNumber(constraint: Constraint, value: any): boolean {
|
2022-07-28 13:16:13 +02:00
|
|
|
if (isString(value)) {
|
2020-04-08 22:45:21 +02:00
|
|
|
value = parseInteger(value);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!isNumber(value)) {
|
2020-04-08 22:45:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
if (isNaN(value) || !isFinite(value)) {
|
2020-04-08 22:45:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
if (isNumber(constraint.min) && value < constraint.min) {
|
2020-04-08 22:45:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// noinspection RedundantIfStatementJS
|
2022-07-28 13:16:13 +02:00
|
|
|
if (isNumber(constraint.max) && value > constraint.max) {
|
2020-04-08 22:45:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isValidEnum(constraint: Constraint, value: any): boolean {
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!isString(value)) {
|
2020-04-08 22:45:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
const allowed = constraint.allowed || [];
|
|
|
|
return allowed.indexOf(value) >= 0;
|
2020-04-08 22:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isValidString(constraint: Constraint, value: any): boolean {
|
|
|
|
if (!constraint.regex) {
|
|
|
|
throw new Error("String constraints must have regex set: " + constraint);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!isString(value)) {
|
2020-04-08 22:45:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const trimmed = value.trim();
|
|
|
|
return (trimmed === '' && constraint.optional) || constraint.regex.test(trimmed);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isValid(constraint: Constraint, acceptUndefined: boolean, value: any): boolean {
|
|
|
|
if (value === undefined) {
|
|
|
|
return acceptUndefined || constraint.optional === true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (constraint.type) {
|
|
|
|
case 'boolean':
|
|
|
|
return isValidBoolean(value);
|
|
|
|
|
|
|
|
case 'number':
|
|
|
|
return isValidNumber(constraint, value);
|
|
|
|
|
|
|
|
case 'enum':
|
|
|
|
return isValidEnum(constraint, value);
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
return isValidString(constraint, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.tag('validation').error('No validation method for constraint type: {}', constraint.type);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function areValid(constraints: Constraints, acceptUndefined: boolean, values: Values): boolean {
|
|
|
|
const fields = new Set(Object.keys(constraints));
|
|
|
|
for (const field of fields) {
|
|
|
|
if (!isValid(constraints[field], acceptUndefined, values[field])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const field of Object.keys(values)) {
|
|
|
|
if (!fields.has(field)) {
|
|
|
|
Logger.tag('validation').error('Validation failed: No constraint for field: {}', field);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
export function forConstraint(constraint: Constraint, acceptUndefined: boolean): (value: any) => boolean {
|
2020-04-08 22:45:21 +02:00
|
|
|
return ((value: any): boolean => isValid(constraint, acceptUndefined, value));
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
export function forConstraints(constraints: Constraints, acceptUndefined: boolean): (values: Values) => boolean {
|
2020-04-08 22:45:21 +02:00
|
|
|
return ((values: Values): boolean => areValid(constraints, acceptUndefined, values));
|
|
|
|
}
|