Skip to content
This repository has been archived by the owner on Aug 5, 2019. It is now read-only.

Commit

Permalink
Support for setLocale regardless of import order
Browse files Browse the repository at this point in the history
  • Loading branch information
haydnhkim authored and ctavan committed Sep 26, 2017
1 parent 24266b1 commit cd00557
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ schema.cast({
Allows you to customize the default messages used by Yup, when no message is provided with a validation test.
If any message is missing in the custom dictionary the error message will default to Yup's one.
```js
import { setLocale } from 'yup/lib/customLocale'
import setLocale from 'yup/lib/customLocale'

setLocale({
mixed: {
Expand Down
15 changes: 7 additions & 8 deletions src/customLocale.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
let dict = {};
import locale from './locale';

export function setLocale(custom) {
dict = { ...dict, ...custom };
return dict;
}

export function getLocale() {
return dict;
export default function setLocale(custom) {
Object.keys(custom).forEach((type) => {
Object.keys(custom[type]).forEach((method) => {
locale[type][method] = custom[type][method];
});
});
}
10 changes: 0 additions & 10 deletions src/locale.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* eslint-disable no-template-curly-in-string */
import printValue from './util/printValue';
import { getLocale } from './customLocale';

const customLocale = getLocale();

export const mixed = {
default: '${path} is invalid',
Expand All @@ -21,7 +18,6 @@ export const mixed = {

return msg;
},
...customLocale.mixed,
};

export const string = {
Expand All @@ -34,7 +30,6 @@ export const string = {
trim: '${path} must be a trimmed string',
lowercase: '${path} must be a lowercase string',
uppercase: '${path} must be a upper case string',
...customLocale.string,
};

export const number = {
Expand All @@ -43,28 +38,23 @@ export const number = {
positive: '${path} must be a positive number',
negative: '${path} must be a negative number',
integer: '${path} must be an integer',
...customLocale.number,
};

export const date = {
min: '${path} field must be later than ${min}',
max: '${path} field must be at earlier than ${max}',
...customLocale.date,
};

export const boolean = {
...customLocale.boolean,
};

export const object = {
noUnknown: '${path} field cannot have keys not specified in the object shape',
...customLocale.object,
};

export const array = {
min: '${path} field must have at least ${min} items',
max: '${path} field must have less than ${max} items',
...customLocale.array,
};

export default {
Expand Down
13 changes: 10 additions & 3 deletions test/customLocale.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { setLocale, getLocale } from '../src/customLocale';
/* eslint-disable no-template-curly-in-string */
import setLocale from '../src/customLocale';

describe('Custom locale', () => {
it('should get default locale', () => {
const locale = require('../src/locale').default; // eslint-disable-line global-require
expect(locale.string.email).to.equal('${path} must be a valid email');
});

it('should set a new locale', () => {
const locale = require('../src/locale').default; // eslint-disable-line global-require
const dict = {
string: {
email: 'Invalid email',
Expand All @@ -10,11 +17,11 @@ describe('Custom locale', () => {

setLocale(dict);

expect(getLocale()).to.deep.equal(dict);
expect(locale.string.email).to.equal(dict.string.email);
});

it('should update the main locale', () => {
const locale = require('../src/locale').default; // eslint-disable-line global-require
expect(locale.string).to.deep.include(getLocale().string);
expect(locale.string.email).to.equal('Invalid email');
});
});

0 comments on commit cd00557

Please sign in to comment.