Add some validation rules

This commit is contained in:
Semigradsky 2015-04-29 16:35:53 +03:00
parent 37018b10fa
commit f168b53a10
3 changed files with 87 additions and 14 deletions

View File

@ -2,7 +2,7 @@ var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var Formsy = require('./../src/main.js');
describe('Rules: hasValue', function() {
describe('Rules: isExisty', function() {
var TestInput, isValid, form, input;
beforeEach(function() {
@ -23,7 +23,7 @@ describe('Rules: hasValue', function() {
form = TestUtils.renderIntoDocument(
<Formsy.Form>
<TestInput name="foo" validations="hasValue"/>
<TestInput name="foo" validations="isExisty"/>
</Formsy.Form>
);
@ -41,6 +41,12 @@ describe('Rules: hasValue', function() {
expect(isValid).toHaveBeenCalled();
});
it('should pass with an empty string', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: ''}});
expect(isValid).toHaveBeenCalled();
});
it('should fail with an undefined', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: undefined}});
@ -59,20 +65,10 @@ describe('Rules: hasValue', function() {
expect(isValid).toHaveBeenCalled();
});
/* ToDo:
it('should pass with an empty string', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: ''}});
expect(isValid).toHaveBeenCalled();
});
it('should pass with a zero', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 0}});
expect(isValid).toHaveBeenCalled();
});
*/
});

View File

@ -0,0 +1,74 @@
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var Formsy = require('./../src/main.js');
describe('Rules: isUrl', function() {
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
},
render: function () {
if (this.isValid()) {
isValid();
}
return <input value={this.getValue()} onChange={this.updateValue}/>
}
});
form = TestUtils.renderIntoDocument(
<Formsy.Form>
<TestInput name="foo" value="foo" validations="isUrl"/>
</Formsy.Form>
);
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
});
it('should fail with "foo"', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 'foo'}});
expect(isValid).not.toHaveBeenCalled();
});
it('should pass with "https://www.google.com/"', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 'https://www.google.com/'}});
expect(isValid).toHaveBeenCalled();
});
it('should fail with an undefined', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: undefined}});
expect(isValid).not.toHaveBeenCalled();
});
it('should fail with a null', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: null}});
expect(isValid).not.toHaveBeenCalled();
});
it('should fail with a number', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: 42}});
expect(isValid).not.toHaveBeenCalled();
});
it('should fail with an empty string', function () {
expect(isValid).not.toHaveBeenCalled();
TestUtils.Simulate.change(input, {target: {value: ''}});
expect(isValid).not.toHaveBeenCalled();
});
});

View File

@ -6,8 +6,8 @@ var validations = {
isDefaultRequiredValue: function (values, value) {
return value === undefined || value === '';
},
hasValue: function (values, value) {
return !!value;
isExisty: function (values, value) {
return isExisty(value);
},
matchRegexp: function (values, value, regexp) {
return isExisty(value) && regexp.test(value);
@ -21,6 +21,9 @@ var validations = {
isEmail: function (values, value) {
return validations.matchRegexp(values, value, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i);
},
isUrl: function (values, value) {
return validations.matchRegexp(values, value, /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i);
},
isTrue: function (values, value) {
return value === true;
},