Throw warning on missing client validation

Client-side validation is not essential (we can only rely on server-side validation being enforced).
So it doesn’t make sense to throw an exception in this context, and stop the entire CMS execution because of missing rules.
This commit is contained in:
Ingo Schommer 2017-01-11 23:31:03 +13:00
parent 558a171366
commit fa2ef05ef1
3 changed files with 12 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

@ -60,7 +60,9 @@ class Validator {
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();
});
});