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

Column not created using when clause #100

Open
DSheffield opened this issue Jun 5, 2018 · 1 comment
Open

Column not created using when clause #100

DSheffield opened this issue Jun 5, 2018 · 1 comment

Comments

@DSheffield
Copy link

DSheffield commented Jun 5, 2018

I've written a schema as follows in which my goal is that the 'itemOrder' array should always have the same length as the items attr / relationship. When I add the 'when' clause, however, the itemOrder column does not get created (using postgres). If I omit the 'when' clause it does get created. Not sure if this is a bug or if I'm not using / understanding the when clause correctly. Thanks for any help you can offer.

jsonApi.define({
    resource: 'itemSets',
    handlers: resourceHandlers.itemSets,
    attributes: {
        title: jsonApi.Joi.string().required(),
        items: jsonApi.Joi.many('items').required(),
        itemOrder: jsonApi.Joi.array().required()
                            .items(jsonApi.Joi.string().uuid())
                            .when('items', {
                                is: jsonApi.Joi.exist(),
                                then: jsonApi.Joi.array().length(jsonApi.Joi.ref('items.length')),
                                otherwise: jsonApi.Joi.array().length(0)
                            })
    }
});
@Exlipse7
Copy link

Exlipse7 commented Jun 14, 2018

I'm having the same issue with mysql. Without .when it works fine, with .when it drops the column. Even if I set force: false with the column already created, it doesn't handle the field properly. What I mean is that my 'score' field shows as empty in the response even though it's populated in the request and partialResponse objects. I've also verified that this schema works correctly using Joi directly (without jsonapi-server).

{
    status: Joi.string().regex(/^queued$|^cancelled$|^processing$|^failed$|^completed$/)
                        .description("The processing state: queued|cancelled|processing|failed|completed"),
    company: Joi.string().required().description("The company the individual is related to."),
    individual: Joi.string().required().description("The individual to initiate on."),
    score: Joi.number().integer().valid([1,2,3,4])
                        .description("The normalized score. 1..4 => low..high risk")
                        .when('status', {is: Joi.string().regex(/^completed$/).required(),
                                        then: Joi.required(),
                                        otherwise: Joi.allow(null).optional()}),
    expires: Joi.date().iso()
                        .description("The date after which the score is no longer "+
                                     "valid.")
                        .when('status', {is: Joi.string().regex(/^completed$/).required(),
                                        then: Joi.required(),
                                        otherwise: Joi.allow(null).optional()}),
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants