-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverifiers.js
More file actions
51 lines (37 loc) · 1.67 KB
/
verifiers.js
File metadata and controls
51 lines (37 loc) · 1.67 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
50
51
const CONFIG = require("./config")
function isValidDate(date) {
const dateRegex = /^\d{4}-\d{2}-\d{2}$/
if (!dateRegex.test(date)) return false
const [year, month, day] = date.split("-").map(Number)
if (month < 1 || month > 12 || day < 1 || day > 31) return false
const parsedDate = new Date(year, month - 1, day)
return parsedDate.getFullYear() === year &&
parsedDate.getMonth() + 1 === month &&
parsedDate.getDate() === day
}
function isValidName(name) {
return typeof name === "string" && name.trim().length > 0 && /^[a-zA-ZáéíóúÁÉÍÓÚñÑ\s]+$/.test(name)
}
function isValidID(id) {
return /^\d+$/.test(id) && parseInt(id, 10) > 0
}
function verifyLine(line) {
const parts = line.split(CONFIG.SEPARATOR).map(item => item.trim())
if (parts.length !== 3) return { errors: ["Invalid format"], severity: "CRITICAL", criticalCount: 1, warningCount: 0 }
const [date, name, id] = parts
const errors = []
let criticalCount = 0
let warningCount = 0
const validateError = (condition, errorMessage) => {
if (condition) {
errors.push(errorMessage)
CONFIG.ERROR_LEVELS.CRITICAL.includes(errorMessage) ? criticalCount++ : warningCount++
}
}
validateError(!isValidDate(date), "Invalid date")
validateError(!isValidName(name), "Invalid name")
validateError(!isValidID(id), "Invalid ID")
const severity = criticalCount > 0 ? "CRITICAL" : "WARNING"
return errors.length > 0 ? { errors, severity, criticalCount, warningCount } : { valid: true }
}
module.exports = { isValidDate, isValidName, isValidID, verifyLine }