forked from opengovsg/FormSG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.ts
More file actions
37 lines (32 loc) · 1.25 KB
/
Copy pathvalidate.ts
File metadata and controls
37 lines (32 loc) · 1.25 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
import { FormField, FormFieldsV3 } from '../types'
function determineIsFormFields(tbd: any): tbd is FormField[] {
if (!Array.isArray(tbd)) {
return false
}
// If there exists even a single internal response that does not fit the
// shape, the object is not created properly.
const filter = tbd.filter(
(internal) =>
// Have either answer or answerArray or is isHeader
// Since empty strings are allowed, check using typeof.
(typeof internal.answer === 'string' ||
Array.isArray(internal.answerArray) ||
internal.isHeader) &&
internal._id &&
internal.fieldType &&
// The field is still valid even when the question title is empty string
// (even though it is not intended behavior).
typeof internal.question === 'string'
)
return filter.length === tbd.length
}
// TODO(MRF): This is currently very rudimentary, we should look at making this more specific where required.
function determineIsFormFieldsV3(tbd: any): tbd is FormFieldsV3 {
for (const id of Object.keys(tbd)) {
const value = tbd[id]
const hasCorrectShape = value.fieldType && value.answer !== undefined
if (!hasCorrectShape) return false
}
return true
}
export { determineIsFormFields, determineIsFormFieldsV3 }