fix: Improved model validation for Collection (#3749)
* fix: Added improved model validation for Collection attributes * sp * fix: Enforce title length in UI
This commit is contained in:
@@ -20,6 +20,7 @@ import {
|
||||
DataType,
|
||||
} from "sequelize-typescript";
|
||||
import isUUID from "validator/lib/isUUID";
|
||||
import { MAX_TITLE_LENGTH } from "@shared/constants";
|
||||
import { sortNavigationNodes } from "@shared/utils/collections";
|
||||
import { SLUG_URL_REGEX } from "@shared/utils/urlHelpers";
|
||||
import slugify from "@server/utils/slugify";
|
||||
@@ -33,6 +34,8 @@ import Team from "./Team";
|
||||
import User from "./User";
|
||||
import ParanoidModel from "./base/ParanoidModel";
|
||||
import Fix from "./decorators/Fix";
|
||||
import IsHexColor from "./validators/IsHexColor";
|
||||
import Length from "./validators/Length";
|
||||
import NotContainsUrl from "./validators/NotContainsUrl";
|
||||
|
||||
// without this indirection, the app crashes on starup
|
||||
@@ -133,18 +136,35 @@ class Collection extends ParanoidModel {
|
||||
urlId: string;
|
||||
|
||||
@NotContainsUrl
|
||||
@Length({
|
||||
max: MAX_TITLE_LENGTH,
|
||||
msg: `Collection name must be less than ${MAX_TITLE_LENGTH} characters`,
|
||||
})
|
||||
@Column
|
||||
name: string;
|
||||
|
||||
@Length({
|
||||
max: 1000,
|
||||
msg: `Collection description must be less than 1000 characters`,
|
||||
})
|
||||
@Column
|
||||
description: string;
|
||||
|
||||
@Length({
|
||||
max: 50,
|
||||
msg: `Collection icon must be less than 50 characters`,
|
||||
})
|
||||
@Column
|
||||
icon: string | null;
|
||||
|
||||
@IsHexColor
|
||||
@Column
|
||||
color: string | null;
|
||||
|
||||
@Length({
|
||||
max: 50,
|
||||
msg: `Collection index must be less than 50 characters`,
|
||||
})
|
||||
@Column
|
||||
index: string | null;
|
||||
|
||||
|
||||
@@ -188,7 +188,6 @@ class Document extends ParanoidModel {
|
||||
urlId: string;
|
||||
|
||||
@Length({
|
||||
min: 0,
|
||||
max: MAX_TITLE_LENGTH,
|
||||
msg: `Document title must be less than ${MAX_TITLE_LENGTH} characters`,
|
||||
})
|
||||
|
||||
@@ -84,21 +84,17 @@ export enum UserFlag {
|
||||
@Fix
|
||||
class User extends ParanoidModel {
|
||||
@IsEmail
|
||||
@Length({ min: 0, max: 255, msg: "email must be less than 255 characters" })
|
||||
@Length({ max: 255, msg: "User email must be less than 255 characters" })
|
||||
@Column
|
||||
email: string | null;
|
||||
|
||||
@NotContainsUrl
|
||||
@Length({
|
||||
min: 0,
|
||||
max: 255,
|
||||
msg: "username must be less than 255 characters",
|
||||
})
|
||||
@Length({ max: 255, msg: "User username must be less than 255 characters" })
|
||||
@Column
|
||||
username: string | null;
|
||||
|
||||
@NotContainsUrl
|
||||
@Length({ min: 0, max: 255, msg: "name must be less than 255 characters" })
|
||||
@Length({ max: 255, msg: "User name must be less than 255 characters" })
|
||||
@Column
|
||||
name: string;
|
||||
|
||||
|
||||
@@ -22,13 +22,13 @@ import Length from "./validators/Length";
|
||||
@Fix
|
||||
class WebhookSubscription extends ParanoidModel {
|
||||
@NotEmpty
|
||||
@Length({ min: 0, max: 255, msg: "Must be less than 255 characters" })
|
||||
@Length({ max: 255, msg: "Webhook name be less than 255 characters" })
|
||||
@Column
|
||||
name: string;
|
||||
|
||||
@IsUrl
|
||||
@NotEmpty
|
||||
@Length({ min: 0, max: 255, msg: "Must be less than 255 characters" })
|
||||
@Length({ max: 255, msg: "Webhook url be less than 255 characters" })
|
||||
@Column
|
||||
url: string;
|
||||
|
||||
|
||||
17
server/models/validators/IsHexColor.ts
Normal file
17
server/models/validators/IsHexColor.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { isHexColor } from "class-validator";
|
||||
import { addAttributeOptions } from "sequelize-typescript";
|
||||
|
||||
/**
|
||||
* A decorator that validates that a string is a valid hex color code.
|
||||
*/
|
||||
export default function IsHexColor(target: any, propertyName: string) {
|
||||
return addAttributeOptions(target, propertyName, {
|
||||
validate: {
|
||||
validDomain(value: string) {
|
||||
if (!isHexColor(value)) {
|
||||
throw new Error("Must be a valid hex color code");
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -7,11 +7,11 @@ import { addAttributeOptions } from "sequelize-typescript";
|
||||
*/
|
||||
export default function Length({
|
||||
msg,
|
||||
min,
|
||||
min = 0,
|
||||
max,
|
||||
}: {
|
||||
msg?: string;
|
||||
min: number;
|
||||
min?: number;
|
||||
max: number;
|
||||
}): (target: any, propertyName: string) => void {
|
||||
return (target: any, propertyName: string) =>
|
||||
|
||||
Reference in New Issue
Block a user