Notes/src/etapi/validators.ts

123 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-04-07 14:59:40 +03:00
import noteTypeService = require('../services/note_types');
import dateUtils = require('../services/date_utils');
2022-01-12 19:32:23 +01:00
2024-04-07 15:13:34 +03:00
function mandatory(obj: ValidatorArg) {
2024-04-07 14:59:40 +03:00
if (obj === undefined) {
2022-01-12 19:32:23 +01:00
return `mandatory, but not set`;
2022-01-07 19:33:59 +01:00
}
}
2024-04-07 15:13:34 +03:00
function notNull(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === null) {
return `cannot be null`;
}
}
2024-04-07 15:13:34 +03:00
function isString(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2022-01-12 19:32:23 +01:00
if (typeof obj !== 'string') {
return `'${obj}' is not a string`;
2022-01-07 19:33:59 +01:00
}
}
2024-04-07 15:13:34 +03:00
function isLocalDateTime(obj: ValidatorArg) {
2024-04-07 14:59:40 +03:00
if (obj === undefined || obj === null || typeof obj !== "string") {
return;
}
return dateUtils.validateLocalDateTime(obj);
}
2024-04-07 15:13:34 +03:00
function isUtcDateTime(obj: ValidatorArg) {
2024-04-07 14:59:40 +03:00
if (obj === undefined || obj === null || typeof obj !== "string") {
return;
}
return dateUtils.validateUtcDateTime(obj);
}
2024-04-07 15:13:34 +03:00
function isBoolean(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2022-01-07 19:33:59 +01:00
if (typeof obj !== 'boolean') {
return `'${obj}' is not a boolean`;
}
}
2024-04-07 15:13:34 +03:00
function isInteger(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2022-01-07 19:33:59 +01:00
if (!Number.isInteger(obj)) {
return `'${obj}' is not an integer`;
}
}
2024-04-07 15:13:34 +03:00
function isNoteId(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
const becca = require('../becca/becca');
2022-01-12 19:32:23 +01:00
if (typeof obj !== 'string') {
return `'${obj}' is not a valid noteId`;
}
2022-01-12 19:32:23 +01:00
if (!(obj in becca.notes)) {
return `Note '${obj}' does not exist`;
}
}
2024-04-07 15:13:34 +03:00
function isNoteType(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2022-12-16 16:00:49 +01:00
const noteTypes = noteTypeService.getNoteTypeNames();
2024-04-07 14:59:40 +03:00
if (typeof obj !== "string" || !noteTypes.includes(obj)) {
return `'${obj}' is not a valid note type, allowed types are: ${noteTypes.join(", ")}`;
2022-01-12 19:32:23 +01:00
}
}
2024-04-07 15:13:34 +03:00
function isAttributeType(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2024-04-07 14:59:40 +03:00
if (typeof obj !== "string" || !['label', 'relation'].includes(obj)) {
2022-01-12 19:32:23 +01:00
return `'${obj}' is not a valid attribute type, allowed types are: label, relation`;
}
}
2024-04-07 15:13:34 +03:00
function isValidEntityId(obj: ValidatorArg) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
if (typeof obj !== 'string' || !/^[A-Za-z0-9_]{4,128}$/.test(obj)) {
2022-01-12 19:32:23 +01:00
return `'${obj}' is not a valid entityId. Only alphanumeric characters are allowed of length 4 to 32.`;
}
}
2024-04-07 14:59:40 +03:00
export = {
2022-01-12 19:32:23 +01:00
mandatory,
notNull,
2022-01-07 19:33:59 +01:00
isString,
isBoolean,
2022-01-12 19:32:23 +01:00
isInteger,
isNoteId,
isNoteType,
isAttributeType,
isValidEntityId,
isLocalDateTime,
isUtcDateTime
};