Skip to content

Allows using any.rename() and any.default() together#201

Merged
hueniverse merged 3 commits intohapijs:masterfrom
paulillo:develop
Mar 20, 2014
Merged

Allows using any.rename() and any.default() together#201
hueniverse merged 3 commits intohapijs:masterfrom
paulillo:develop

Conversation

@paulillo
Copy link
Contributor

Example:

With this Joi object: { foo: Joi.string().rename('foo2').default('test') }
And the input value: {}

The validation should output:

{
  foo : 'test',
  foo2: 'test'
}

But was outputting:

{
  foo: 'test',
  foo2: undefined
}

@hueniverse
Copy link
Contributor

This is not the right solution. It should first set the default value in _validate and then do the rename.

@paulillo
Copy link
Contributor Author

Sure thing! I will fix that!

@paulillo
Copy link
Contributor Author

Hey @hueniverse, Changing the order of the Any._validate.finish() implementation to apply the mutators after setting the value, works properly. What do you think?

    var finish = function () {

        if (!errors.length &&
            state.parent &&
            ((options.modify && state.parent.hasOwnProperty(state.key)) || (value === undefined && self._flags.default !== undefined))) {

            state.parent[state.key] = (value !== undefined ? value : self._flags.default);
        }

        // Apply mutators as long as there are no errors

        for (var m = 0, ml = self._mutators.length; m < ml && !errors.length; ++m) {
            var err = self._mutators[m](value, state, options);
            if (err) {
                errors.push(err);
            }
        }

        // Return null or errors

        return errors.length ? errors : null;
    };

@hueniverse hueniverse added this to the 2.9.0 milestone Mar 20, 2014
@hueniverse hueniverse added the bug label Mar 20, 2014
@hueniverse hueniverse self-assigned this Mar 20, 2014
hueniverse pushed a commit that referenced this pull request Mar 20, 2014
Allows using any.rename() and any.default() together
@hueniverse hueniverse merged commit 14a88eb into hapijs:master Mar 20, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Bug or defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants