Skip to content

Commit 2182c02

Browse files
committed
allow multiple error messages
1 parent 2982fdf commit 2182c02

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const MyComponent = () => {
7373
{/* show error */}
7474
{errors.username && (
7575
<div className="error">
76-
{errors.username}
76+
{errors.username.map((message) => <strong>message</strong>)}
7777
</div>
7878
)}
7979

src/context.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {ValidationError} from "class-validator";
22
import React, {createContext, FunctionComponent} from "react";
33

4-
export type OnErrorMessageHandler = (error: ValidationError) => string;
4+
export type OnErrorMessageHandler = (error: ValidationError) => string[];
55
export type ValidatorContextOptions = {
66
onErrorMessage: OnErrorMessageHandler;
77
};
88

99
const _getDefaultContextOptions = (): ValidatorContextOptions => ({
10-
onErrorMessage: (error) => Object.keys(error.constraints).map((key) => error.constraints[key])[0]
10+
onErrorMessage: (error) => Object.keys(error.constraints).map((key) => error.constraints[key])
1111
});
1212

1313
export const ValidatorContext = createContext<ValidatorContextOptions>(null);

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Newable<T> = {
88
new(): T;
99
} | Function;
1010

11-
type ValidationErrorMap<T, K extends keyof T> = { [key in K]?: string };
11+
type ValidationErrorMap<T, K extends keyof T> = { [key in K]?: string[] };
1212
type ValidationPayload<T, K extends keyof T> = { [key in K]?: T[K] };
1313
type ValidationFunction<T, K extends keyof T> = (payload: ValidationPayload<T, K>, filter?: K[]) => Promise<boolean>;
1414
type UseValidationResult<T, K extends keyof T> = [ValidationFunction<T, K>, ValidationErrorMap<T, K>];

0 commit comments

Comments
 (0)