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() causes errors when nothing has to be renamed #1854

Closed
aron123 opened this issue Jun 8, 2019 · 2 comments
Closed

.rename() causes errors when nothing has to be renamed #1854

aron123 opened this issue Jun 8, 2019 · 2 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@aron123
Copy link

aron123 commented Jun 8, 2019

Context

  • node version: v10.14.0
  • joi version: 15.0.3
  • environment: node
  • used with: standalone

What are you trying to achieve or the steps to reproduce ?

const Joi = require('@hapi/joi');

const regex = /^foobar$/i; //case-insensitive match!

const schema1 = Joi.object().keys({
    fooBar: Joi.string().required()
}).rename(regex, 'fooBar');

const schema2 = Joi.object().keys({
    fooBar: Joi.string().required()
}).rename(regex, 'fooBar', { override: true });

const handler = (err, val) => {
    if (err) return console.error(err);
    console.log(val);
};

console.log('1. ---------------------');
schema1.validate({ FooBar: 'a'}, handler);
console.log('2. ---------------------');
schema1.validate({ fooBar: 'a'}, handler);
console.log('3. ---------------------');
schema2.validate({ FooBar: 'a'}, handler);
console.log('4. ---------------------');
schema2.validate({ fooBar: 'a'}, handler);

Which result you had ?

  1. { fooBar: 'a' }
  2. ValidationError: '"value" cannot rename children [fooBar] because override is disabled and target "fooBar" exists' (type: 'object.rename.regex.override')
  3. { fooBar: 'a' }
  4. ValidationError: '"fooBar" is required' (type: 'any.required')

The validation is successful, when FooBar field is passed, but fails every time, when I pass object with field fooBar.

What did you expect ?

I want to define a schema, that accepts fields case insensitively, e.g. I want to generate { fooBar: 'a' } output from every of the following inputs without failure:

  • { fooBar: 'a' }
  • { FooBar: 'a' }
  • { FoOBaR: 'a' }
@aron123
Copy link
Author

aron123 commented Jun 9, 2019

I tested it today with the current state of master branch, and it now works as expected. I think it is fixed in #1770, so I'm waiting for the next release to come out.

Sorry for the duplication.

@aron123 aron123 closed this as completed Jun 9, 2019
@Marsup
Copy link
Collaborator

Marsup commented Jun 9, 2019

As a temporary fix you can use the alias setting in current version, but be careful that it'll result in potentially having duplicate properties with different casing.

@Marsup Marsup self-assigned this Jun 9, 2019
@Marsup Marsup added the bug Bug or defect label Jun 9, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone Jun 9, 2019
@hueniverse hueniverse assigned hueniverse and unassigned Marsup Jun 9, 2019
@hueniverse hueniverse added the v16 label Aug 11, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 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

3 participants