-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidate.js
73 lines (69 loc) · 1.97 KB
/
Validate.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { errorHandler } = require("./error/error");
class Validate {
/**
* Validate the data string for create operation
* @param {*} key
* @param {*} value
* @param {*} data
*/
static async createValidate(key, value, data, flag) {
if (data.length === 0 || Object.keys(JSON.parse(data)).length === 0) {
return { [key]: { value: value, expire: flag } };
} else {
const parsedData = JSON.parse(data);
if (key in parsedData.root) {
return errorHandler(
"Key already exists!! Cannot create a value for already existing key.."
);
} else {
const { root } = parsedData;
root[key] = { value: value, expire: flag };
return root;
}
}
}
/**
* Validate the key TTL, parsed data length and key present in the file or not for read operation
* @param {*} key
* @param {*} parsedData
*/
static async readValidate(key, parsedData) {
if (parsedData === " ") {
return errorHandler(
"File is empty!! Create an object to perform read operation..."
);
} else {
if (key in parsedData.root) {
if (parsedData.root[key].expire) {
return errorHandler("Key exceeded Time To Live");
}
return parsedData.root[key].value;
} else {
return errorHandler("Invalid key...");
}
}
}
/**
* Validate the delete key presence and TTL property to perform delete operation
* @param {*} key
* @param {*} parsedData
*/
static async deleteValidate(key, parsedData) {
if (parsedData === " ") {
return errorHandler(
"File is empty!! Create an object to perform delete operation..."
);
} else {
if (key in parsedData.root) {
if (parsedData.root[key].expire) {
return errorHandler("Key exceeded Time To Live");
} else {
return true;
}
} else {
return errorHandler("Invalid key...");
}
}
}
}
exports.Validate = Validate;