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

stripUnknown doesn't work for nested objects #2459

Closed
Koala-gentil opened this issue Sep 2, 2020 · 3 comments · Fixed by #2518
Closed

stripUnknown doesn't work for nested objects #2459

Koala-gentil opened this issue Sep 2, 2020 · 3 comments · Fixed by #2518
Assignees
Labels
bug Bug or defect
Milestone

Comments

@Koala-gentil
Copy link

Koala-gentil commented Sep 2, 2020

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: v14.6.0
  • module version with issue:
  • last module version without issue:
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

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

It looks like stripUnknown doesn't works for nested object when the nested object has invalid fields.

const schema = Joi.object({
    foo: Joi.string(),
    nested: Joi.object({
        bar: Joi.string(),
    })
})

const options = {
    abortEarly: false,
    allowUnknown: true,
    presence: 'required',
    stripUnknown: true,
}

const input = {
    foo: null, // invalid value
    unknown: 'unknown',
    nested: {
        bar: null, // invalid value
        unknown: 'unknown',
    }
}

const { value } = schema.validate(input, options)
// { foo: null, nested: { bar: null, unknown: 'unknown' } }

What was the result you got?

nested.unknow is still present

What result did you expect?

It should have been "stripped"

@Koala-gentil Koala-gentil changed the title stripUnknown doesn't works for nested objects stripUnknown doesn't work for nested objects Sep 2, 2020
@hueniverse
Copy link
Contributor

If validation fails, joi will just stop what it was doing and return an error.

@hueniverse hueniverse added the support Questions, discussions, and general support label Sep 12, 2020
@hueniverse hueniverse self-assigned this Sep 12, 2020
@Koala-gentil
Copy link
Author

unless the abortEarly option has been set to false.

@Koala-gentil
Copy link
Author

In this case everything work as expected, the validation fail but continue and strip unknown fields

const Joi = require('joi');

const schema = Joi.object({
    foo: Joi.string(),
    nested: Joi.object({
        bar: Joi.string(),
    })
})

const options = {
    abortEarly: false,
    allowUnknown: true,
    presence: 'required',
    stripUnknown: true,
}

const input = {
    foo: null, // invalid value
    unknown: 'unknown',
    nested: {
        bar: 'bar', // valid value
        unknown: 'unknown',
    }
}

const { value } = schema.validate(input, options)
// { foo: null, nested: { bar: 'bar' } }

There is definitely an inconsistent behavior with nested objects and stripUnknown when abortEarly is false.
Please re-open @hueniverse

@hueniverse hueniverse reopened this Sep 12, 2020
@hueniverse hueniverse added bug Bug or defect and removed support Questions, discussions, and general support labels Feb 8, 2021
@hueniverse hueniverse added this to the 17.4.0 milestone Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants