Notes/apps/server/src/etapi/validators.ts

122 lines
2.6 KiB
TypeScript
Raw Normal View History

import noteTypeService from "../services/note_types.js";
import dateUtils from "../services/date_utils.js";
import becca from "../becca/becca.js";
2022-01-12 19:32:23 +01:00
2024-04-18 21:26:29 +03:00
function mandatory(obj: unknown) {
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-18 21:26:29 +03:00
function notNull(obj: unknown) {
2022-01-12 19:32:23 +01:00
if (obj === null) {
return `cannot be null`;
}
}
2024-04-18 21:26:29 +03:00
function isString(obj: unknown) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2025-01-09 18:07:02 +02:00
if (typeof obj !== "string") {
2022-01-12 19:32:23 +01:00
return `'${obj}' is not a string`;
2022-01-07 19:33:59 +01:00
}
}
2024-04-18 21:26:29 +03:00
function isLocalDateTime(obj: unknown) {
2024-04-16 21:10:39 +03:00
if (typeof obj !== "string") {
return;
}
return dateUtils.validateLocalDateTime(obj);
}
2024-04-18 21:26:29 +03:00
function isUtcDateTime(obj: unknown) {
2024-04-16 21:10:39 +03:00
if (typeof obj !== "string") {
return;
}
return dateUtils.validateUtcDateTime(obj);
}
2024-04-18 21:26:29 +03:00
function isBoolean(obj: unknown) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2025-01-09 18:07:02 +02:00
if (typeof obj !== "boolean") {
2022-01-07 19:33:59 +01:00
return `'${obj}' is not a boolean`;
}
}
2024-04-18 21:26:29 +03:00
function isInteger(obj: unknown) {
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-18 21:26:29 +03:00
function isNoteId(obj: unknown) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2025-01-09 18:07:02 +02:00
if (typeof obj !== "string") {
2022-01-12 19:32:23 +01:00
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-18 21:26:29 +03:00
function isNoteType(obj: unknown) {
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-18 21:26:29 +03:00
function isAttributeType(obj: unknown) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2025-01-09 18:07:02 +02: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-18 21:26:29 +03:00
function isValidEntityId(obj: unknown) {
2022-01-12 19:32:23 +01:00
if (obj === undefined || obj === null) {
return;
}
2025-01-09 18:07:02 +02:00
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.`;
}
}
export default {
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
};