Skip to content

Build your personal knowledge base with Trilium Notes

2.5 KB
Raw Blame
import { ALLOWED_NOTE_TYPES, isNoteType as isKnownNoteType } from "@triliumnext/commons";
import { becca, date_utils as dateUtils, utils } from "@triliumnext/core";

function mandatory(obj: unknown) {
    if (obj === undefined) {
        return `mandatory, but not set`;
    }
}

function notNull(obj: unknown) {
    if (obj === null) {
        return `cannot be null`;
    }
}

function isString(obj: unknown) {
    if (obj === undefined || obj === null) {
        return;
    }

    if (typeof obj !== "string") {
        return `'${obj}' is not a string`;
    }
}

function isLocalDateTime(obj: unknown) {
    if (typeof obj !== "string") {
        return;
    }

    return dateUtils.validateLocalDateTime(obj);
}

function isUtcDateTime(obj: unknown) {
    if (typeof obj !== "string") {
        return;
    }

    return dateUtils.validateUtcDateTime(obj);
}

function isBoolean(obj: unknown) {
    if (obj === undefined || obj === null) {
        return;
    }

    if (typeof obj !== "boolean") {
        return `'${obj}' is not a boolean`;
    }
}

function isInteger(obj: unknown) {
    if (obj === undefined || obj === null) {
        return;
    }

    if (!Number.isInteger(obj)) {
        return `'${obj}' is not an integer`;
    }
}

function isNoteId(obj: unknown) {
    if (obj === undefined || obj === null) {
        return;
    }

    if (typeof obj !== "string") {
        return `'${obj}' is not a valid noteId`;
    }

    if (!(obj in becca.notes)) {
        return `Note '${obj}' does not exist`;
    }
}

function isNoteType(obj: unknown) {
    if (obj === undefined || obj === null) {
        return;
    }

    if (!isKnownNoteType(obj)) {
        return `'${obj}' is not a valid note type, allowed types are: ${ALLOWED_NOTE_TYPES.join(", ")}`;
    }
}

function isAttributeType(obj: unknown) {
    if (obj === undefined || obj === null) {
        return;
    }

    if (typeof obj !== "string" || !["label", "relation"].includes(obj)) {
        return `'${obj}' is not a valid attribute type, allowed types are: label, relation`;
    }
}

function isValidEntityId(obj: unknown) {
    if (obj === undefined || obj === null) {
        return;
    }

    if (typeof obj !== "string" || !utils.isValidEntityId(obj)) {
        return `'${obj}' is not a valid entityId. Only letters, digits and underscores are allowed, with a length of 4 to 128.`;
    }
}

export default {
    mandatory,
    notNull,
    isString,
    isBoolean,
    isInteger,
    isNoteId,
    isNoteType,
    isAttributeType,
    isValidEntityId,
    isLocalDateTime,
    isUtcDateTime
};