From 8f4e2566d55500a98e58bafc13cc4fe1c8c5dfc2 Mon Sep 17 00:00:00 2001 From: Erwan Jegouzo Date: Tue, 21 Apr 2015 15:32:13 -0400 Subject: [PATCH] improving validation rules + unit tests --- specs/Rules-equals-spec.js | 60 +++++++++++++ specs/Rules-hasValue-spec.js | 54 ++++++++++++ specs/Rules-isAlpha-spec.js | 60 +++++++++++++ specs/Rules-isEmail-spec.js | 48 ++++++++++ specs/Rules-isLength-spec.js | 72 +++++++++++++++ specs/Rules-isNumeric-spec.js | 72 +++++++++++++++ specs/Rules-isWords-spec.js | 66 ++++++++++++++ specs/Rules-maxLength-spec.js | 72 +++++++++++++++ specs/Rules-minLength-spec.js | 72 +++++++++++++++ specs/Validation-spec.js | 162 ---------------------------------- src/validationRules.js | 43 ++++----- 11 files changed, 599 insertions(+), 182 deletions(-) create mode 100644 specs/Rules-equals-spec.js create mode 100644 specs/Rules-hasValue-spec.js create mode 100644 specs/Rules-isAlpha-spec.js create mode 100644 specs/Rules-isEmail-spec.js create mode 100644 specs/Rules-isLength-spec.js create mode 100644 specs/Rules-isNumeric-spec.js create mode 100644 specs/Rules-isWords-spec.js create mode 100644 specs/Rules-maxLength-spec.js create mode 100644 specs/Rules-minLength-spec.js diff --git a/specs/Rules-equals-spec.js b/specs/Rules-equals-spec.js new file mode 100644 index 0000000..b4b72db --- /dev/null +++ b/specs/Rules-equals-spec.js @@ -0,0 +1,60 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: equals', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with null', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: null}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail when the value is not equal', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'foo'}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass when the value is equal', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'myValue'}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-hasValue-spec.js b/specs/Rules-hasValue-spec.js new file mode 100644 index 0000000..7cd71c9 --- /dev/null +++ b/specs/Rules-hasValue-spec.js @@ -0,0 +1,54 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: hasValue', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with null', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: null}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass with a string', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'myValue'}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-isAlpha-spec.js b/specs/Rules-isAlpha-spec.js new file mode 100644 index 0000000..7b71173 --- /dev/null +++ b/specs/Rules-isAlpha-spec.js @@ -0,0 +1,60 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: isAlpha', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with 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: 123}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass with a string', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'myValue'}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-isEmail-spec.js b/specs/Rules-isEmail-spec.js new file mode 100644 index 0000000..4c75cd0 --- /dev/null +++ b/specs/Rules-isEmail-spec.js @@ -0,0 +1,48 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: isEmail', 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 "foo@foo.com"', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'foo@foo.com'}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-isLength-spec.js b/specs/Rules-isLength-spec.js new file mode 100644 index 0000000..e456908 --- /dev/null +++ b/specs/Rules-isLength-spec.js @@ -0,0 +1,72 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: isLength', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with 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: 123}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with a string too small', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: "hi"}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with a string too long', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: "foo bar"}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass with the right length', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'sup'}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-isNumeric-spec.js b/specs/Rules-isNumeric-spec.js new file mode 100644 index 0000000..8048792 --- /dev/null +++ b/specs/Rules-isNumeric-spec.js @@ -0,0 +1,72 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: isNumeric', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with null', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: null}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with a string', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'myValue'}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass with a number as string', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: '123'}}); + expect(isValid).toHaveBeenCalled(); + }); + + it('should pass with an int', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 123}}); + expect(isValid).toHaveBeenCalled(); + }); + + it('should pass with a float', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 1.23}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-isWords-spec.js b/specs/Rules-isWords-spec.js new file mode 100644 index 0000000..bec480d --- /dev/null +++ b/specs/Rules-isWords-spec.js @@ -0,0 +1,66 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: isWords', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with 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: 123}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass with a 1 word', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'sup'}}); + expect(isValid).toHaveBeenCalled(); + }); + + it('should pass with 2 words', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'sup dude'}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-maxLength-spec.js b/specs/Rules-maxLength-spec.js new file mode 100644 index 0000000..690a90d --- /dev/null +++ b/specs/Rules-maxLength-spec.js @@ -0,0 +1,72 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: maxLength', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with 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: 123}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass when a string\'s length is smaller', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'hi'}}); + expect(isValid).toHaveBeenCalled(); + }); + + it('should pass when a string\'s length is equal', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'bar'}}); + expect(isValid).toHaveBeenCalled(); + }); + + it('should fail when a string\'s length is bigger', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'myValue'}}); + expect(isValid).not.toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Rules-minLength-spec.js b/specs/Rules-minLength-spec.js new file mode 100644 index 0000000..f442d55 --- /dev/null +++ b/specs/Rules-minLength-spec.js @@ -0,0 +1,72 @@ +var Formsy = require('./../src/main.js'); + +describe('Rules: minLength', 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 undefined', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: undefined}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail with 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: 123}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should fail when a string\'s length is smaller', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'hi'}}); + expect(isValid).not.toHaveBeenCalled(); + }); + + it('should pass when a string\'s length is equal', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'bar'}}); + expect(isValid).toHaveBeenCalled(); + }); + + it('should pass when a string\'s length is bigger', function () { + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'myValue'}}); + expect(isValid).toHaveBeenCalled(); + }); + +}); \ No newline at end of file diff --git a/specs/Validation-spec.js b/specs/Validation-spec.js index aefb9b2..971d0d4 100644 --- a/specs/Validation-spec.js +++ b/specs/Validation-spec.js @@ -215,166 +215,4 @@ describe('Validation', function() { }); - it('RULE: isEmail', function () { - - var isValid = jasmine.createSpy('valid'); - var TestInput = React.createClass({ - mixins: [Formsy.Mixin], - updateValue: function (event) { - this.setValue(event.target.value); - }, - render: function () { - if (this.isValid()) { - isValid(); - } - return - } - }); - var form = TestUtils.renderIntoDocument( - - - - ); - - var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT'); - expect(isValid).not.toHaveBeenCalled(); - TestUtils.Simulate.change(input, {target: {value: 'foo@foo.com'}}); - expect(isValid).toHaveBeenCalled(); - - }); - - it('RULE: isNumeric', function () { - - var isValid = jasmine.createSpy('valid'); - var TestInput = React.createClass({ - mixins: [Formsy.Mixin], - updateValue: function (event) { - this.setValue(event.target.value); - }, - render: function () { - if (this.isValid()) { - isValid(); - } - return - } - }); - var form = TestUtils.renderIntoDocument( - - - - ); - - var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT'); - expect(isValid).not.toHaveBeenCalled(); - TestUtils.Simulate.change(input, {target: {value: '123'}}); - expect(isValid).toHaveBeenCalled(); - - }); - - it('RULE: isNumeric (actual number)', function () { - - var isValid = jasmine.createSpy('valid'); - var TestInput = React.createClass({ - mixins: [Formsy.Mixin], - updateValue: function (event) { - this.setValue(Number(event.target.value)); - }, - render: function () { - if (this.isValid()) { - isValid(); - } - return - } - }); - var form = TestUtils.renderIntoDocument( - - - - ); - - var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT'); - expect(isValid).not.toHaveBeenCalled(); - TestUtils.Simulate.change(input, {target: {value: '123'}}); - expect(isValid).toHaveBeenCalled(); - - }); - - it('RULE: isNumeric (string representation of a float)', function () { - - var isValid = jasmine.createSpy('valid'); - var TestInput = React.createClass({ - mixins: [Formsy.Mixin], - updateValue: function (event) { - this.setValue(event.target.value); - }, - render: function () { - if (this.isValid()) { - isValid(); - } - return - } - }); - var form = TestUtils.renderIntoDocument( - - - - ); - - var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT'); - expect(isValid).not.toHaveBeenCalled(); - TestUtils.Simulate.change(input, {target: {value: '1.5'}}); - expect(isValid).toHaveBeenCalled(); - - }); - - it('RULE: isNumeric is false (string representation of an invalid float)', function () { - - var isValid = jasmine.createSpy('valid'); - var TestInput = React.createClass({ - mixins: [Formsy.Mixin], - updateValue: function (event) { - this.setValue(event.target.value); - }, - render: function () { - if (this.isValid()) { - isValid(); - } - return - } - }); - var form = TestUtils.renderIntoDocument( - - - - ); - - var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT'); - expect(isValid).not.toHaveBeenCalled(); - TestUtils.Simulate.change(input, {target: {value: '1.'}}); - expect(isValid).not.toHaveBeenCalled(); - - }); - - it('RULE: equalsField', function () { - - var TestInput = React.createClass({ - mixins: [Formsy.Mixin], - render: function () { - return - } - }); - var form = TestUtils.renderIntoDocument( - - - - - - ); - - var input = TestUtils.scryRenderedComponentsWithType(form, TestInput); - expect(input[0].isValid()).toBe(true); - expect(input[1].isValid()).toBe(false); - - }); - }); diff --git a/src/validationRules.js b/src/validationRules.js index 05fa3b2..0d30179 100644 --- a/src/validationRules.js +++ b/src/validationRules.js @@ -1,33 +1,36 @@ module.exports = { - 'isDefaultRequiredValue': function (values, value) { + isDefaultRequiredValue: function (values, value) { return value === undefined || value === ''; }, - 'hasValue': function (values, value) { - return value !== undefined; + hasValue: function (values, value) { + return !!value; }, - 'matchRegexp': function (values, value, regexp) { - return value !== undefined && !!value.match(regexp); + matchRegexp: function (values, value, regexp) { + return !!value && !!value.match(regexp); }, - 'isUndefined': function (values, value) { + isUndefined: function (values, value) { return value === undefined; }, - 'isEmptyString': function (values, value) { + isEmptyString: function (values, value) { return value === ''; }, - 'isEmail': function (values, value) { + isEmail: function (values, value) { return !value || value.match(/^((([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); }, - 'isTrue': function (values, value) { + isTrue: function (values, value) { return value === true; }, - 'isFalse': function (values, value) { + isFalse: function (values, value) { return value === false; }, - 'isNumeric': function (values, value) { + isNumeric: function (values, value) { + if (!value) { + return false; + } if (typeof value === 'number') { return true; } else { - var matchResults = value !== undefined && value.match(/[-+]?(\d*[.])?\d+/); + var matchResults = value.match(/[-+]?(\d*[.])?\d+/); if (!!matchResults) { return matchResults[0] == value; } else { @@ -35,17 +38,17 @@ module.exports = { } } }, - 'isAlpha': function (values, value) { - return !value || value.match(/^[a-zA-Z]+$/); + isAlpha: function (values, value) { + return value && /^[a-zA-Z]+$/.test(value); }, - 'isWords': function (values, value) { - return !value || value.match(/^[a-zA-Z\s]+$/); + isWords: function (values, value) { + return value && /^[a-zA-Z\s]+$/.test(value); }, - 'isSpecialWords': function (values, value) { + isSpecialWords: function (values, value) { return !value || value.match(/^[a-zA-Z\s\u00C0-\u017F]+$/); }, isLength: function (values, value, length) { - return value !== undefined && value.length === length; + return value && value.length === length; }, equals: function (values, value, eql) { return value == eql; @@ -54,9 +57,9 @@ module.exports = { return value == values[field]; }, maxLength: function (values, value, length) { - return value !== undefined && value.length <= length; + return value && value.length && value.length <= length; }, minLength: function (values, value, length) { - return value !== undefined && value.length >= length; + return value && value.length && value.length >= length; } };