Skip to content

Commit

Permalink
fix(types): Fix StringType validators working pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
CheerlessCloud committed Jun 3, 2017
1 parent 2b31d3e commit aee25d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/types/strign.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
});
9 changes: 7 additions & 2 deletions src/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
}
}
Expand Down

0 comments on commit aee25d0

Please sign in to comment.