Joi: Declarative Schemas for Data Validation
Joi lets you describe your data's shape with a readable schema instead of writing manual validation logic. It's used to validate API request bodies or config files.
Why it exists
Manual data validation is error-prone, repetitive, and hard to read. Checking if a field exists, is the right type, has a certain length, and relates to other fields creates nested if statements that are difficult to maintain. Joi was created to replace this imperative code with a single, declarative schema object that serves as the source of truth for your data's shape.
The mental model
Think of a Joi schema as a contract or a blueprint for your data. You don't write the steps to check the data; you describe what the final, valid data must look like. Joi is the inspector that takes your blueprint and checks if the data conforms to it, providing a detailed report of any violations. It separates the "what" (the schema) from the "how" (the validation engine).
How it works
You import Joi and create a schema using Joi.object(). Inside, you define keys and chain validation rules to them, like Joi.string().email().required(). To validate data, you call schema.validate(yourDataObject). This returns an object containing error and value. If error is null, validation passed. If not, the error object contains a rich, detailed list of every validation failure, including the field path, message, and type of error. You can also define complex relationships, like requiring one field if another is present (.with()) or making fields mutually exclusive (.xor()).
When to use it
Use Joi for validating any data where the structure is known but the content is untrusted. Its primary use case is in API backends (like Express or Hapi) to validate incoming request bodies, query parameters, and headers. It's also excellent for validating application configuration files or environment variables at startup to fail fast with clear errors.
When not to use it
Joi is for validation, not sanitization. While it can coerce types (e.g., convert a string "123" to the number 123), it's not a tool for removing malicious input like XSS attacks. For that, you need a dedicated sanitization library. For very simple, one-off checks, a quick typeof check might be sufficient, though a schema is often more robust long-term.
One canonical example
A common use case is validating a user registration form where a password and confirmation must match. The schema can use Joi.ref() to create this dependency without any custom logic. const schema = Joi.object({ password: Joi.string().min(8).required(), confirmPassword: Joi.any().valid(Joi.ref('password')).required() }); This declaratively states that confirmPassword must be identical to the value of password.
Interview question
What is the fundamental benefit of using Joi for data validation over writing custom imperative checks?
- a.It guarantees faster execution of validation rules due to optimized internal algorithms.
- b.It integrates directly with database schemas, ensuring data consistency across the application layers.
- c.It enables a declarative approach to defining data structures, enhancing readability and maintainability.Correct
- d.It automatically cleanses data, protecting against common injection attacks like XSS.
Why? this is the answer
Joi's primary advantage is replacing error-prone, repetitive imperative validation with a readable, declarative schema that acts as a blueprint for data. Option D is incorrect because the card explicitly states Joi is for validation, not sanitization against attacks like XSS.
Just read this? Test yourself on what you have been reading.
Read the original → joi.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on javascript — each one lists the topics its interview covers.
See open roles