Object schema description language and validator for JavaScript objects.
Questions tagged [joi]
1003 questions
30
votes
6 answers
Using Joi, require one of two fields to be non empty
If I have two fields, I'd just like to validate when at least one field is a non empty string, but fail when both fields are empty strings.
Something like this does not validate
var schema = Joi.object().keys({
a: Joi.string(),
b:…

Brendan
- 809
- 1
- 8
- 13
29
votes
2 answers
Joi validation - how to require or optional field based on another key exists in array?
I have this Joi schema:
let schema = {};
let stations = {
contact: {
first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors),
last_name:…

Raz Buchnik
- 7,753
- 14
- 53
- 96
29
votes
5 answers
Joi Nested schema
I am trying to create nested schema in joi and it is throwing error
[Error: Object schema cannot be a joi schema]
var nestedSchema = joi.object({
b: joi.number()
});
var base = joi.object({
a: joi.string(),
…

Anshul
- 293
- 1
- 3
- 5
25
votes
1 answer
Joi: "tel" is not allowed to be empty
Joi is returning the following error even though tel is set to be optional. How do we fix this?
Thanks.
Error: Joi Failed: ValidationError: child "tel" fails because ["tel" is not allowed to be empty]
//Define Joi schema
const schema = {
…

Cazineer
- 2,235
- 4
- 26
- 44
23
votes
5 answers
Implement Joi in Typescript
Simple joi validation snippet in javascript.It will simply return an error object when validation fails.
validate.js
const Joi =require("joi");
function validateObject (input) {
const schema = {
key: Joi.string().required(),
};
return…

Sathya Narayanan GVK
- 849
- 2
- 16
- 32
23
votes
5 answers
JOI email validation
Im using Joi library as standalone validator for my CRA project but when firing email() validator im getting cryptic error
Uncaught Error: Built-in TLD list disabled

Konrad Albrecht
- 1,701
- 1
- 9
- 20
23
votes
3 answers
JOI :allow null values in array
I am trying to add a validation for array in a POST request
Joi.array().items(Joi.string()).single().optional()
I need to allow null values in the payload. Can you please tell me how this can be done ?

user1110790
- 787
- 2
- 8
- 27
23
votes
3 answers
hapijs joi validation , just validate one field and to allow any field
I want to validate one field and to allow another fields without validation; by example just to validate: "firstname" field. In my code when I comment 'payload', hapi allow me to record any field, when I uncomment 'payload' hapijs dont allow me…

stackdave
- 6,655
- 9
- 37
- 56
22
votes
4 answers
What is the difference between Joi.object() and Joi.object().keys()?
According to Joi documentation, you can use Joi.object() like so:
const object = Joi.object({
a: Joi.number().min(1).max(10).integer(),
b: Joi.any()
});
But you can also write an equivalent code using Joi.object().keys() like so:
const…

Berry
- 2,143
- 4
- 23
- 46
21
votes
3 answers
How to extend a Joi schema?
I have a joi schema called user
const user = {
firstName: Joi.string()
.min(2)
.max(50)
.required()
.label('First Name'),
lastName: Joi.string()
.min(3)
.max(50)
.required()
.label('Last Name'),
email:…

Malik Bagwala
- 2,695
- 7
- 23
- 38
20
votes
2 answers
Joi Validation - Compare to dates from POST
I'm currently using Joi in HapiJS / NodeJS to validate data. One POST in particular has two ISO dates (start date and end date) that are passed to the route and validated to make sure they are ISO dates.
{
method: 'POST',
path:…

tdotcspot
- 345
- 1
- 5
- 13
19
votes
2 answers
Is using Joi for validation on top of Mongoose good practice?
I'm developing a RESTful API with Node.js, Mongoose and Koa and I'm a bit stuck on what are the best practices when it comes to schemas and input validation.
Currently I have both a Mongoose and Joi schema for each resource. The Mongoose schema only…

rok
- 557
- 4
- 20
18
votes
3 answers
How to set custom error messages in @hapi/joi?
I've created following Schema for validation using Joi:
const createProfileSchema = Joi.object().keys({
username: Joi.string()
.required()
.message("username is required")
.empty()
.message("username is not allowed to be empty")
…

Shifut Hossain
- 1,599
- 3
- 14
- 24
17
votes
8 answers
Is There A Way To Merge Joi Schemas?
Is there any way to merge two joi schemas into a single schema?
Schema 1
{
alpha: Joi.number().required(),
beta: Joi.string().required(),
chalie: Joi.object({
xray: Joi.number().required(),
}).required()
}
Schema 1
{
delta:…

Undistraction
- 42,754
- 56
- 195
- 331
17
votes
2 answers
Multiple Joi validation types
I search a lot but nothing found to allow multiple type validation in Joi
Link: https://github.com/hapijs/joi
I would like to use something like this:
validate: {
type: joi.or([
joi.string(),
joi.array(),
])
};

Mr.Orange
- 426
- 1
- 4
- 8