hapijs / hapijs/joi

Generic schema adapter mechanism for deriving Joi schemas

Open
#3,154 0 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

### Runtime

node.js

### Runtime version

v26.8.2

### Module version

joi@18.2.9

### Used with

sequelize@6.37.8

### Any other relevant information

_No response_

### What problem are you trying to solve?

Hi,

I'm coming from the C# ecosystem, where validation rules are often defined directly on models.

While working with Sequelize and Joi, I noticed that Sequelize already allows validation rules to be defined directly on model attributes:

const User = sequelize.define("User", {
email: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isEmail: true
}
}
});

At the HTTP/API layer, I may then define the same rules again with Joi:

const schema = Joi.object({
email: Joi.string().email().required()
});

This can lead to the same validation rules being expressed twice.

I was wondering whether Joi could provide a generic mechanism for deriving a Joi schema from another schema/model definition through an adapter.

For example, conceptually:

const joiSchema = Joi.fromSchemaUsing(sequelizeAdapter, User);

The important part for me is the adapter approach rather than a direct Sequelize integration.

This could potentially allow different integrations without coupling Joi to a specific ORM or validation system:

Joi.fromSchemaUsing(sequelizeAdapter, User);
Joi.fromSchemaUsing(otherAdapter, Model);

For Sequelize, some rules could be mapped relatively naturally:

allowNull → required()
STRING(n) → string().max(n)
validate.isEmail → email()
validate.len → min() / max()

I understand that not every Sequelize validator could be translated, especially custom validation functions. The adapter could therefore support only the rules that have a meaningful Joi equivalent.

I'm not necessarily suggesting that this should be implemented directly inside Joi. A separate adapter package or extension mechanism could also make sense.

I'd be interested to know whether this kind of generic adapter mechanism would fit the design philosophy of Joi, or if there are existing approaches that I'm overlooking.

Thanks!

### Do you have a new or modified API suggestion to solve the problem?

_No response_

Contributor guide

Open the contributing guide

Research direction

The issue names no files or tests. Start by reviewing Joi's public API and extension mechanisms, then assess whether a generic adapter can map Sequelize rules without coupling Joi to an ORM. Done would require a concrete API or extension scope with acceptance criteria, or a documented decision that this belongs in a separate adapter package.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.