Skip to content

Commit

Permalink
docs: add setLocale examples with functions. (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoliveira authored and jquense committed Oct 16, 2019
1 parent 3ad9472 commit 5f1a3d4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,35 @@ schema.validate({ name: 'jimmy', age: 11 }).catch(function(err) {
});
```

If you need multi-language support, Yup has got you covered. The function `useLocale` accepts functions that can be used to generate error objects with translation keys and values. Just get this output and feed it into your favorite i18n library.

```js
import { setLocale } from 'yup';

setLocale({
// use constant translation keys for messages without values
mixed: {
default: 'field_invalid',
},
// use functions to generate an error object that includes the value from the schema
number: {
min: ({ min }) => ({ key: 'field_too_short', values: { min } }),
max: ({ max }) => ({ key: 'field_too_big', values: { max } }),
},
});

// now use Yup schemas AFTER you defined your custom dictionary
let schema = yup.object().shape({
name: yup.string(),
age: yup.number().min(18),
});

schema.validate({ name: 'jimmy', age: 11 }).catch(function(err) {
err.name; // => 'ValidationError'
err.errors; // => [{ key: 'field_too_short', values: { min: 18 } }]
});
```

## API

### `yup`
Expand Down

0 comments on commit 5f1a3d4

Please sign in to comment.