diff --git a/specs/Rules-hasValue-spec.jsx b/specs/Rules-isExisty-spec.jsx similarity index 94% rename from specs/Rules-hasValue-spec.jsx rename to specs/Rules-isExisty-spec.jsx index ff4967a..e0e0524 100644 --- a/specs/Rules-hasValue-spec.jsx +++ b/specs/Rules-isExisty-spec.jsx @@ -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( - + ); @@ -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(); }); -*/ - }); diff --git a/specs/Rules-isUrl-spec.jsx b/specs/Rules-isUrl-spec.jsx new file mode 100644 index 0000000..5adc1a5 --- /dev/null +++ b/specs/Rules-isUrl-spec.jsx @@ -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 + } + }); + + form = TestUtils.renderIntoDocument( + + + + ); + + 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(); + }); + +}); diff --git a/src/validationRules.js b/src/validationRules.js index 6634310..7971262 100644 --- a/src/validationRules.js +++ b/src/validationRules.js @@ -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; },