Skip to content

Don't delete object key when it equals the rename.to in a rename with regex#1770

Closed
paixaop wants to merge 1 commit intohapijs:masterfrom
paixaop:rename-fix
Closed

Don't delete object key when it equals the rename.to in a rename with regex#1770
paixaop wants to merge 1 commit intohapijs:masterfrom
paixaop:rename-fix

Conversation

@paixaop
Copy link

@paixaop paixaop commented Mar 30, 2019

Separating out the rename fix from thekeysToLowerCase

Description copied from original pull request #1763, for easier reference.

Calling rename with a regex where the key matches the rename.to causes the key to get deleted from the object.

Example: I use the folllowing schema to validate an AWS x-api-key header:

Joi.object()
    .keys({
        'x-api-key': stringsSchema.awsApiKey.required(),
    })
    .rename(/x-api-key/i, 'x-api-key', {
        // alias: true,
        override: true,
        ignoreUndefined: true,
    })

Using rename to force a value such as 'X-ApI-KeY' to be all lower case, and therefore normalizing it to 'x-api-key'.
The bug is when the header is already all lower case 'x-api-key', the rename.from is the same as rename.to so it gets deleted if option.alias = false, leaving the object with no x-api-key at all.

So this:

const Joi = require('joi');

const schema = Joi.object()
    .keys({
        'x-api-key': Joi.string().token().min(30).max(128),
    })
    .rename(/x-api-key/i, 'x-api-key', {
        // alias: true,
        override: true,
        ignoreUndefined: true,
    });

const result = Joi.attempt({
      'x-api-key': 'ABCEFGHIJKLMNOPQRSTUVXYWZabcefghijklmnopqrstuvxywz0123456789'
 }, schema);

console.log(result); // prints { }

…ed the original key to be deleted if rename.options.alias was false
@hueniverse hueniverse self-assigned this May 29, 2019
@hueniverse hueniverse added the bug Bug or defect label May 29, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone May 29, 2019
@hueniverse hueniverse added the v16 label Aug 10, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@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