Skip to content

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

@aron123

Description

@aron123

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' }

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions