-
Notifications
You must be signed in to change notification settings - Fork 933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support custom locale #105
Changes from 9 commits
ed08e3a
9b41938
811b9aa
91f1d80
c050c08
57d5b5b
aa71851
d0e2632
c1e1525
e9f0592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ json separate from validating it, via the `cast` method. | |
|
||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [Using a custom locale dictionary](#using-a-custom-locale-dictionary) | ||
- [API](#api) | ||
- [`yup`](#yup) | ||
- [`yup.reach(schema: Schema, path: string, value: ?object, context: ?object): Schema`](#yupreachschema-schema-path-string-value-object-context-object-schema) | ||
|
@@ -138,6 +139,25 @@ schema.cast({ | |
// => { name: 'jimmy', age: 24, createdOn: Date } | ||
``` | ||
|
||
### Using a custom locale dictionary | ||
```js | ||
import { setLocale } from 'yup/lib/locale' | ||
|
||
setLocale({ | ||
string: { | ||
email: 'Invalid', | ||
}, | ||
}) | ||
|
||
// Now use Yup schemas AFTER you defined your custom dicionary | ||
``` | ||
Why do you have to set the locales before you use Yup at all? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was think more about what this does generally, not why it needs be first. something like: "Allows you to customize the default messages used by Yup, when no message is provided with a validation test." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh ok |
||
As the locale is used directly from the object vars across the project once they boot up | ||
they'll stick to those values. This is related to the very way modules and objects are interpreted in JS. | ||
|
||
To be able to dynamically update the locale dict at any time in the app a more signicant change needs to be made. | ||
To instead of the modules of the lib to use `locale.mixed` to be something like `locale().mixed`, so we can change the dict value whenever we want. This is for the future. | ||
|
||
## API | ||
|
||
### `yup` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
exports.__esModule = true; | ||
|
||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
|
||
exports.setLocale = setLocale; | ||
exports.getLocale = getLocale; | ||
var dict = {}; | ||
|
||
function setLocale(custom) { | ||
return dict = _extends({}, dict, custom); | ||
} | ||
|
||
function getLocale() { | ||
return dict; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let dict = {} | ||
|
||
export function setLocale(custom){ | ||
return (dict = { ...dict, ...custom }) | ||
} | ||
|
||
export function getLocale(){ | ||
return dict | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { setLocale, getLocale } from '../src/customLocale' | ||
|
||
describe('Custom locale', () => { | ||
it('should set a new locale', () => { | ||
const dict = { | ||
string: { | ||
email: 'Invalid email', | ||
}, | ||
} | ||
|
||
setLocale(dict) | ||
|
||
expect(getLocale()).to.deep.equal(dict) | ||
}) | ||
|
||
it('should update the main locale', () => { | ||
const locale = require('../src/locale').default | ||
expect(locale.string).to.deep.include(getLocale().string) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you flesh this out a bit more, explanation of the code, why you need to do this, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I'll do it soon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done @jquense