xogroup / felicity

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

Add support for .example with input #95

Closed WesTyler closed 7 years ago

WesTyler commented 7 years ago

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

Allow .example methods to accept an input parameter. Valid input will be used instead of randomly-generated data.

const schema = Joi.object().keys({
    people: Joi.object().pattern(/^[0-9a-fA-F]{8}$/, Joi.object().keys({
        id  : Joi.string().guid().required(),
        tags: Joi.array().items(Joi.string())
    })).min(2).required()
});

const Pairing = Felicity.entityFor(schema);

const commonPerson = {
    people: {
        adc7d80c: {
            id: 'adc7d80c-f78c-44f1-94c6-73e816f5aa84',
            tags: ['admin']
        }
    }
};

const semiRandom = Pairing.example(commonPerson);
/*
{
    people: {
        adc7d80c: {
            id: 'adc7d80c-f78c-44f1-94c6-73e816f5aa84',
            tags: ['admin']
        },
        0ac079ea: {
            id: '0ac079ea-e92e-4d41-b6b5-ed493008e807',
            tags: ['da34fad']
        }
    }
}
*/
lamchakchan commented 7 years ago

I'm torn about this feature. Would it not be the same if not more flexible to allow the end user to do this themselves outside the scope of Felicity. What type of regulations needs to be imposed around this? In your example, if the id is not provided, should commonPerson still be added since it does not comply with the defined schema?

Seems like a lot to do when the user can just write something like this.



const random = Pairing.example();
const random.people['adc7d80c'] = {
            id: 'adc7d80c-f78c-44f1-94c6-73e816f5aa84',
            tags: ['admin']
        } ;
WesTyler commented 7 years ago

My plan was to follow a similar process to entityFor with input. In the case of data that does not comply with the defined schema, it will not be added.

I do hear you though. I'll leave this open for discussion, but it may end up not being worth building into the Felicity source.

WesTyler commented 7 years ago

I think you're right. Rules for partial inclusion of data started getting hairy pretty quickly as I was putting together a spike for this.

I'm going to close it out and revisit only if users in the future start requesting this functionality.