Skip to content

Commit 581dd22

Browse files
committed
migration names validate correct days in months
1 parent a8e9af6 commit 581dd22

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

scripts/analyzeMigrationNames.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ const regDate = /^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])_/
2121
// - underscore
2222
const regPrefix = /^\d{8}_\d{2}_/
2323

24-
// Correct date + prefix and filename snake cased
24+
// Full migration name format:
25+
// - YYYYMMDD_NN_
26+
// - then any filename-safe characters (no slashes)
2527
const regFull = /^(?:\d{4})(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])_\d{2}_[^\\/]+$/
2628

29+
// Date validity check using JS Date
30+
function isValidDate(year, month, day) {
31+
const d = new Date(year, month - 1, day)
32+
return d.getFullYear() === year && d.getMonth() === month - 1 && d.getDate() === day
33+
}
34+
2735
const MIGRATIONS_PATH = './src/server/db/migrations'
2836
const dirPath = path.join(process.cwd(), MIGRATIONS_PATH)
2937
const files = fs.readdirSync(dirPath)
@@ -35,10 +43,21 @@ for (const file of files) {
3543
errors.push(`${FgRed}${Bright}${Reset} ${file} ${FgYellow}→ must start with YYYYMMDD_NN_${Reset}`)
3644
continue
3745
}
38-
if (!regDate.test(file)) {
46+
47+
const match = file.match(regDate)
48+
if (!match) {
3949
errors.push(`${FgRed}${Bright}${Reset} ${file} ${FgYellow}→ invalid date format${Reset}`)
4050
continue
51+
} else {
52+
const year = parseInt(match[1], 10)
53+
const month = parseInt(match[2], 10)
54+
const day = parseInt(match[3], 10)
55+
if (!isValidDate(year, month, day)) {
56+
errors.push(`${FgRed}${Bright}${Reset} ${file} ${FgYellow}→ impossible calendar date${Reset}`)
57+
continue
58+
}
4159
}
60+
4261
if (!regFull.test(file)) {
4362
errors.push(`${FgRed}${Bright}${Reset} ${file} ${FgYellow}→ contains invalid characters${Reset}`)
4463
}

0 commit comments

Comments
 (0)