|
1 | | -/*eslint-disable no-prototype-builtins*/ |
2 | 1 | 'use strict'; |
3 | 2 |
|
4 | 3 | import { assert } from 'console'; |
5 | 4 | import Store from 'electron-store'; |
6 | 5 | import fs from 'fs'; |
7 | 6 |
|
8 | 7 | import { generateKey } from './date-db-formatter.mjs'; |
9 | | -import TimeMath from './time-math.mjs'; |
| 8 | +import { validateJSON } from './validate-json.mjs'; |
10 | 9 |
|
11 | 10 | /** |
12 | 11 | * Returns the database as an array of: |
@@ -55,45 +54,8 @@ function _getWaivedEntries() |
55 | 54 | return output; |
56 | 55 | } |
57 | 56 |
|
58 | | -function _validateDate(dateStr) |
59 | | -{ |
60 | | - const date = new Date(dateStr); |
61 | | - return date instanceof Date && !Number.isNaN(date.getTime()); |
62 | | -} |
63 | | - |
64 | 57 | class ImportExport |
65 | 58 | { |
66 | | - static validEntry(entry) |
67 | | - { |
68 | | - if (entry.hasOwnProperty('type') && ['waived', 'flexible'].indexOf(entry.type) !== -1) |
69 | | - { |
70 | | - const validatedDate = entry.hasOwnProperty('date') && _validateDate(entry.date); |
71 | | - let hasExpectedProperties; |
72 | | - let validatedTime = true; |
73 | | - if (entry.type === 'flexible') |
74 | | - { |
75 | | - hasExpectedProperties = entry.hasOwnProperty('values') && Array.isArray(entry.values) && entry.values.length > 0; |
76 | | - if (hasExpectedProperties) |
77 | | - { |
78 | | - for (const value of entry.values) |
79 | | - { |
80 | | - validatedTime &= (TimeMath.validateTime(value) || value === '--:--'); |
81 | | - } |
82 | | - } |
83 | | - } |
84 | | - else |
85 | | - { |
86 | | - hasExpectedProperties = entry.hasOwnProperty('data'); |
87 | | - validatedTime = entry.hasOwnProperty('hours') && TimeMath.validateTime(entry.hours); |
88 | | - } |
89 | | - if (hasExpectedProperties && validatedDate && validatedTime) |
90 | | - { |
91 | | - return true; |
92 | | - } |
93 | | - } |
94 | | - return false; |
95 | | - } |
96 | | - |
97 | 59 | static exportDatabaseToFile(filename) |
98 | 60 | { |
99 | 61 | let information = _getEntries(); |
@@ -122,7 +84,7 @@ class ImportExport |
122 | 84 | for (let i = 0; i < information.length; ++i) |
123 | 85 | { |
124 | 86 | const entry = information[i]; |
125 | | - if (!ImportExport.validEntry(entry)) |
| 87 | + if (!validateJSON([entry])) |
126 | 88 | { |
127 | 89 | failedEntries += 1; |
128 | 90 | continue; |
|
0 commit comments