Skip to content

Joi should not strip non-enumerable properties of an object #1696

@pierreis

Description

@pierreis

Context

  • node version: 11.5.0
  • joi version: 10.6.0
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone

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

Consider the following example code:

const joi = require('joi');
const obj = {foo: 'bar'};
Object.defineProperty(obj, 'test', { value: 42, enumerable: false });
joi.validate(obj, joi.object().keys({}).unknown()).value
// >> { foo: 'bar' }
joi.validate(obj, joi.object().keys({}).unknown()).value.test
// >> undefined

Which result you had ?

Joi strips non-enumerable properties of the object.

What did you expect ?

Unknown properties being allowed, the object should be returned unaffected. Own object properties should not be stripped from the returned value.

Use-case

I do have a validator that takes an object, and validates that one of its values is an ORM model:

Joi.object().type(MyModel)

This works fine. The ORM defines on the model a few methods, that are not enumerable, and these are kept at this point.

If, however, I try to validate one of the values of a model instance:

Joi.object().type(MyModel).keys({ something: Joi.string() }).unknown()

The ORM-defined methods are stripped from the resulting value.

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