Skip to content

Commit 2982fdf

Browse files
committed
add custom error handling
1 parent d446a4e commit 2982fdf

7 files changed

Lines changed: 47 additions & 6 deletions

File tree

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea/
12
src/
23
node_modules/
34

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ Easy-to-use React hook for validating forms with the [class-validator](https://g
1111
npm install --save react-class-validator
1212
```
1313

14+
```typescript
15+
16+
const validatorOptions: ValidatorContextOptions = {
17+
onErrorMessage: (error): string => {
18+
// custom error message handling (localization, etc)
19+
}
20+
}
21+
22+
render((
23+
<ValidatorContext options={validatorOptions}>
24+
<MyComponent />
25+
</ValidatorContext>
26+
), document.getElementById('root'))
27+
```
28+
1429
## Usage
1530

1631
```typescript
@@ -32,7 +47,7 @@ class LoginValidation {
3247
```
3348

3449
```typescript jsx
35-
const LoginForm = () => {
50+
const MyComponent = () => {
3651

3752
const [username, setUsername] = useState('');
3853
const [password, setPassword] = useState('');

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-class-validator",
3-
"version": "1.0.0-beta.2",
3+
"version": "1.0.0-beta.3",
44
"description": "React hook for validation with class-validator",
55
"main": "dist/index.js",
66
"scripts": {

src/context.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {ValidationError} from "class-validator";
2+
import React, {createContext, FunctionComponent} from "react";
3+
4+
export type OnErrorMessageHandler = (error: ValidationError) => string;
5+
export type ValidatorContextOptions = {
6+
onErrorMessage: OnErrorMessageHandler;
7+
};
8+
9+
const _getDefaultContextOptions = (): ValidatorContextOptions => ({
10+
onErrorMessage: (error) => Object.keys(error.constraints).map((key) => error.constraints[key])[0]
11+
});
12+
13+
export const ValidatorContext = createContext<ValidatorContextOptions>(null);
14+
15+
export const ValidatorProvider: FunctionComponent<{ options?: ValidatorContextOptions }> =
16+
({options = _getDefaultContextOptions(), children}) => (
17+
<ValidatorContext.Provider value={options}>
18+
{children}
19+
</ValidatorContext.Provider>
20+
);

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {validate} from 'class-validator';
2-
import {useState} from 'react';
2+
import {useContext, useState} from 'react';
3+
import {ValidatorContext} from "./context";
4+
5+
export {ValidatorProvider, ValidatorContextOptions, OnErrorMessageHandler} from './context';
36

47
type Newable<T> = {
58
new(): T;
@@ -12,6 +15,8 @@ type UseValidationResult<T, K extends keyof T> = [ValidationFunction<T, K>, Vali
1215

1316
export const useValidation = <T, K extends keyof T>(validationClass: Newable<T>): UseValidationResult<T, K> => {
1417

18+
const {onErrorMessage} = useContext(ValidatorContext);
19+
1520
const [validationErrors, setErrors] = useState<ValidationErrorMap<T, K>>({});
1621

1722
const validateCallback: ValidationFunction<T, K> = async (payload, filter: K[] = []) => {
@@ -31,7 +36,7 @@ export const useValidation = <T, K extends keyof T>(validationClass: Newable<T>)
3136
const validation: ValidationErrorMap<T, K> = errors.reduce(
3237
(acc, value) => ({
3338
...acc,
34-
[value.property as K]: Object.keys(value.constraints).map((key) => value.constraints[key])[0]
39+
[value.property as K]: onErrorMessage(value)
3540
}),
3641
{} as ValidationErrorMap<T, K>
3742
);

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"/**/*.spec.tsx"
2727
],
2828
"include": [
29-
"./src/**/*"
29+
"./src/index.ts"
3030
]
3131
}
3232

0 commit comments

Comments
 (0)