Merge pull request #6482 from open-sausages/pulls/4.0/client-form-validation-warning

Client-side email validation rule
This commit is contained in:
Damian Mooyman 2017-01-16 16:39:17 +13:00 committed by GitHub
commit 34903fd4a7
3 changed files with 15 additions and 6 deletions

View File

@ -424,7 +424,8 @@ case"alphanumeric":return u["default"].isAlphanumeric(e)
case"alpha":return u["default"].isAlpha(e)
case"regex":return u["default"].matches(e,n.pattern)
case"max":return e.length<=n.length
default:throw new Error("Unknown validation rule used: '"+t+"'")}}},{key:"validateFieldSchema",value:function l(e){return this.validateField(e.name,e.validation,null!==e.leftTitle?e.leftTitle:e.title,e.customValidationMessage)
case"email":return u["default"].isEmail(e)
default:console.warn("Unknown validation rule used: '"+t+"'")}}},{key:"validateFieldSchema",value:function l(e){return this.validateField(e.name,e.validation,null!==e.leftTitle?e.leftTitle:e.title,e.customValidationMessage)
}},{key:"getMessage",value:function d(e,t){var n=""
if("string"==typeof t.message)n=t.message

View File

@ -56,8 +56,13 @@ class Validator {
case 'max': {
return value.length <= config.length;
}
case 'email': {
return validator.isEmail(value);
}
default: {
throw new Error(`Unknown validation rule used: '${rule}'`);
// eslint-disable-next-line no-console
console.warn(`Unknown validation rule used: '${rule}'`);
return false;
}
}
}

View File

@ -1,4 +1,7 @@
/* global jest, describe, beforeEach, it, expect */
/* global jest, describe, beforeEach, it, expect, console */
// Needs to be set before requiring other libraries
global.console = { warn: jest.fn() };
jest.unmock('react');
jest.unmock('react-addons-test-utils');
@ -56,9 +59,9 @@ describe('Validator', () => {
describe('validateValue()', () => {
it('should throw an error', () => {
validator = new Validator({});
const callRuleDoesNotExist = () => validator.validateValue('one', 'ruledoesnotexist');
expect(callRuleDoesNotExist).toThrowError(/unknown/i);
validator.validateValue('one', 'ruledoesnotexist');
// eslint-disable-next-line no-console
expect(console.warn).toBeCalled();
});
});