Skip to content

Commit

Permalink
Add assertion for string.trim()
Browse files Browse the repository at this point in the history
  • Loading branch information
rokoroku committed Jun 30, 2018
1 parent 77012b2 commit 0a82b61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/types/string/index.js
Expand Up @@ -531,7 +531,9 @@ internals.String = class extends Any {

trim(enabled) {

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

let obj;
if (enabled) {
obj = this._test('trim', undefined, function (value, state, options) {
Expand Down
31 changes: 24 additions & 7 deletions test/types/string.js
Expand Up @@ -1533,13 +1533,22 @@ describe('string', () => {

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

const schema = Joi.string().trim().trim(false);
Helper.validateOptions(schema, [
[' something', true],
['something ', true],
['something\n', true],
['some thing', true],
['something', true]
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 });
});

Expand Down Expand Up @@ -1666,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 0a82b61

Please sign in to comment.