From fdfb9bc71b8e68d82a42b48bcaf5ec34d341f34b Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Fri, 18 Mar 2016 11:44:15 +0000 Subject: [PATCH] Updated phone validator to return empty for null or undefined objects --- .jscsrc | 46 +++++++++++++++++++++++++++++++++++++++++ lib/md-validator.js | 5 +++++ test/phone-validator.js | 10 +++++++++ 3 files changed, 61 insertions(+) create mode 100644 .jscsrc diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 0000000..480fcc4 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,46 @@ +{ + "disallowKeywords": ["with"], + "disallowKeywordsOnNewLine": ["else"], + "disallowMixedSpacesAndTabs": true, + "disallowMultipleVarDecl": "exceptUndefined", + "disallowNewlineBeforeBlockStatements": true, + "disallowQuotedKeysInObjects": true, + "disallowSpaceAfterObjectKeys": true, + "disallowSpaceAfterPrefixUnaryOperators": true, + "disallowSpacesInFunction": { + "beforeOpeningRoundBrace": true + }, + "disallowSpacesInsideParentheses": true, + "disallowTrailingWhitespace": true, + "maximumLineLength": 160, + "requireCamelCaseOrUpperCaseIdentifiers": false, + "requireCapitalizedComments": true, + "requireCapitalizedConstructors": true, + "requireCurlyBraces": true, + "requireSpaceAfterKeywords": [ + "if", + "else", + "for", + "while", + "do", + "switch", + "case", + "return", + "try", + "catch", + "typeof" + ], + "requireSpaceAfterLineComment": true, + "requireSpaceAfterBinaryOperators": true, + "requireSpaceBeforeBinaryOperators": true, + "requireSpaceBeforeBlockStatements": true, + "requireSpaceBeforeObjectValues": true, + "requireSpacesInFunction": { + "beforeOpeningCurlyBrace": true + }, + "requireTrailingComma": false, + "requireEarlyReturn": false, + "validateIndentation": 2, + "validateLineBreaks": "LF", + "validateQuoteMarks": "'" +} diff --git a/lib/md-validator.js b/lib/md-validator.js index 1801afe..523e83e 100644 --- a/lib/md-validator.js +++ b/lib/md-validator.js @@ -265,6 +265,11 @@ VALIDATE = new function() { this.validatePhone = function(params) { var onlyDigits; var uk, v, required, itrx; + + if ((typeof params === 'undefined') || (typeof params === 'object' && params === null)) { + return ''; + } + if (typeof params === 'object') { uk = params.uk || false; v = params.number; diff --git a/test/phone-validator.js b/test/phone-validator.js index caa0795..2bc7144 100644 --- a/test/phone-validator.js +++ b/test/phone-validator.js @@ -9,6 +9,16 @@ describe('Phone Validator', function() { done(); }); + it('should not validate an non existing object', function(done) { + assert.equal($V.validatePhone(), ''); + done(); + }); + + it('should not validate an null object', function(done) { + assert.equal($V.validatePhone(null), ''); + done(); + }); + it('should validate an normal uk number', function(done) { assert.equal($V.validatePhone({number: '01389 602001', uk: true}), '01389602001');