fix: Add additional model validation (#3725)
This commit is contained in:
27
server/models/validators/Length.ts
Normal file
27
server/models/validators/Length.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { size } from "lodash";
|
||||
import { addAttributeOptions } from "sequelize-typescript";
|
||||
|
||||
/**
|
||||
* A decorator that validates the size of a string based on lodash's size.
|
||||
* function. Useful for strings with unicode characters of variable lengths.
|
||||
*/
|
||||
export default function Length({
|
||||
msg,
|
||||
min,
|
||||
max,
|
||||
}: {
|
||||
msg?: string;
|
||||
min: number;
|
||||
max: number;
|
||||
}): (target: any, propertyName: string) => void {
|
||||
return (target: any, propertyName: string) =>
|
||||
addAttributeOptions(target, propertyName, {
|
||||
validate: {
|
||||
validLength(value: string) {
|
||||
if (size(value) > max || size(value) < min) {
|
||||
throw new Error(msg);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
16
server/models/validators/NotContainsUrl.ts
Normal file
16
server/models/validators/NotContainsUrl.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { addAttributeOptions } from "sequelize-typescript";
|
||||
|
||||
/**
|
||||
* A decorator that validates that a string does not include something that
|
||||
* looks like a URL.
|
||||
*/
|
||||
export default function NotContainsUrl(target: any, propertyName: string) {
|
||||
return addAttributeOptions(target, propertyName, {
|
||||
validate: {
|
||||
not: {
|
||||
args: /(www|file:|http:|https:)+[^\s]+[\w]/,
|
||||
msg: "Must not contain a URL",
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user