-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add localization support * add pluralization back in * add localization tests * update readme * update changelog --------- Co-authored-by: Kyriakos Nicola <knicola@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
216 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* eslint-disable no-console, no-template-curly-in-string */ | ||
import * as yup from 'yup' | ||
import YupPassword from '../src' | ||
YupPassword(yup) | ||
|
||
// StringLocale declaration merging does not seem to | ||
// work, so we have to declare yup password's locale | ||
// overrides as a separate "Record" object. | ||
const locale: Record<string, any> = { | ||
minSymbols: 'This is now localized. path=${path};length=${length}', | ||
} | ||
|
||
yup.setLocale({ | ||
string: { | ||
...locale, | ||
// Add other messages here | ||
}, | ||
}) | ||
|
||
const schema = yup.object({ | ||
password: yup.string().password(), | ||
}) | ||
|
||
const input = { | ||
password: 'Password1', | ||
} | ||
|
||
schema.validate(input, { abortEarly: false }) | ||
.catch(e => console.error(e.errors)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* eslint-disable | ||
no-template-curly-in-string | ||
*/ | ||
|
||
import * as yup from 'yup' | ||
import setup from '../src' | ||
setup(yup) | ||
const schema = yup.string() | ||
|
||
describe('Locale support', () => { | ||
describe('test default message: singular', () => { | ||
test('minLowercase()', async () => { | ||
const errorMessage = await schema.minLowercase(1).validate('A').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 1 lowercase letter') | ||
}) | ||
|
||
test('minUppercase()', async () => { | ||
const errorMessage = await schema.minUppercase(1).validate('a').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 1 uppercase letter') | ||
}) | ||
|
||
test('minNumbers()', async () => { | ||
const errorMessage = await schema.minNumbers(1).validate('a').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 1 number') | ||
}) | ||
|
||
test('minSymbols()', async () => { | ||
const errorMessage = await schema.minSymbols(1).validate('a').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 1 symbol') | ||
}) | ||
|
||
test('maxRepeating()', async () => { | ||
const errorMessage = await schema.maxRepeating(1).validate('aa').catch(e => e.message) | ||
expect(errorMessage).toBe('this must not contain sequences of more than 1 repeated characters') | ||
}) | ||
|
||
test('minWords()', async () => { | ||
const errorMessage = await schema.minWords(1).validate('$').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 1 word') | ||
}) | ||
}) // group | ||
|
||
describe('test default message: plural', () => { | ||
test('minLowercase()', async () => { | ||
const errorMessage = await schema.minLowercase(2).validate('a').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 2 lowercase letters') | ||
}) | ||
|
||
test('minUppercase()', async () => { | ||
const errorMessage = await schema.minUppercase(2).validate('A').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 2 uppercase letters') | ||
}) | ||
|
||
test('minNumbers()', async () => { | ||
const errorMessage = await schema.minNumbers(2).validate('1').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 2 numbers') | ||
}) | ||
|
||
test('minSymbols()', async () => { | ||
const errorMessage = await schema.minSymbols(2).validate('!').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 2 symbols') | ||
}) | ||
|
||
test('maxRepeating()', async () => { | ||
const errorMessage = await schema.maxRepeating(2).validate('aaa').catch(e => e.message) | ||
expect(errorMessage).toBe('this must not contain sequences of more than 2 repeated characters') | ||
}) | ||
|
||
test('minWords()', async () => { | ||
const errorMessage = await schema.minWords(2).validate('a').catch(e => e.message) | ||
expect(errorMessage).toBe('this must contain at least 2 words') | ||
}) | ||
}) // group | ||
|
||
describe('test localized message', () => { | ||
beforeAll(() => { | ||
yup.setLocale({ | ||
string: { | ||
minLowercase: 'name=minLowercase;path=${path};length=${length}', | ||
minUppercase: 'name=minUppercase;path=${path};length=${length}', | ||
minNumbers: 'name=minNumbers;path=${path};length=${length}', | ||
minSymbols: 'name=minSymbols;path=${path};length=${length}', | ||
maxRepeating: 'name=maxRepeating;path=${path};length=${length}', | ||
minWords: 'name=minWords;path=${path};length=${length}', | ||
} as any, | ||
}) | ||
}) | ||
test('minLowercase()', async () => { | ||
const errorMessage = await schema.minLowercase(2).validate('a').catch(e => e.message) | ||
expect(errorMessage).toBe('name=minLowercase;path=this;length=2') | ||
}) | ||
|
||
test('minUppercase()', async () => { | ||
const errorMessage = await schema.minUppercase(2).validate('A').catch(e => e.message) | ||
expect(errorMessage).toBe('name=minUppercase;path=this;length=2') | ||
}) | ||
|
||
test('minNumbers()', async () => { | ||
const errorMessage = await schema.minNumbers(2).validate('1').catch(e => e.message) | ||
expect(errorMessage).toBe('name=minNumbers;path=this;length=2') | ||
}) | ||
|
||
test('minSymbols()', async () => { | ||
const errorMessage = await schema.minSymbols(2).validate('!').catch(e => e.message) | ||
expect(errorMessage).toBe('name=minSymbols;path=this;length=2') | ||
}) | ||
|
||
test('maxRepeating()', async () => { | ||
const errorMessage = await schema.maxRepeating(2).validate('aaa').catch(e => e.message) | ||
expect(errorMessage).toBe('name=maxRepeating;path=this;length=2') | ||
}) | ||
|
||
test('minWords()', async () => { | ||
const errorMessage = await schema.minWords(2).validate('a').catch(e => e.message) | ||
expect(errorMessage).toBe('name=minWords;path=this;length=2') | ||
}) | ||
}) // group | ||
}) // group |