generated from nhcarrigan/template
feat: validate book image correctly
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
validateUrl,
|
||||
validateRating,
|
||||
validateStringLength,
|
||||
validateDataUrl,
|
||||
MAX_LENGTHS,
|
||||
} from "../utils/validation";
|
||||
|
||||
@@ -44,9 +45,23 @@ export class BookService {
|
||||
throw new Error("Rating must be an integer between 0 and 10.");
|
||||
}
|
||||
|
||||
// Validate cover image URL
|
||||
if (data.coverImage && !validateUrl(data.coverImage)) {
|
||||
throw new Error("Invalid cover image URL. Only http and https URLs are allowed.");
|
||||
if (data.coverImage) {
|
||||
if (data.coverImage.startsWith("data:")) {
|
||||
const sizeInBytes = data.coverImage.length * 0.75;
|
||||
if (sizeInBytes > MAX_LENGTHS.IMAGE_DATA) {
|
||||
throw new Error("Cover image must be under 5MB.");
|
||||
}
|
||||
if (!validateDataUrl(data.coverImage)) {
|
||||
throw new Error("Invalid image data URL.");
|
||||
}
|
||||
} else {
|
||||
if (!validateStringLength(data.coverImage, MAX_LENGTHS.URL)) {
|
||||
throw new Error(`Cover image URL must be ${MAX_LENGTHS.URL} characters or less.`);
|
||||
}
|
||||
if (!validateUrl(data.coverImage)) {
|
||||
throw new Error("Invalid cover image URL. Only http and https URLs are allowed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate tags
|
||||
|
||||
Reference in New Issue
Block a user