Search code examples
typescriptjoi

Joi for return value validation


I have a factory that returns data from different services. The data from the raw source each factory service hits is mapped from its original format to a shared common format. Each service in the factory same type of data (car data). We want to map the data from different systems to a common format for use on the frontend.

We are having an issue where the schema for the different subsystems (Honda, Toyota, for example) are starting to deviate. Keys are sometimes missing. Other times we are adding extra keys. FE devs are starting to complain. They ALWAYS want to EXACT same schema.

I use Joi to validate API params. Is it weird to also use Joi to enforce a return schema? Id like to throw an error if the data is improperly mapped in one of my factory’s services


Solution

  • It is not weird to validate the return values also.

    Just need to take into account that you have a performance cost associated with this.

    I'm validating return values at the API level (using hapi).