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: What is the expected behavior when validating nested data? (array) #903

Closed
sberryman opened this issue May 26, 2016 · 3 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@sberryman
Copy link

Context

  • node version: 5.11.1
  • joi version: 8.1.1
  • environment: node:
  • used with: standalone
  • any other relevant information: Not expected behavior

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

I'm expecting an error when using { stripUnknown: true }

'use strict';

const Joi = require('joi');

const schema = Joi.object().keys({
    emails: Joi.array()
            .items(
                Joi.object().keys({
                    value: Joi.string().email()
                })
            )
});

const data = {
    emails: [{
        value: 'hagrid@ potterworld'
    }, {
        value: 'hagrid@harrypotterworld.com'
    }]
};

const resultDefault = Joi.validate(data, schema);
console.log(require('util').inspect(resultDefault, { depth: 3 }));

const resultStripUnknown = Joi.validate(data, schema, { stripUnknown: true });
console.log(require('util').inspect(resultStripUnknown, { depth: 3 }));

Which result you had ?

[resultDefault]  { error: 
   { [ValidationError: child "emails" fails because ["emails" at position 0 fails because [child "value" fails because ["value" must be a valid email]]]]
     isJoi: true,
     name: 'ValidationError',
     details: 
      [ { message: '"value" must be a valid email',
          path: 'emails.0.value',
          type: 'string.email',
          context: [Object] } ],
     _object: { emails: [ [Object], [Object] ] },
     annotate: [Function] },
  value: 
   { emails: 
      [ { value: 'hagrid@ potterworld' },
        { value: 'hagrid@harrypotterworld.com' } ] } }
[resultStripUnknown]  { error: null,
  value: { emails: [ { value: 'hagrid@harrypotterworld.com' } ] } }

What did you expect ?

I expect both results to be an error. Considering I'm specifically testing the value inside an array I was expecting the behavior to be an error, instead the value was removed. It may very well be intentional, I was just confused by the behavior.

@Marsup Marsup added the request label May 28, 2016
@Marsup Marsup self-assigned this May 28, 2016
@Marsup Marsup added this to the 8.3.0 milestone May 28, 2016
Marsup added a commit that referenced this issue May 28, 2016
Fixes #903, #904, and probably many others.
@Marsup Marsup closed this as completed May 28, 2016
@Marsup
Copy link
Collaborator

Marsup commented May 28, 2016

Will release in a few hours if I stop working on joi for today.

@sberryman
Copy link
Author

Wow, thanks for the extremely quick fix/solution on this!

@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 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
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants