Skip to content

stripUnknown: What is the expected behavior when validating nested data? (array) #903

@sberryman

Description

@sberryman

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.

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions