I am creating an API using HAPI and Joi to validate inputs and I am having troubles with sharing my validation schema between different modules. I am using a component-oriented architecture that looks like that
components
|_ moduleA
|_ moduleAController
|_ moduleAModel
|_ moduleARoute
|_ moduleAValidate
|_ moduleB
|_ moduleBController
|_ moduleBModel
|_ moduleBRoute
|_ moduleBValidate
|_ moduleC
...
In each module, moduleXRoute
create a route associating a handler from moduleXController
and a validator from moduleXValidate
.
As I am using Joi, I am performing some tests on input data and that where comes the problem, my moduleA
keeps a list of moduleB
and my moduleB
keeps a reference to moduleA
, thus this implies in the validators :
var moduleASchema = {
_id: Joi.objectId(),
name: Joi.string().required(),
moduleB: Joi.array().items(Joi.alternatives().try(Joi.objectId(), moduleBSchema)),
};
var moduleBSchema = {
_id: Joi.objectId(),
name: Joi.string().required(),
moduleA: Joi.alternatives().try(Joi.objectId(), moduleASchema),
};
That's why, I think it would be a good idea that moduleAValidate
and moduleBValidate
expose moduleASchema
and moduleBSchema
that other modules could use.
The problem is that it makes a circular dependency problem because in the case above, I would have:
//moduleAValidate.js
var moduleBSchema = require('../moduleBValidate').moduleBschema;
//moduleBValidate.js
var moduleASchema = require('../moduleAValidate').moduleAschema;
Thus, what would be the good way to handle the problem ?
I found the easy way would be to centralize all schemas in a single file that could be required in all validators, but I feel it like it would just be in contradiction with component-architecture.