2020-01-02 16:58:06 +00:00
|
|
|
/*
|
2021-01-30 12:32:46 +00:00
|
|
|
* Functions for validation of user input
|
|
|
|
* This gets used on server and on the client.
|
|
|
|
*
|
|
|
|
* On the server the return values will be again translated with gettext
|
|
|
|
* which could be a bit questionable, but it is preferable to write this file
|
|
|
|
* two times imho.
|
|
|
|
*
|
2020-01-02 16:58:06 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-30 21:36:53 +00:00
|
|
|
import { t } from 'ttag';
|
2021-01-30 12:32:46 +00:00
|
|
|
|
2020-01-09 16:09:29 +00:00
|
|
|
// eslint-disable-next-line no-useless-escape, max-len
|
2020-01-03 03:12:21 +00:00
|
|
|
const mailTester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;
|
2020-01-02 16:58:06 +00:00
|
|
|
|
|
|
|
export function validateEMail(email) {
|
2021-01-30 12:32:46 +00:00
|
|
|
if (!email) return t`Email can't be empty.`;
|
|
|
|
if (email.length < 5) return t`Email should be at least 5 characters long.`;
|
|
|
|
if (email.length > 40) return t`Email can't be longer than 40 characters.`;
|
|
|
|
if (email.indexOf('.') === -1) return t`Email should at least contain a dot`;
|
2020-01-04 06:00:47 +00:00
|
|
|
if (email.split('').filter((x) => x === '@').length !== 1) {
|
2021-01-30 12:32:46 +00:00
|
|
|
return t`Email should contain a @`;
|
2020-01-03 03:12:21 +00:00
|
|
|
}
|
|
|
|
if (!mailTester.test(email)) return 'Your Email looks shady';
|
2020-01-02 16:58:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateName(name) {
|
2021-01-30 12:32:46 +00:00
|
|
|
if (!name) return t`Name can't be empty.`;
|
2022-03-28 15:34:03 +00:00
|
|
|
if (name.length < 2) return t`Name must be at least 2 characters long`;
|
2021-01-30 12:32:46 +00:00
|
|
|
if (name.length > 26) return t`Name must be shorter than 26 characters`;
|
2020-01-04 06:00:47 +00:00
|
|
|
if (name.indexOf('@') !== -1
|
|
|
|
|| name.indexOf('/') !== -1
|
|
|
|
|| name.indexOf('\\') !== -1
|
|
|
|
|| name.indexOf('>') !== -1
|
|
|
|
|| name.indexOf('<') !== -1
|
|
|
|
|| name.indexOf('#') !== -1) {
|
2021-01-30 12:32:46 +00:00
|
|
|
return t`Name contains invalid character like @, /, \\ or #`;
|
2020-01-03 03:12:21 +00:00
|
|
|
}
|
2020-01-02 16:58:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sanitizeName(name) {
|
|
|
|
name = name.substring(0, 25);
|
|
|
|
// just sanitizes @ for now, other characters do not seem
|
|
|
|
// problematic, even thought that we rule them out in validateName
|
|
|
|
name = name.replace(/@/g, 'at');
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validatePassword(password) {
|
2021-01-30 12:32:46 +00:00
|
|
|
if (!password) {
|
|
|
|
return t`No password given.`;
|
|
|
|
}
|
2020-01-03 03:12:21 +00:00
|
|
|
if (password.length < 6) {
|
2021-01-30 12:32:46 +00:00
|
|
|
return t`Password must be at least 6 characters long.`;
|
2020-01-03 03:12:21 +00:00
|
|
|
}
|
|
|
|
if (password.length > 60) {
|
2021-01-30 12:32:46 +00:00
|
|
|
return t`Password must be shorter than 60 characters.`;
|
2020-01-03 03:12:21 +00:00
|
|
|
}
|
2020-01-02 16:58:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-28 15:34:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* validate an area given by top-left and bottom-right corner coords
|
|
|
|
* @param ulcoor coords in x_y format, top-left corner
|
|
|
|
* @param brcoor coords in x_y format, bottom-right corner
|
|
|
|
* @param canvasSize dimension of canvas, integer
|
|
|
|
* @return [x, y, u, v] Corner coords if success, error string is failure
|
|
|
|
*/
|
|
|
|
export function validateCoorRange(ulcoor, brcoor, canvasSize) {
|
|
|
|
if (!ulcoor || !brcoor) {
|
|
|
|
return 'Not all coordinates defined';
|
|
|
|
}
|
|
|
|
|
|
|
|
let splitCoords = ulcoor.trim().split('_');
|
|
|
|
if (splitCoords.length !== 2) {
|
|
|
|
return 'Invalid Coordinate Format for top-left corner';
|
|
|
|
}
|
|
|
|
const [x, y] = splitCoords.map((z) => Math.floor(Number(z)));
|
|
|
|
splitCoords = brcoor.trim().split('_');
|
|
|
|
if (splitCoords.length !== 2) {
|
|
|
|
return 'Invalid Coordinate Format for bottom-right corner';
|
|
|
|
}
|
|
|
|
const [u, v] = splitCoords.map((z) => Math.floor(Number(z)));
|
|
|
|
|
|
|
|
let error = null;
|
|
|
|
if (Number.isNaN(x)) {
|
|
|
|
error = 'x of top-left corner is not a valid number';
|
|
|
|
} else if (Number.isNaN(y)) {
|
|
|
|
error = 'y of top-left corner is not a valid number';
|
|
|
|
} else if (Number.isNaN(u)) {
|
|
|
|
error = 'x of bottom-right corner is not a valid number';
|
|
|
|
} else if (Number.isNaN(v)) {
|
|
|
|
error = 'y of bottom-right corner is not a valid number';
|
|
|
|
} else if (u < x || v < y) {
|
|
|
|
error = 'Corner coordinates are alligned wrong';
|
|
|
|
}
|
|
|
|
if (error !== null) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const canvasMaxXY = canvasSize / 2;
|
|
|
|
const canvasMinXY = -canvasMaxXY;
|
|
|
|
if (x < canvasMinXY || y < canvasMinXY
|
|
|
|
|| x >= canvasMaxXY || y >= canvasMaxXY) {
|
|
|
|
return 'Coordinates of top-left corner are outside of canvas';
|
|
|
|
}
|
|
|
|
if (u < canvasMinXY || v < canvasMinXY
|
|
|
|
|| u >= canvasMaxXY || v >= canvasMaxXY) {
|
|
|
|
return 'Coordinates of bottom-right corner are outside of canvas';
|
|
|
|
}
|
|
|
|
|
|
|
|
return [x, y, u, v];
|
|
|
|
}
|