-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion-data.ts
More file actions
115 lines (90 loc) · 3.72 KB
/
question-data.ts
File metadata and controls
115 lines (90 loc) · 3.72 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
import { z } from 'zod'
import { makeJsonCodecSimple } from '../utils/json'
import { AnnotationDataSchema } from './annotation-data'
export const QuestionValueTypeSchema = z.enum([
'StringQuestionValueType',
'NumberQuestionValueType',
'DateQuestionValueType',
'DateTimeQuestionValueType',
'TimeQuestionValueType',
'TextQuestionValueType',
'EmailQuestionValueType',
'UrlQuestionValueType',
'ColorQuestionValueType',
])
// Field schemas
export const CommonQuestionFieldsSchema = z.object({
uuid: z.uuid(),
title: z.string(),
text: z.string().nullable(),
requiredPhaseUuid: z.uuid().nullable(),
tagUuids: z.array(z.uuid()),
referenceUuids: z.array(z.uuid()),
expertUuids: z.array(z.uuid()),
annotations: z.array(AnnotationDataSchema),
})
export const OptionsQuestionFieldsSchema = z.object({
answerUuids: z.array(z.uuid()),
})
export const ListQuestionFieldsSchema = z.object({
itemTemplateQuestionUuids: z.array(z.uuid()),
})
export const ValueQuestionFieldsSchema = z.object({
valueType: QuestionValueTypeSchema,
})
export const MultiChoiceQuestionFieldsSchema = z.object({
choiceUuids: z.array(z.uuid()),
})
export const ItemSelectQuestionFieldsSchema = z.object({
listQuestionUuid: z.uuid().nullable(),
})
export const IntegrationQuestionFieldsSchema = z.object({
integrationUuid: z.uuid(),
variables: z.record(z.string(), z.string()), // Dict String String
})
export const FileQuestionFieldsSchema = z.object({
maxSize: z.number().int().nullable(),
fileTypes: z.string().nullable(),
})
// Question type schemas
export const OptionsQuestionSchema = CommonQuestionFieldsSchema.extend({
questionType: z.literal('OptionsQuestion'),
}).extend(OptionsQuestionFieldsSchema.shape)
export const ListQuestionSchema = CommonQuestionFieldsSchema.extend({
questionType: z.literal('ListQuestion'),
}).extend(ListQuestionFieldsSchema.shape)
export const ValueQuestionSchema = CommonQuestionFieldsSchema.extend({
questionType: z.literal('ValueQuestion'),
}).extend(ValueQuestionFieldsSchema.shape)
export const MultiChoiceQuestionSchema = CommonQuestionFieldsSchema.extend({
questionType: z.literal('MultiChoiceQuestion'),
}).extend(MultiChoiceQuestionFieldsSchema.shape)
export const ItemSelectQuestionSchema = CommonQuestionFieldsSchema.extend({
questionType: z.literal('ItemSelectQuestion'),
}).extend(ItemSelectQuestionFieldsSchema.shape)
export const IntegrationQuestionSchema = CommonQuestionFieldsSchema.extend({
questionType: z.literal('IntegrationQuestion'),
}).extend(IntegrationQuestionFieldsSchema.shape)
export const FileQuestionSchema = CommonQuestionFieldsSchema.extend({
questionType: z.literal('FileQuestion'),
}).extend(FileQuestionFieldsSchema.shape)
// Full QuestionData schema
export const QuestionDataSchema = z.discriminatedUnion('questionType', [
OptionsQuestionSchema,
ListQuestionSchema,
ValueQuestionSchema,
MultiChoiceQuestionSchema,
ItemSelectQuestionSchema,
IntegrationQuestionSchema,
FileQuestionSchema,
])
export type QuestionValueType = z.infer<typeof QuestionValueTypeSchema>
export type QuestionData = z.infer<typeof QuestionDataSchema>
export type OptionsQuestionData = z.infer<typeof OptionsQuestionSchema>
export type ListQuestion = z.infer<typeof ListQuestionSchema>
export type ValueQuestion = z.infer<typeof ValueQuestionSchema>
export type MultiChoiceQuestion = z.infer<typeof MultiChoiceQuestionSchema>
export type ItemSelectQuestion = z.infer<typeof ItemSelectQuestionSchema>
export type IntegrationQuestion = z.infer<typeof IntegrationQuestionSchema>
export type FileQuestionData = z.infer<typeof FileQuestionSchema>
export const QuestionDataCodec = makeJsonCodecSimple(QuestionDataSchema)