forked from QuorumProof/QuorumProof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.ts
More file actions
374 lines (340 loc) · 11.1 KB
/
Copy pathvalidate.ts
File metadata and controls
374 lines (340 loc) · 11.1 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* Issue #1312 — Request Validation Middleware
*
* Provides a composable, schema-driven validation middleware for Express
* endpoints. Uses AJV for JSON Schema validation with coercion and defaults,
* and supports custom validator functions for complex business-logic checks
* that cannot be expressed purely in JSON Schema.
*
* ## Features
*
* 1. **JSON Schema validation** — body, query, and params can each have a
* JSON Schema. Schemas are compiled once at startup and reused across
* requests (AJV caches compiled validators internally).
*
* 2. **Detailed error responses** — on validation failure the 400 body
* includes a `details` array describing every AJV error:
* `{ path, keyword, message }`. This lets API clients surface precise
* field-level errors rather than a generic "invalid" message.
*
* 3. **Custom validators** — each location (body / query / params) accepts an
* optional `custom` function `(data) => true | string | string[]`.
* Returning a string (or array of strings) means validation failed; the
* string becomes the error message in the 400 response. This is useful for
* cross-field invariants, Stellar address checks, etc.
*
* ## Usage
*
* ```ts
* import { validate, schemas } from '../middleware/validate.js';
*
* router.post(
* '/batch',
* validate({
* body: {
* schema: schemas.verifyBatch.body,
* custom: (data) => {
* const d = data as { credential_ids: number[]; slice_id: number };
* if (d.credential_ids.includes(d.slice_id)) {
* return 'slice_id must not appear in credential_ids';
* }
* return true;
* },
* },
* }),
* handler,
* );
* ```
*/
import { Request, Response, NextFunction } from 'express';
import { default as AjvLib } from 'ajv';
const Ajv = AjvLib as unknown as new (opts: Record<string, unknown>) => {
compile: (schema: Record<string, unknown>) => AjvValidatorFn;
};
const ajv = new Ajv({ coerceTypes: true, useDefaults: true, removeAdditional: true });
// ---------------------------------------------------------------------------
// Internal types
// ---------------------------------------------------------------------------
interface AjvError {
schemaPath: string;
keyword: string;
message: string;
data: unknown;
}
interface AjvValidatorFn {
(data: unknown): boolean;
errors?: AjvError[];
}
/**
* A custom validator that runs *after* the JSON Schema check passes.
*
* Return `true` to indicate the value is valid.
* Return a `string` (or `string[]`) to indicate failure; the string(s) become
* the error message(s) in the 400 response body.
*/
export type CustomValidator = (data: unknown) => true | string | string[];
/**
* Per-location validation configuration. You can supply either a plain JSON
* Schema object (backward-compatible with the previous API) or a richer
* `LocationConfig` object that also carries a custom validator.
*/
export type LocationSchema = Record<string, unknown>;
export interface LocationConfig {
/** AJV-compatible JSON Schema for structural validation. */
schema?: LocationSchema;
/**
* Custom validator for business-logic checks.
* Runs only when the JSON Schema check passes (or when no schema is
* provided).
*/
custom?: CustomValidator;
}
export type LocationInput = LocationSchema | LocationConfig;
export type ValidationSchemas = {
body?: LocationInput;
query?: LocationInput;
params?: LocationInput;
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Normalise a `LocationInput` to a `LocationConfig`.
* A plain schema object is wrapped in `{ schema }`.
*/
function normalise(input: LocationInput): LocationConfig {
if ('schema' in input || 'custom' in input) {
return input as LocationConfig;
}
// Plain JSON Schema object — treat as `{ schema: input }`.
return { schema: input as LocationSchema };
}
// ---------------------------------------------------------------------------
// Middleware factory
// ---------------------------------------------------------------------------
export function validate(schemas: ValidationSchemas) {
type Entry = {
location: 'body' | 'query' | 'params';
validator: AjvValidatorFn | null;
custom: CustomValidator | null;
};
const entries: Entry[] = [];
for (const location of ['body', 'query', 'params'] as const) {
const input = schemas[location];
if (!input) continue;
const cfg = normalise(input);
const validator = cfg.schema
? (ajv.compile(cfg.schema) as AjvValidatorFn)
: null;
entries.push({
location,
validator,
custom: cfg.custom ?? null,
});
}
return (req: Request, res: Response, next: NextFunction): void => {
for (const { location, validator, custom } of entries) {
let data: unknown;
if (location === 'body') data = req.body;
else if (location === 'query') data = req.query;
else data = req.params;
// 1. JSON Schema validation
if (validator !== null && !validator(data)) {
const errors = validator.errors ?? [];
res.status(400).json({
error: 'Validation failed',
location,
details: errors.map((err) => ({
path: err.schemaPath,
keyword: err.keyword,
message: err.message,
})),
});
return;
}
// 2. Custom validation (only reached when schema check passes or absent)
if (custom !== null) {
const result = custom(data);
if (result !== true) {
const messages = Array.isArray(result) ? result : [result];
res.status(400).json({
error: 'Validation failed',
location,
details: messages.map((message) => ({ path: '#', keyword: 'custom', message })),
});
return;
}
}
}
next();
};
}
// ---------------------------------------------------------------------------
// Built-in custom validators
// ---------------------------------------------------------------------------
/**
* Checks that a string value looks like a valid Stellar account address
* (G-address, 56 characters, base32 alphabet).
*
* This is a lightweight format check — it does not verify the checksum.
* Use `@stellar/stellar-sdk`'s `StrKey.isValidEd25519PublicKey` for a
* rigorous check when the SDK is already in scope.
*/
export function stellarAddressValidator(fieldName: string): CustomValidator {
const STELLAR_G_ADDRESS = /^G[A-Z2-7]{55}$/;
return (data) => {
if (typeof data !== 'object' || data === null) return true;
const value = (data as Record<string, unknown>)[fieldName];
if (value === undefined || value === null) return true; // required-ness enforced by schema
if (typeof value !== 'string' || !STELLAR_G_ADDRESS.test(value)) {
return `${fieldName} must be a valid Stellar G-address`;
}
return true;
};
}
/**
* Ensures that an array field has no duplicate values.
*/
export function noDuplicatesValidator(fieldName: string): CustomValidator {
return (data) => {
if (typeof data !== 'object' || data === null) return true;
const arr = (data as Record<string, unknown>)[fieldName];
if (!Array.isArray(arr)) return true;
const seen = new Set();
for (const item of arr) {
const key = JSON.stringify(item);
if (seen.has(key)) return `${fieldName} must not contain duplicate values`;
seen.add(key);
}
return true;
};
}
// ---------------------------------------------------------------------------
// Shared schemas
// ---------------------------------------------------------------------------
export const schemas = {
verifyBatch: {
body: {
type: 'object',
properties: {
credential_ids: {
type: 'array',
items: { type: 'integer', minimum: 1 },
minItems: 1,
maxItems: 50,
},
slice_id: { type: 'integer', minimum: 1 },
},
required: ['credential_ids', 'slice_id'],
additionalProperties: false,
},
},
verifyBatchClaims: {
body: {
type: 'object',
properties: {
items: {
type: 'array',
minItems: 1,
maxItems: 100,
items: {
type: 'object',
properties: {
credential_id: { type: 'integer', minimum: 1 },
claim_type: { type: 'string', minLength: 1, maxLength: 64 },
},
required: ['credential_id', 'claim_type'],
additionalProperties: false,
},
},
},
required: ['items'],
additionalProperties: false,
},
},
notificationPreferences: {
body: {
type: 'object',
properties: {
address: { type: 'string', minLength: 1 },
email: { type: 'string' },
phone: { type: 'string' },
channels: {
type: 'array',
items: { type: 'string', enum: ['email', 'sms'] },
minItems: 1,
},
events: {
type: 'array',
items: {
type: 'string',
enum: [
'credential_issued', 'credential_revoked', 'credential_suspended',
'credential_attested', 'credential_expiring',
],
},
minItems: 1,
},
/** #928: optional per-type filter; 1=Degree, 2=License, 3=Employment */
credential_type_filters: {
type: 'array',
items: { type: 'integer', minimum: 1 },
},
enabled: { type: 'boolean' },
},
required: ['address', 'channels', 'events'],
additionalProperties: false,
},
},
notificationSend: {
body: {
type: 'object',
properties: {
address: { type: 'string', minLength: 1 },
event: {
type: 'string',
enum: [
'credential_issued', 'credential_revoked', 'credential_suspended',
'credential_attested', 'credential_expiring',
],
},
credential_id: { type: 'integer', minimum: 1 },
/** #928: optional credential type for per-type preference filtering */
credential_type: { type: 'integer', minimum: 1 },
issuer: { type: 'string' },
holder: { type: 'string' },
},
required: ['address', 'event', 'credential_id'],
additionalProperties: false,
},
},
analyticsEvent: {
body: {
type: 'object',
properties: {
type: {
type: 'string',
enum: ['issued', 'attested', 'revoked', 'suspended', 'verified'],
},
credential_id: { type: 'string', minLength: 1 },
timestamp: { type: 'string', minLength: 1 },
issuer: { type: 'string' },
subject: { type: 'string' },
attestor: { type: 'string' },
},
required: ['type', 'credential_id', 'timestamp'],
additionalProperties: false,
},
},
auditVerify: {
body: {
type: 'object',
properties: {
batch_id: { type: 'integer', minimum: 1 },
},
required: ['batch_id'],
additionalProperties: false,
},
},
};
export default validate;