Skip to content

Commit

Permalink
Cleanup for #1532.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Jul 30, 2018
1 parent 3372df0 commit 334c1e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/types/string/index.js
Expand Up @@ -572,11 +572,14 @@ internals.String = class extends Any {
return obj;
}

trim(enabled) {
trim(enabled = true) {

enabled = enabled === undefined ? true : enabled;
Hoek.assert(typeof enabled === 'boolean', 'option must be a boolean');

if ((this._flags.trim && enabled) || (!this._flags.trim && !enabled)) {
return this;
}

let obj;
if (enabled) {
obj = this._test('trim', undefined, function (value, state, options) {
Expand Down
12 changes: 12 additions & 0 deletions test/types/string.js
Expand Up @@ -1495,6 +1495,18 @@ describe('string', () => {

describe('trim()', () => {

it('avoids unnecessary cloning when called twice', () => {

const schema1 = Joi.string().trim();
expect(schema1.trim()).to.shallow.equal(schema1);

const schema2 = Joi.string().trim(false);
expect(schema2.trim(false)).to.shallow.equal(schema2);

const schema3 = Joi.string();
expect(schema3.trim(false)).to.shallow.equal(schema3);
});

it('only allow strings that have no leading or trailing whitespace', () => {

const schema = Joi.string().trim();
Expand Down

0 comments on commit 334c1e3

Please sign in to comment.