-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeUnit.ts
More file actions
executable file
·72 lines (69 loc) · 2.43 KB
/
Copy pathtimeUnit.ts
File metadata and controls
executable file
·72 lines (69 loc) · 2.43 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
import { EditDataType, TimeUnitDetailsType } from '../types'
import { Validators, validateFields, validator } from './validator'
const createTimeUnitValidators = (
editData: EditDataType<TimeUnitDetailsType>
): Validators<EditDataType<Partial<TimeUnitDetailsType>>> => ({
tu_display_name: {
name: 'Name',
required: true,
},
sequence: {
name: 'Sequence',
required: true,
},
up_bnd: {
name: 'New Upper Bound',
asNumber: (num: number) => {
if (num === editData.low_bnd) return 'Upper and lower bounds cannot be the same'
return
},
},
low_bnd: {
name: 'New Lower Bound',
asNumber: (num: number) => {
if (num === editData.up_bnd) return 'Upper and lower bounds cannot be the same'
return
},
},
up_bound: {
name: 'Upper Bound',
required: () =>
editData.up_bnd !== undefined && editData.up_bnd !== null
? `Upper bound with ID ${editData.up_bnd} does not exist`
: 'This field is required',
miscCheck: () => {
if (editData.low_bound && editData.low_bound.age! === editData.up_bound!.age!) {
return 'Upper bound age cannot be the same as lower bound age'
}
if (editData.low_bound && editData.low_bound.age! < editData.up_bound!.age!) {
return 'Upper bound age has to be lower than lower bound age'
}
return
},
},
low_bound: {
name: 'Lower Bound',
required: () =>
editData.low_bnd !== undefined && editData.low_bnd !== null
? `Lower bound with ID ${editData.low_bnd} does not exist`
: 'This field is required',
miscCheck: () => {
if (editData.up_bound && editData.up_bound.age! === editData.low_bound!.age!) {
return 'Lower bound age cannot be the same as upper bound age'
}
if (editData.up_bound && editData.up_bound.age! > editData.low_bound!.age!) {
return 'Lower bound age has to be higher than upper bound age'
}
return
},
},
})
export const validateTimeUnit = (editData: EditDataType<TimeUnitDetailsType>, fieldName: keyof TimeUnitDetailsType) => {
const validators = createTimeUnitValidators(editData)
return validator<EditDataType<TimeUnitDetailsType>>(validators, editData, fieldName)
}
export const validateTimeUnitFields = (editData: Partial<EditDataType<TimeUnitDetailsType>>) =>
validateFields<EditDataType<TimeUnitDetailsType>>(
createTimeUnitValidators(editData as EditDataType<TimeUnitDetailsType>),
editData
)