-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreference.ts
More file actions
executable file
·294 lines (265 loc) · 9.57 KB
/
Copy pathreference.ts
File metadata and controls
executable file
·294 lines (265 loc) · 9.57 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { EditDataType, ReferenceDetailsType, ReferenceAuthorType, ReferenceJournalType } from '../types'
import { Validators, validateFields, validator, ValidationError } from './validator'
const authorCheck: (data: ReferenceAuthorType[]) => ValidationError = (data: ReferenceAuthorType[]) => {
if (data.length === 0) {
return 'There must be at least one author'
}
//Check that there's at least one author that's not going to be deleted
let nonRemovedAuthor = false
for (const author of data) {
if (author.rowState && author.rowState !== 'removed') {
nonRemovedAuthor = true
break
}
if (!author.rowState) {
nonRemovedAuthor = true
break
}
}
if (!nonRemovedAuthor) {
return 'There must be at least one author'
}
const errors: string[] = []
for (const author of data) {
if (typeof author.au_num !== 'number' || author.au_num < 0 || author.au_num > data.length) {
const str = 'Something failed with author numbering'
if (!errors.includes(str)) {
errors.push(str)
}
}
if (typeof author.author_initials !== 'string' || author.author_initials.length < 1) {
const str = 'Author initials must be a non-empty string'
if (!errors.includes(str)) {
errors.push(str)
}
}
if (typeof author.author_surname !== 'string' || author.author_surname.length < 1) {
const str = 'Author surname must be a non-empty string'
if (!errors.includes(str)) {
errors.push(str)
}
}
if (typeof author.field_id !== 'number' || author.field_id < 0) {
const str = 'Something failed with setting author field_id'
if (!errors.includes(str)) {
errors.push(str)
}
}
}
if (errors.length > 0) {
return ('Authors gave the following errors: ' + errors.join(', ')) as ValidationError
}
return null as ValidationError
}
const journalCheck: (journal: ReferenceJournalType) => ValidationError = (journal: ReferenceJournalType) => {
if (!journal) {
return 'You must select or create a new journal' as ValidationError
}
//Existing journal can't have rowState 'removed' if journal is mandatory
if (journal.rowState && journal.rowState == 'removed') {
return 'You must select or create a new journal' as ValidationError
}
if (typeof journal.journal_title !== 'string' || journal.journal_title?.length < 1) {
return 'Journal must have a title' as ValidationError
}
return null as ValidationError
}
const dateCheck: (dateString: string) => ValidationError = (dateString: string) => {
// Regular expression to match yyyy-MM-dd format
const regex = /^\d{4}-\d{2}-\d{2}$/
if (!regex.test(dateString)) {
return 'Date must be in the format yyyy-MM-dd'
}
const [year, month, day] = dateString.split('-').map(Number)
if (month < 1 || month > 12) {
return 'Month must be between 01 and 12'
}
const maxDaysInMonth = new Date(year, month, 0).getDate()
if (day < 1 || day > maxDaysInMonth) {
return `Day must be between 01 and ${maxDaysInMonth} for month ${month}`
}
return null
}
const yearCheck: (year: number) => ValidationError = (year: number) => {
if (!Number.isFinite(year)) {
return 'Year must be a valid number'
}
if (!Number.isInteger(year)) {
return 'Year must be a whole number'
}
if (year <= 0) {
return 'Year must be a positive integer'
}
const currentYear = new Date().getFullYear()
if (year > currentYear) {
return `Year cannot be in the future (max ${currentYear})`
}
return null
}
const positiveIntegerCheck: (name: string, num: number) => ValidationError = (name: string, num: number) => {
if (!Number.isFinite(num)) return `${name} must be a valid number`
if (!Number.isInteger(num)) return `${name} must be a whole number`
if (num < 1) return `${name} must be a positive integer`
return null
}
export type ReferenceFieldDisplayNames = Partial<Record<keyof EditDataType<ReferenceDetailsType>, string>>
export type ReferenceDisplayLabelMap = Partial<Record<number, ReferenceFieldDisplayNames>>
type ReferenceValidationOptions = {
displayLabelMap?: ReferenceDisplayLabelMap
}
const getDisplayLabels = (
fields: (keyof EditDataType<ReferenceDetailsType>)[],
refTypeId: number | null,
displayLabelMap?: ReferenceDisplayLabelMap
) => {
if (!refTypeId || !displayLabelMap?.[refTypeId]) return fields.map(field => String(field))
const labelMap = displayLabelMap[refTypeId]
return fields.map(field => labelMap?.[field] ?? String(field))
}
const orCheck = (
data: EditDataType<ReferenceDetailsType>,
fieldName: keyof EditDataType<ReferenceDetailsType>,
options?: ReferenceValidationOptions
): ValidationError => {
let fields: (keyof EditDataType<ReferenceDetailsType>)[] = []
if (data.ref_type_id && data.ref_type_id === 2) {
fields = ['title_primary', 'title_series', 'gen_notes']
}
if (data.ref_type_id && [1, 3, 5, 8, 9, 11, 14].includes(data.ref_type_id)) {
fields = ['title_primary', 'title_secondary', 'title_series', 'gen_notes']
}
if (data.ref_type_id && [4, 7, 12, 13].includes(data.ref_type_id)) {
fields = ['title_primary', 'gen_notes']
}
if (data.ref_type_id && data.ref_type_id === 6) {
fields = ['title_primary', 'title_secondary', 'gen_notes']
}
if (data.ref_type_id && data.ref_type_id === 10) {
fields = ['gen_notes']
}
if (!fields.includes(fieldName)) return null
const hasValue = fields.some(field => {
const value = data[field]
return value != null && typeof value === 'string' && value.length > 0
})
if (!hasValue) {
const displayLabels = getDisplayLabels(fields, data.ref_type_id ?? null, options?.displayLabelMap)
return `At least one of the following fields is required: ${displayLabels.join(', ')}`
}
return null
}
const createReferenceValidators = (
editData: EditDataType<ReferenceDetailsType>,
options?: ReferenceValidationOptions
): Validators<Partial<EditDataType<ReferenceDetailsType>>> => ({
title_primary: {
name: 'title_primary',
useEditData: true,
miscCheck: (obj: object) => {
return orCheck(obj as EditDataType<ReferenceDetailsType>, 'title_primary', options)
},
},
title_secondary: {
name: 'title_secondary',
useEditData: true,
miscCheck: (obj: object) => {
return orCheck(obj as EditDataType<ReferenceDetailsType>, 'title_secondary', options)
},
},
title_series: {
name: 'title_series',
useEditData: true,
miscCheck: (obj: object) => {
return orCheck(obj as EditDataType<ReferenceDetailsType>, 'title_series', options)
},
},
gen_notes: {
name: 'gen_notes',
useEditData: true,
miscCheck: (obj: object) => {
return orCheck(obj as EditDataType<ReferenceDetailsType>, 'gen_notes', options)
},
},
ref_type_id: {
name: 'ref_type_id',
required: true,
asNumber: true,
},
date_primary: {
name: 'date_primary',
required: true,
asNumber: yearCheck,
condition: (data: Partial<EditDataType<ReferenceDetailsType>>) => {
const ids: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
return data.ref_type_id != null && ids.includes(data.ref_type_id)
},
},
start_page: {
name: 'start_page',
required: false,
asNumber: (num: number) => positiveIntegerCheck('start_page', num),
},
end_page: {
name: 'end_page',
required: false,
asNumber: (num: number) => {
const base = positiveIntegerCheck('end_page', num)
if (base) return base
if (typeof editData.start_page === 'number' && num < editData.start_page) {
return 'end_page must be greater than or equal to start_page'
}
return null
},
},
date_secondary: {
name: 'date_secondary',
required: false,
asNumber: yearCheck,
},
ref_authors: {
name: 'ref_authors',
required: true,
minLength: 1,
miscArray: authorCheck,
condition: (data: Partial<EditDataType<ReferenceDetailsType>>) => {
const ids: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
return data.ref_type_id != null && ids.includes(data.ref_type_id)
},
},
ref_journal: {
name: 'ref_journal',
miscCheck: journalCheck,
condition: (data: Partial<EditDataType<ReferenceDetailsType>>) => {
const ids: number[] = [1, 5, 14]
return data.ref_type_id != null && ids.includes(data.ref_type_id)
},
},
exact_date: {
name: 'exact_date',
required: true,
asString: dateCheck,
condition: (data: Partial<EditDataType<ReferenceDetailsType>>) => {
const ids: number[] = [6, 7, 10, 11, 12, 13, 14]
return data.ref_type_id != null && ids.includes(data.ref_type_id)
},
},
})
export const validateReference = (
editData: EditDataType<ReferenceDetailsType>,
fieldName: keyof EditDataType<ReferenceDetailsType>,
options?: ReferenceValidationOptions
) => {
const validators = createReferenceValidators(editData, options)
return validator<EditDataType<ReferenceDetailsType>>(validators, editData, fieldName)
}
export const validateReferenceFields = (
editData: EditDataType<ReferenceDetailsType>,
options?: ReferenceValidationOptions
) => validateFields<EditDataType<ReferenceDetailsType>>(createReferenceValidators(editData, options), editData)
export const createReferenceValidatorWithLabels =
(displayLabelMap?: ReferenceDisplayLabelMap) =>
(editData: EditDataType<ReferenceDetailsType>, fieldName: keyof EditDataType<ReferenceDetailsType>) =>
validateReference(editData, fieldName, { displayLabelMap })
export const createReferenceFieldsValidatorWithLabels =
(displayLabelMap?: ReferenceDisplayLabelMap) => (editData: EditDataType<ReferenceDetailsType>) =>
validateReferenceFields(editData, { displayLabelMap })