diff --git a/README.md b/README.md index afb65c3..e7d8d39 100644 --- a/README.md +++ b/README.md @@ -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: { diff --git a/src/customLocale.js b/src/customLocale.js index 1d9f3dc..73647de 100644 --- a/src/customLocale.js +++ b/src/customLocale.js @@ -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]; + }); + }); } diff --git a/src/locale.js b/src/locale.js index 90a4893..7d10d28 100644 --- a/src/locale.js +++ b/src/locale.js @@ -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', @@ -21,7 +18,6 @@ export const mixed = { return msg; }, - ...customLocale.mixed, }; export const string = { @@ -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 = { @@ -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 { diff --git a/test/customLocale.js b/test/customLocale.js index cf30d20..2e9a60c 100644 --- a/test/customLocale.js +++ b/test/customLocale.js @@ -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', @@ -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'); }); });