From aee25d067a6232fc74d15e153f02eb494dbd247a Mon Sep 17 00:00:00 2001 From: EnRoute Date: Sat, 3 Jun 2017 20:08:02 +0300 Subject: [PATCH] fix(types): Fix StringType validators working pipeline --- src/types/strign.test.js | 13 ++++++++++++- src/types/string.js | 9 +++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/types/strign.test.js b/src/types/strign.test.js index 861ca49..a10a3ca 100644 --- a/src/types/strign.test.js +++ b/src/types/strign.test.js @@ -39,7 +39,7 @@ describe('string type', () => { it('check allowed string list validator', () => { const stringType = new StringType({ allowed: ['allow1', 'allow2'] }); - expect(stringType.validators).to.be.have.length(3); + expect(stringType.validators).to.be.have.length(2); expect(stringType.validators[1]('allow1')).to.be.equal(true); expect(stringType.validators[1]('allow2')).to.be.equal(true); expect(stringType.validators[1]('disallow string')).to.be.equal(false); @@ -48,10 +48,21 @@ describe('string type', () => { it('check allowed regexp list validator', () => { const stringType = new StringType({ allowed: [/^[A-Z]+$/, /^\d+$/] }); + expect(stringType.validators).to.be.have.length(2); + expect(stringType.validators[1]('deny value')).to.be.equal(false); + expect(stringType.validators[1]('AZA')).to.be.equal(true); + expect(stringType.validators[1]('125')).to.be.equal(true); + expect(stringType.validators[1]('125abc')).to.be.equal(false); + }); + + it('check allowed regexp and string list validator', () => { + const stringType = new StringType({ allowed: ['abc', /^[A-Z]+$/, /^\d+$/] }); + expect(stringType.validators).to.be.have.length(3); expect(stringType.validators[2]('deny value')).to.be.equal(false); expect(stringType.validators[2]('AZA')).to.be.equal(true); expect(stringType.validators[2]('125')).to.be.equal(true); expect(stringType.validators[2]('125abc')).to.be.equal(false); + expect(stringType.validators[1]('abc')).to.be.equal(true); }); }); diff --git a/src/types/string.js b/src/types/string.js index ebeb1d0..35dba11 100644 --- a/src/types/string.js +++ b/src/types/string.js @@ -41,8 +41,13 @@ class StringType extends ConfigFieldBaseType { } } - this.validators.push(val => this.allowedStrings.includes(val)); - this.validators.push(val => this.allowedRegExp.some(regexp => !!val.match(regexp))); + if (this.allowedStrings.length > 0) { + this.validators.push(val => this.allowedStrings.includes(val)); + } + + if (this.allowedRegExp.length > 0) { + this.validators.push(val => this.allowedRegExp.some(regexp => !!val.match(regexp))); + } } } }