hemerajs / hemera

🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/
MIT License
806 stars 70 forks source link

[hemera-joi] Validate the whole pattern #24

Closed acehko closed 7 years ago

acehko commented 7 years ago

Is it possible to validate the whole pattern using a custom function like in seneca-joi? Example:

seneca.add({
    a: 1,
    joi$: function (schema) {
        return schema.keys({ b: Joi.required() })
    }
})
StarpTech commented 7 years ago

Hi @acehko yes, you can seperate your data from the pattern with an extra property and validate it. We also could implement custom functions easily.

seneca.add({
    topic: 'math',
    cmd: 'add',
    data: Joi.object().keys({ b: Joi.required() })
})
acehko commented 7 years ago

@StarpTech I don't want to separate the data like that. I just want the validation for the whole pattern to be in one "key" in the pattern. Like the joi$ property in seneca-joi. What I want is something like this:

hemera.add({
    topic : 'math',
    cmd : 'add',
    joi$ : function(schema) {
        return schema.keys({
            a : Joi.number().required(),
            b : Joi.number().required()
        });
    }
}

The main reason is to be able to separate the validation functions to another module. eg.:

hemera.add({ topic : 'math', cmd : 'add', joi$: require('./validation/add') });
StarpTech commented 7 years ago

With hemera-joi 0.1.71 you can pass full schema for the action by the joi$ property

Changes: https://github.com/hemerajs/hemera/blob/master/packages/hemera-joi/index.js#L23

let Joi = hemera.exposition['hemera-joi'].joi

hemera.add({
    topic : 'math',
    cmd : 'add',
    joi$ :  require('./validation/add')
})
acehko commented 7 years ago

That was fast, thanks :)