Object schema description language and validator for JavaScript objects.
Questions tagged [joi]
1003 questions
106
votes
7 answers
Joi validation of array
trying to validate that an array has zero or more strings in one case and that it has zero or more objects in another , struggling with Joi docs :(
validate: {
headers: Joi.object({
'content-type': "application/vnd.api+json",
…

1977
- 2,580
- 6
- 26
- 37
85
votes
3 answers
Allow only specific values for key in Joi schema
Is there any other way to set specific values in Joi validation schema for key except regex pattern?
My example schema:
const schema = joi.object().keys({
query: joi.object().keys({
// allow only apple and banana
id:…

mardok
- 2,135
- 2
- 23
- 37
85
votes
17 answers
Node.js + Joi how to display a custom error messages?
It seems pretty straight forward to validate user's input in Node.js RESTapi with Joi.
But the problem is that my app is not written in English.
That means I need to send a custom written messages to the frontend user.
I have googled for this and…

Raz
- 1,910
- 1
- 15
- 28
80
votes
6 answers
How to use enum values with Joi String validation
I am using the Joi validator for my HTTP requests. There I have a parameter called type. I need to make sure that the possible values for the parameter are either ios or android.
How can I do that?
body: {
device_key : joi.string().required(),
…

Eranga Kapukotuwa
- 4,542
- 5
- 25
- 30
76
votes
6 answers
How to validate array of objects using Joi?
I am getting an array of objects to backend, where each object contains a service name.
The structure looks like below
[{"serviceName":"service1"},
{"serviceName":"service2"},..]
when I get the array at backend, I want to validate that every…

zakir
- 2,390
- 2
- 13
- 16
58
votes
2 answers
How to allow any other key in Joi
I have a simple requirement. I tried to search on the internet as well as documentation but failed.
So here is what I want to achieve:
I have a schema:
const schema = Joi.object().keys({
a: Joi.string().required(),
b:…

Anand Undavia
- 3,493
- 5
- 19
- 33
55
votes
6 answers
Allow string to be null or empty in joi and express-validation
I am using "joi": "10.0.6" and "express-validation": "1.0.1" in my project to validate a JSON object which I will save in the DB.
One of the field is a string but it could be null or empty.
How can I allow my string to pass the validation in the…

dventi3
- 901
- 4
- 11
- 19
42
votes
1 answer
Why is JOI more popular than AJV?
I am deciding on a validation library that I can use for both client and server side validation. I have been comparing JOI (+ joi-browser) and AJV.
As I understand it JOI and AJV can accomplish the same things.
AJV can even perform async validation…

tkalis
- 557
- 1
- 5
- 5
37
votes
2 answers
Joi validation multiple conditions
I have the following schema:
var testSchema = Joi.object().keys({
a: Joi.string(),
b: Joi.string(),
c: Joi.string().when('a', {'is': 'avalue', then: Joi.string().required()})
});
but I would like to add a condition on c field…

user2468170
- 1,234
- 2
- 15
- 19
35
votes
17 answers
joi_1.default.validate is not a function
I want to validate my Express routes before calling the controller logic. I use joi and created a validator which is able to validate the Request object against the schema object
import { Request, Response, NextFunction } from 'express';
import joi,…
user9945420
33
votes
3 answers
Yup vs Joi for frontend validation
I want to implement frontend validation with either Yup or Joi.
From all the docs and articles that I've found, I've got to a couple of conclusions:
Joi has better performance
Joi is more popular for backend validation, while Yup is more popular…

Nikola
- 332
- 1
- 3
- 6
32
votes
1 answer
Joi object validation: How to validate values with unknown key names?
I have an object with key names I cannot possibly know - they are created by user. However I do know what values they (keys) are going to store, and they (values) are going to be ISO strings. How do I validate those values? And, optionally, how do I…

wscourge
- 10,657
- 14
- 59
- 80
31
votes
3 answers
How to add custom validator function in Joi?
I have Joi schema and want to add a custom validator for validating data which isn't possible with default Joi validators.
Currently, I'm using the version 16.1.7 of Joi
const method = (value, helpers) => {
// for example if the username value is…

Shifut Hossain
- 1,599
- 3
- 14
- 24
31
votes
3 answers
Nodejs - Joi Check if string is in a given list
I'm using Joi package for server side Validation. I want to check if a given string is in a given list or if it is not in a given list.(define black list or white list for values)
sth like an "in" or "notIn" function. How can I do that?
var schema =…

Salar
- 5,305
- 7
- 50
- 78
30
votes
7 answers
Joi validator conditional schema
I need to create dynamic schema to validate my api request query in node js using Joi validator depending on a key in the request query. Say the following below mentioned pattern are my valid queries.
I'm using hapi/joi version 16.1.8
Combination…

Nitheesh
- 19,238
- 3
- 22
- 49