Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.rename() does not work when new value is undefined #593

Closed
mgartner opened this issue Mar 8, 2015 · 3 comments
Closed

.rename() does not work when new value is undefined #593

mgartner opened this issue Mar 8, 2015 · 3 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@mgartner
Copy link
Contributor

mgartner commented Mar 8, 2015

I have a case where I want to rename a field to override another field. If the value of the field being renamed is undefined, renaming does not work.

var Joi = require('joi');

var obj = { deleted: true }

var schema = Joi.object().rename('other', 'deleted', { override: true }).keys({ deleted: Joi.any().optional() });

Joi.validate(obj, schema, { stripUnknown: true }).value;
// => { deleted: true } instead of { }
@mgartner
Copy link
Contributor Author

mgartner commented Mar 8, 2015

Also, is there a work around where I can convert a boolean into either true, or undefined? If the value is false, I basically want the key to be removed from the output object. That's really my overall goal.

@Marsup Marsup added the non issue Issue is not a problem or requires changes label Mar 8, 2015
@Marsup Marsup self-assigned this Mar 8, 2015
@Marsup Marsup added bug Bug or defect and removed non issue Issue is not a problem or requires changes labels Mar 8, 2015
@Marsup Marsup added this to the 6.0.4 milestone Mar 8, 2015
@Marsup Marsup closed this as completed in c46617d Mar 8, 2015
@Marsup
Copy link
Collaborator

Marsup commented Mar 8, 2015

This should be fixed in 6.0.4.
If I understood you correctly, your schema should work like this :

var schema = Joi.object()
                .keys({
                    deleted: [
                        Joi.only(false).strip(),
                        Joi.boolean()
                    ]
                })
                .rename('other', 'deleted', { override: true });

This is using alternatives to strip the value you don't want.

@mgartner
Copy link
Contributor Author

mgartner commented Mar 9, 2015

Thanks!

@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 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

No branches or pull requests

2 participants