ESLint: Auto reformat and fixing some warnings / errors.

This commit is contained in:
baldo 2022-08-23 20:08:53 +02:00
parent 5237db38e0
commit 91690509d3
50 changed files with 2141 additions and 1493 deletions
server/shared/validation

View file

@ -3,115 +3,115 @@
// noinspection RegExpSimplifiable
const CONSTRAINTS = {
id:{
type: 'string',
id: {
type: "string",
regex: /^[1-9][0-9]*$/,
optional: false
optional: false,
},
token:{
type: 'string',
token: {
type: "string",
regex: /^[0-9a-f]{16}$/i,
optional: false
optional: false,
},
node: {
hostname: {
type: 'string',
type: "string",
regex: /^[-a-z0-9_]{1,32}$/i,
optional: false
optional: false,
},
key: {
type: 'string',
type: "string",
regex: /^([a-f0-9]{64})$/i,
optional: true
optional: true,
},
email: {
type: 'string',
type: "string",
regex: /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
optional: false
optional: false,
},
nickname: {
type: 'string',
type: "string",
regex: /^[-a-z0-9_ äöüß]{1,64}$/i,
optional: false
optional: false,
},
mac: {
type: 'string',
type: "string",
regex: /^([a-f0-9]{12}|([a-f0-9]{2}:){5}[a-f0-9]{2}|([a-f0-9]{2}-){5}[a-f0-9]{2})$/i,
optional: false
optional: false,
},
coords: {
type: 'string',
type: "string",
regex: /^(-?[0-9]{1,3}(\.[0-9]{1,15})? -?[0-9]{1,3}(\.[0-9]{1,15})?)$/,
optional: true
optional: true,
},
monitoring: {
type: 'boolean',
optional: false
}
type: "boolean",
optional: false,
},
},
nodeFilters: {
hasKey: {
type: 'boolean',
optional: true
type: "boolean",
optional: true,
},
hasCoords: {
type: 'boolean',
optional: true
type: "boolean",
optional: true,
},
onlineState: {
type: 'string',
type: "string",
regex: /^(ONLINE|OFFLINE)$/,
optional: true
optional: true,
},
monitoringState: {
type: 'string',
type: "string",
regex: /^(disabled|active|pending)$/,
optional: true
optional: true,
},
site: {
type: 'string',
type: "string",
regex: /^[a-z0-9_-]{1,32}$/,
optional: true
optional: true,
},
domain: {
type: 'string',
type: "string",
regex: /^[a-z0-9_-]{1,32}$/,
optional: true
}
optional: true,
},
},
rest: {
list: {
_page: {
type: 'number',
type: "number",
min: 1,
optional: true,
default: 1
default: 1,
},
_perPage: {
type: 'number',
type: "number",
min: 1,
max: 50,
optional: true,
default: 20
default: 20,
},
_sortDir: {
type: 'enum',
allowed: ['ASC', 'DESC'],
type: "enum",
allowed: ["ASC", "DESC"],
optional: true,
default: 'ASC'
default: "ASC",
},
_sortField: {
type: 'string',
type: "string",
regex: /^[a-zA-Z0-9_]{1,32}$/,
optional: true
optional: true,
},
q: {
type: 'string',
type: "string",
regex: /^[äöüß a-z0-9!#$%&@:.'*+/=?^_`{|}~-]{1,64}$/i,
optional: true
}
}
}
optional: true,
},
},
},
};
export default CONSTRAINTS;

View file

@ -1,23 +1,31 @@
import {parseInteger} from "../utils/strings";
import {isBoolean, isNumber, isObject, isOptional, isRegExp, isString, toIsArray} from "../types";
import { parseInteger } from "../utils/strings";
import {
isBoolean,
isNumber,
isObject,
isOptional,
isRegExp,
isString,
toIsArray,
} from "../types";
export interface Constraint {
type: string,
type: string;
default?: any,
default?: unknown;
optional?: boolean,
optional?: boolean;
allowed?: string[],
allowed?: string[];
min?: number,
max?: number,
min?: number;
max?: number;
regex?: RegExp,
regex?: RegExp;
}
export type Constraints = { [key: string]: Constraint };
export type Values = { [key: string]: any };
export type Values = { [key: string]: unknown };
export function isConstraint(arg: unknown): arg is Constraint {
if (!isObject(arg)) {
@ -36,18 +44,22 @@ export function isConstraint(arg: unknown): arg is Constraint {
);
}
export function isConstraints(constraints: unknown): constraints is Constraints {
export function isConstraints(
constraints: unknown
): constraints is Constraints {
if (!isObject(constraints)) {
return false;
}
return Object.entries(constraints).every(([key, constraint]) => isString(key) && isConstraint(constraint));
return Object.entries(constraints).every(
([key, constraint]) => isString(key) && isConstraint(constraint)
);
}
// TODO: sanitize input for further processing as specified by constraints (correct types, trimming, etc.)
function isValidBoolean(value: unknown): boolean {
return isBoolean(value) || value === 'true' || value === 'false';
return isBoolean(value) || value === "true" || value === "false";
}
function isValidNumber(constraint: Constraint, value: unknown): boolean {
@ -86,7 +98,9 @@ function isValidEnum(constraint: Constraint, value: unknown): boolean {
function isValidString(constraint: Constraint, value: unknown): boolean {
if (!constraint.regex) {
throw new Error("String constraints must have regex set: " + constraint);
throw new Error(
"String constraints must have regex set: " + constraint
);
}
if (!isString(value)) {
@ -94,32 +108,43 @@ function isValidString(constraint: Constraint, value: unknown): boolean {
}
const trimmed = value.trim();
return (trimmed === '' && constraint.optional) || constraint.regex.test(trimmed);
return (
(trimmed === "" && constraint.optional) ||
constraint.regex.test(trimmed)
);
}
function isValid(constraint: Constraint, acceptUndefined: boolean, value: unknown): boolean {
function isValid(
constraint: Constraint,
acceptUndefined: boolean,
value: unknown
): boolean {
if (value === undefined) {
return acceptUndefined || constraint.optional === true;
}
switch (constraint.type) {
case 'boolean':
case "boolean":
return isValidBoolean(value);
case 'number':
case "number":
return isValidNumber(constraint, value);
case 'enum':
case "enum":
return isValidEnum(constraint, value);
case 'string':
case "string":
return isValidString(constraint, value);
}
return false;
}
function areValid(constraints: Constraints, acceptUndefined: boolean, values: Values): boolean {
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])) {
@ -136,10 +161,18 @@ function areValid(constraints: Constraints, acceptUndefined: boolean, values: Va
return true;
}
export function forConstraint(constraint: Constraint, acceptUndefined: boolean): (value: unknown) => boolean {
return ((value: unknown): boolean => isValid(constraint, acceptUndefined, value));
export function forConstraint(
constraint: Constraint,
acceptUndefined: boolean
): (value: unknown) => boolean {
return (value: unknown): boolean =>
isValid(constraint, acceptUndefined, value);
}
export function forConstraints(constraints: Constraints, acceptUndefined: boolean): (values: Values) => boolean {
return ((values: Values): boolean => areValid(constraints, acceptUndefined, values));
export function forConstraints(
constraints: Constraints,
acceptUndefined: boolean
): (values: Values) => boolean {
return (values: Values): boolean =>
areValid(constraints, acceptUndefined, values);
}