-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateImagesArray.js
43 lines (37 loc) · 1.11 KB
/
validateImagesArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function validateImagesArray(payload) {
const errors = [];
if (!Array.isArray(payload)) {
errors.push("Payload is not an array");
return errors;
}
payload.forEach((item, index) => {
// Check if item is an object
if (typeof item !== "object" || item === null) {
errors.push(`Item at index ${index} is not an object`);
return; // Skip further checks for this item
}
// Check alt
if (
!Object.prototype.hasOwnProperty.call(item, "alt") ||
typeof item.alt !== "string"
) {
errors.push(`Item at index ${index} has an invalid 'alt'`);
}
// Check image_url
if (
!Object.prototype.hasOwnProperty.call(item, "image_url") ||
typeof item.image_url !== "string"
) {
errors.push(`Item at index ${index} has an invalid 'image_url'`);
}
// Check caption (optional)
if (
Object.prototype.hasOwnProperty.call(item, "caption") &&
typeof item.caption !== "string"
) {
errors.push(`Item at index ${index} has an invalid 'caption'`);
}
});
return errors;
}
exports.validateImagesArray = validateImagesArray;