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

Setlocale #2

Merged
merged 2 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ 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: {
default: 'Não é válido',
},
number: {
max: 'Deve ser maior que ${min}',
min: 'Deve ser maior que ${min}',
},
})

Expand All @@ -169,7 +169,7 @@ const schema = yup.object().shape({
name: yup.string(),
age: yup.number().min(18),
})
schema.validate({ name: 'jimmy', age: 'hi' })
schema.validate({ name: 'jimmy', age: 11 })
.catch(function(err){
err.name // 'ValidationError'
err.errors // => ['Deve ser maior que 18']
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');
});
});