-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject.ts
More file actions
78 lines (73 loc) · 2.19 KB
/
Copy pathproject.ts
File metadata and controls
78 lines (73 loc) · 2.19 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
import { EditDataType, ProjectDetailsType } from '../types'
import { ValidationError, Validators, validateFields, validator } from './validator'
/*
pid: number;
contact: string;
proj_code: string | null;
proj_name: string | null;
proj_status: string | null;
proj_records: boolean | null;
*/
const memberArrayCheck = (people: object[]) => {
const errors = new Set<string>()
people.forEach(person => {
if (!('initials' in person) || typeof person.initials !== 'string' || person.initials.length === 0) {
errors.add('Member initials must be a non-empty string')
}
if (
!('com_people' in person) ||
person.com_people === null ||
typeof person.com_people !== 'object' ||
!('user_id' in person.com_people) ||
!Number.isInteger(person.com_people.user_id)
) {
errors.add('Member must have a com_people object with valid user ID')
}
})
if (errors.size > 0) {
return ('Project members gave the following errors: ' + Array.from(errors).join(', ')) as ValidationError
}
return null
}
const projectValidators: Validators<Partial<EditDataType<ProjectDetailsType>>> = {
contact: {
name: 'Coordinator',
required: true,
},
proj_code: {
name: 'Project Code',
required: true,
maxLength: 10,
asString: true,
},
proj_name: {
name: 'Project Name',
required: true,
},
proj_status: {
name: 'Project Status',
required: true,
asString: value => {
if (!['current', 'no_data', 'finished'].includes(value))
return 'Project Status must be one of the following: "current", "no_data", or "finished".'
return null
},
},
proj_records: {
name: 'Record Status',
required: true,
},
now_proj_people: {
name: 'Members',
required: true,
miscArray: memberArrayCheck,
},
}
export const validateProject = (
editData: EditDataType<ProjectDetailsType>,
fieldName: keyof EditDataType<ProjectDetailsType>
) => {
return validator<EditDataType<ProjectDetailsType>>(projectValidators, editData, fieldName)
}
export const validateProjectFields = (editData: Partial<EditDataType<ProjectDetailsType>>) =>
validateFields<EditDataType<ProjectDetailsType>>(projectValidators, editData)