Notes/src/etapi/validators.js

123 lines
2.5 KiB
JavaScript
Raw Normal View History

const noteTypeService = require('../services/note_types.js');
2024-02-16 21:38:09 +02:00
const dateUtils = require('../services/date_utils');
2022-01-12 19:32:23 +01:00
function mandatory(obj) {
if (obj === undefined ) {
return `mandatory, but not set`;
2022-01-07 19:33:59 +01:00
}
}
2022-01-12 19:32:23 +01:00
function notNull(obj) {
if (obj === null) {
return `cannot be null`;
}
}
function isString(obj) {
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
}
}
function isLocalDateTime(obj) {
if (obj === undefined || obj === null) {
return;
}
return dateUtils.validateLocalDateTime(obj);
}
function isUtcDateTime(obj) {
if (obj === undefined || obj === null) {
return;
}
return dateUtils.validateUtcDateTime(obj);
}
2022-01-07 19:33:59 +01:00
function isBoolean(obj) {
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`;
}
}
function isInteger(obj) {
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`;
}
}
2022-01-12 19:32:23 +01:00
function isNoteId(obj) {
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`;
}
}
function isNoteType(obj) {
if (obj === undefined || obj === null) {
return;
}
2022-12-16 16:00:49 +01:00
const noteTypes = noteTypeService.getNoteTypeNames();
2022-01-12 19:32:23 +01:00
if (!noteTypes.includes(obj)) {
return `'${obj}' is not a valid note type, allowed types are: ${noteTypes.join(", ")}`;
2022-01-12 19:32:23 +01:00
}
}
function isAttributeType(obj) {
if (obj === undefined || obj === null) {
return;
}
if (!['label', 'relation'].includes(obj)) {
return `'${obj}' is not a valid attribute type, allowed types are: label, relation`;
}
}
function isValidEntityId(obj) {
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.`;
}
}
2022-01-07 19:33:59 +01:00
module.exports = {
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
};