-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathvalidation.ts
More file actions
46 lines (42 loc) · 1.35 KB
/
validation.ts
File metadata and controls
46 lines (42 loc) · 1.35 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
import type { z } from 'zod';
import { getZodErrorMessage } from './local-helpers';
/**
* Error indicating violations of field-level constraints
*/
export class ValidationError {
constructor(public readonly message: string) {}
}
/**
* Validate the given data with the given zod schema (for field-level constraints)
*/
export function validate(validator: z.ZodType, data: unknown) {
try {
validator.parse(data);
} catch (err) {
throw new ValidationError(getZodErrorMessage(err as z.ZodError));
}
}
/**
* Check if the given object has all the given fields, not null or undefined
* @param obj
* @param fields
* @returns
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function hasAllFields(obj: any, fields: string[]) {
if (typeof obj !== 'object' || !obj) {
return false;
}
return fields.every((f) => obj[f] !== undefined && obj[f] !== null);
}
/**
* Check if the given objects have equal values for the given fields. Returns
* false if either object is nullish or is not an object.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function allFieldsEqual(obj1: any, obj2: any, fields: string[]) {
if (!obj1 || !obj2 || typeof obj1 !== 'object' || typeof obj2 !== 'object') {
return false;
}
return fields.every((f) => obj1[f] === obj2[f]);
}