-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
49 lines (37 loc) · 1.56 KB
/
validation.js
File metadata and controls
49 lines (37 loc) · 1.56 KB
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
44
45
46
47
48
49
export function validateForm(data){
const errors = [];
// {
// title: 'test',
// price: '10',
// original_price: '5',
// deal_url: 'https://github.com/akurfurst/CodeRelic-GameDealFinder',
// promo_code: 'ffff',
// expiry_date: '2026-04-01',
// platform: [ 'steam' ]
// }
//if no title
if(data.title.trim() == "") errors.push("Title is required");
//if title is too long
if(data.title.length >= 50) errors.push("Title too long");
//if no price
if(data.price.trim() == "") errors.push("Price is required");
//if price is negative
if(parseFloat(data.price) < 0) errors.push("Price cannot be negative");
//if no original price
if(data.original_price == "") errors.push("Original Price is required");
//if original price is smaller than price
if(parseFloat(data.original_price) <= parseFloat(data.price)) errors.push("Original price must be more than discounted price");
//check if valid html link
if(!data.deal_url.startsWith("https://")) errors.push("Invlaid link");
//if deal link is too long
if(data.deal_url.length >= 200) errors.push("Deal link too long");
//if promo code is too long
if(data.promo_code.length >= 50) errors.push("Promo code is too long");
//make sure platform is valid
const platforms = ["steam", "xbox", "sony", "epic", "nintendo"];
if(!data.platform || data.platform.length === 0) errors.push("At least one platform is required");
return{
isValid: errors.length === 0,
errors
};
}