Skip to content

Commit

Permalink
Merge pull request #1532 from rokoroku/patch-1
Browse files Browse the repository at this point in the history
Add enabled flag to string.trim()
  • Loading branch information
Marsup committed Jul 30, 2018
2 parents bcc5f12 + 3414eb7 commit 3372df0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
8 changes: 6 additions & 2 deletions API.md
Expand Up @@ -136,7 +136,7 @@
- [`string.normalize([form])`](#stringnormalizeform)
- [`string.lowercase()`](#stringlowercase)
- [`string.uppercase()`](#stringuppercase)
- [`string.trim()`](#stringtrim)
- [`string.trim([enabled])`](#stringtrimenabled)
- [`string.isoDate()`](#stringisodate)
- [`alternatives` - inherits from `Any`](#alternatives---inherits-from-any)
- [`alternatives.try(schemas)`](#alternativestryschemas)
Expand Down Expand Up @@ -2155,13 +2155,17 @@ will be forced to uppercase.
const schema = Joi.string().uppercase();
```

#### `string.trim()`
#### `string.trim([enabled])`

Requires the string value to contain no whitespace before or after. If the validation `convert` option is on (enabled by
default), the string will be trimmed.

Parameters are:
- `enabled` - optional parameter defaulting to `true` which allows you to reset the behavior of trim by providing a falsy value.

```js
const schema = Joi.string().trim();
const schema = Joi.string().trim(false); // disable trim flag
```

#### `string.isoDate()`
Expand Down
28 changes: 19 additions & 9 deletions lib/types/string/index.js
Expand Up @@ -572,20 +572,30 @@ internals.String = class extends Any {
return obj;
}

trim() {
trim(enabled) {

const obj = this._test('trim', undefined, function (value, state, options) {
enabled = enabled === undefined ? true : enabled;
Hoek.assert(typeof enabled === 'boolean', 'option must be a boolean');

if (options.convert ||
value === value.trim()) {
let obj;
if (enabled) {
obj = this._test('trim', undefined, function (value, state, options) {

return value;
}
if (options.convert ||
value === value.trim()) {

return this.createError('string.trim', { value }, state, options);
});
return value;
}

return this.createError('string.trim', { value }, state, options);
});
}
else {
obj = this.clone();
obj._tests = obj._tests.filter((test) => test.name !== 'trim');
}

obj._flags.trim = true;
obj._flags.trim = enabled;
return obj;
}

Expand Down
29 changes: 29 additions & 0 deletions test/types/string.js
Expand Up @@ -1531,6 +1531,27 @@ describe('string', () => {
], { convert: false });
});

it('disable existing trim flag when passing enabled: false', () => {

const trimEnabledSchema = Joi.string().trim(true);
Helper.validateOptions(trimEnabledSchema, [
[' something', false, null, {
message: '"value" must not have leading or trailing whitespace',
details: [{
message: '"value" must not have leading or trailing whitespace',
path: [],
type: 'string.trim',
context: { value: ' something', label: 'value', key: undefined }
}]
}]
], { convert: false });

const trimDisabledSchema = trimEnabledSchema.trim(false);
Helper.validateOptions(trimDisabledSchema, [
[' something', true]
], { convert: false });
});

it('removes leading and trailing whitespace before validation', async () => {

const schema = Joi.string().trim();
Expand Down Expand Up @@ -1654,6 +1675,14 @@ describe('string', () => {
['ABC', true]
]);
});

it('throws when option is not a boolean', () => {

expect(() => {

Joi.string().trim(42);
}).to.throw('option must be a boolean');
});
});

describe('replace()', () => {
Expand Down

0 comments on commit 3372df0

Please sign in to comment.