forked from shakyShane/next-no-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.ts
39 lines (34 loc) · 972 Bytes
/
lib.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
import Joi from "joi";
export type FieldError = {
message: string;
path: (string | number)[];
};
export function validate(values: Record<string, string>): FieldError[] {
const authDataSchema = Joi.object({
email: Joi.string()
.email({ tlds: { allow: false } })
.lowercase()
.required(),
password: Joi.string().min(6).required().strict(),
});
const result = authDataSchema.validate(values);
if (result.error) {
return result.error.details.map((d) => {
return {
message: d.message,
path: d.path,
};
});
}
return [];
}
export function validateObject(values): Record<string, string> {
return obj(validate(values));
}
export function obj(errors: FieldError[]): Record<string, string> {
const output = {};
errors.forEach((e) => {
output[e.path.join(".")] = e.message;
});
return output;
}