-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.ts
More file actions
26 lines (23 loc) · 781 Bytes
/
errors.ts
File metadata and controls
26 lines (23 loc) · 781 Bytes
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
import { constraintKey } from './validate.ts';
import { ValidateFunctionOptions } from './types.ts';
export type ValidationError = {
property: string;
errorMessage: string;
// deno-lint-ignore no-explicit-any
constraints: any[];
};
export const createErrorMessage = (options?: ValidateFunctionOptions) => {
let message = options?.errorMessage || 'Failed behavior';
if (options?.constraints) {
for (const index in options.constraints) {
message = message.replace(
`${constraintKey}${+index + 1}`,
`${options.constraints[index]}`,
);
}
}
return message;
};
export const formatErrors = (errors: ValidationError[], propertyName: string) => {
return errors.map((error: ValidationError) => ({ ...error, property: `${propertyName}.${error.property}` }));
};