xogroup / felicity

Javascript object constructors and sample data based on Joi schema.
Other
111 stars 9 forks source link

entityFor should not strip known keys with invalid values #87

Closed WesTyler closed 7 years ago

WesTyler commented 7 years ago

Context

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

When valid-shaped input with invalid values is provided to a constructor function from Felicity.entityFor, the values should not be stripped out of the input. Only unknown/disallowed keys should be stripped out of the input.

What result did you expect ?

const schema = Joi.object().keys({
    id: Joi.string().guid().required()
});

const Document = Felicity.entityFor(schema);

const input = { id: '1234567890', notes: [] };

const document = new Document(input); // { id: '1234567890' }

What result did you observe ?

const schema = Joi.object().keys({
    id: Joi.string().guid().required()
});

const Document = Felicity.entityFor(schema);

const input = { id: '1234567890', notes: [] };

const document = new Document(input); // { id: null }
WesTyler commented 7 years ago

Fix is going to be implementing a new entityFor option: strictInput, which will default to false to patch the expected behavior above.

Setting strictInput: true will cause this input validation to occur.

Whether strictInput is true or false, unknown keys on the input will always be stripped out.