-
Notifications
You must be signed in to change notification settings - Fork 1
/
validations.ts
190 lines (162 loc) · 4.8 KB
/
validations.ts
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
import { type TypeOf, z } from 'zod'
import { type components } from './registry-core-api'
const dateTime = z.string().datetime({ offset: true })
const paginationRequest = z.object({
page_size: z.number().positive().int(),
page_number: z.number().positive().int().optional()
})
const searchSort = z.object({
attribute_name: z.string().optional(),
sort_order: z.enum(['asc', 'desc'])
})
const consent = z.object({
id: z.string().optional(),
ts: dateTime.optional(),
purpose: z
.object({
text: z.string().optional(),
code: z.string().optional(),
refUri: z.string().optional()
})
.optional()
})
const authorize = z.object({
id: z.string().optional(),
ts: dateTime.optional(),
purpose: z
.object({
text: z.string().optional(),
code: z.string().optional(),
refUri: z.string().optional()
})
.optional()
})
const languageCode = z.string().regex(/^[a-z]{3,3}$/)
const version = z.string().default('1.0.0')
const syncHeader = z.object({
version: version.optional(),
message_id: z.string(),
message_ts: dateTime,
action: z.literal('search'),
sender_id: z.string(),
sender_uri: z.string().optional(),
receiver_id: z.string().optional(),
total_count: z.number(),
is_msg_encrypted: z.boolean().optional().default(false)
})
const asyncHeader = z.object({
version: version.optional(),
message_id: z.string(),
message_ts: dateTime,
action: z.literal('search'),
sender_id: z.string(),
sender_uri: z.string(),
receiver_id: z.string().optional(),
total_count: z.number(),
is_msg_encrypted: z.boolean().optional().default(false)
})
const regType = z.enum([
'ocrvs:registry_type:birth',
'ocrvs:registry_type:death',
'ocrvs:registry_type:marriage'
])
const commonSearchCriteria = z.object({
version: version.optional(),
reg_type: regType,
sort: z.array(searchSort).optional(),
pagination: paginationRequest.optional(),
consent: consent.optional(),
authorize: authorize.optional()
})
const identifierTypeValue = z.object({
type: z.enum(['BRN', 'DRN', 'MRN', 'OPENCRVS_RECORD_ID', 'NID']),
value: z.string()
})
const identifierTypeQuery = commonSearchCriteria.and(
z.object({
query_type: z.literal('idtype-value'),
query: identifierTypeValue
})
)
const expressionCondition = z.enum(['and'])
const expression = z.enum(['gt', 'lt', 'eq', 'ge', 'le'])
const expressionSupportedFields = z.enum(['birthdate', 'birthplace'])
const expressionPredicate = z.object({
attribute_name: expressionSupportedFields,
operator: expression,
attribute_value: z.string()
})
const predicateQuery = commonSearchCriteria.and(
z.object({
query_type: z.literal('predicate'),
query: z.array(
z.object({
seq_num: z.number().optional(),
expression1: expressionPredicate,
condition: expressionCondition.optional(),
expression2: expressionPredicate.optional()
})
)
})
)
const searchCriteria = predicateQuery.or(identifierTypeQuery)
export const searchRequestSchema = z.object({
transaction_id: z.string().max(99),
search_request: z.array(
z.object({
reference_id: z.string(),
timestamp: dateTime,
search_criteria: searchCriteria,
locale: languageCode.optional().default('eng')
})
)
}) satisfies z.Schema<components['schemas']['SearchRequest']>
const encryptedMessage = z.object({
header: z.object({
alg: z.string(),
enc: z.string(),
kid: z.string()
}),
ciphertext: z.string(),
encrypted_key: z.string(),
tag: z.string(),
iv: z.string()
})
const syncSearchRequest = z.object({
signature: z.string().optional(),
header: syncHeader,
message: searchRequestSchema
})
const encryptedSyncSearchRequest = z.object({
signature: z.string().optional(),
header: syncHeader,
message: encryptedMessage
})
const asyncSearchRequest = z.object({
signature: z.string().optional(),
header: asyncHeader,
message: searchRequestSchema
})
const encryptedAsyncSearchRequest = z.object({
signature: z.string().optional(),
header: asyncHeader,
message: encryptedMessage
})
export const maybeEncryptedSyncSearchRequestSchema = syncSearchRequest.or(
encryptedSyncSearchRequest
)
export const maybeEncryptedAsyncSearchRequestSchema = asyncSearchRequest.or(
encryptedAsyncSearchRequest
)
export type MaybeEncryptedSyncSearchRequest = TypeOf<
typeof maybeEncryptedSyncSearchRequestSchema
>
export type MaybeEncryptedAsyncSearchRequest = TypeOf<
typeof maybeEncryptedAsyncSearchRequestSchema
>
export type SyncSearchRequest = TypeOf<typeof syncSearchRequest>
export type AsyncSearchRequest = TypeOf<typeof asyncSearchRequest>
export type SearchCriteria = TypeOf<typeof searchCriteria>
export type PredicateQuery = TypeOf<typeof predicateQuery>
export type IdentifierTypeQuery = TypeOf<typeof identifierTypeQuery>
export type EventType = TypeOf<typeof regType>