Object schema description language and validator for JavaScript objects.
Questions tagged [joi]
1003 questions
11
votes
5 answers
Ignoring "required" in Joi validation?
I'm attempting to use Joi to validate a data model accepted by a RESTful web service.
For a create operation, I want to enforce the "required" validation on fields. However, for an update operation, a partial data object may be submitted so I…

HolySamosa
- 9,011
- 14
- 69
- 102
10
votes
2 answers
How to format JOI date/schema correctly?
I've got a function which enforces validation restrictions via obj.pattern.
The key which I would like to validate is a formatted date which is provided to the function in the following format DD/MM/YYYY.
I am using Joi.date to validate against…

Valiant_Fox
- 113
- 1
- 1
- 4
10
votes
2 answers
Validate array of strings with joi, must be strings
I need to validate an array to check if it's elements are strings using joi. It always sends the error of "Inavlid tag".
// returned array from req.body
let tags = ["Vue", "React", "Angular"]
// joi shema
const schema = {
tags:…

ImInYourCode
- 567
- 1
- 7
- 19
10
votes
1 answer
How to get all validation error in once with Joi?
I am facing issue related to Joi validation, when ever i send the request to joi it throws only single error.
var CreateValidationSchema = Joi.object().keys({
name: Joi.string().required().max(255).label("Name"),
branch_name:…

Abhay S
- 103
- 1
- 4
10
votes
2 answers
Validating unique key pairs in a nested object with Joi and nodeJS
I have the following JSON structure:
{
key1: "value1",
key2: "value2",
transactions: [
{
receiverId: '12341',
senderId: '51634',
someOtherKey: 'value'
},
{
receiverId: '97561',
senderId:…

Akshay Bhimrajka
- 103
- 2
- 6
10
votes
1 answer
How to validate whether a username is unique using joi?
I read an article somewhere in which the author used Joi to validate asynchronously, whether the username is unique or not by checking with the database. I can't find it now and I want to know how can we do that with Joi.

sidoshi
- 2,040
- 2
- 15
- 30
10
votes
2 answers
Joi - validate object keys and values?
how could I use Joi to validate a substitutions field has zero or more key /value pairs ? and that each key is a string and that each value is a string, number or bool ?
"substitutions": {
"somekey": "someval",
"somekey": "someval"
}

1977
- 2,580
- 6
- 26
- 37
10
votes
1 answer
Hapi/Joi validation with nested object
I have the following validation on one of my routes:
payload: {
keywordGroups: Joi.array().items(Joi.object().keys({
language: Joi.string().required(),
containsAny: Joi.array().items(Joi.string()).default([]).when('containsAll',…

woutr_be
- 9,532
- 24
- 79
- 129
9
votes
2 answers
Joi - not.exist()?
I am trying to do a validation with Joi, but I came to a scenario that I cannot achieve. I have an object. In some cases, the object will have an id (edit) and on the others it will not (create).
I want to validate that in case "id" is not present,…

Daniel Stoyanoff
- 1,443
- 1
- 9
- 25
9
votes
1 answer
HapiJS/Joi Allow field to be String or Object with specific keys
I'm trying to validate a POST request where the title can be a String or an Object with language keys and values. Example:
{
title: 'Chicken',
...
}
//OR
{
title: {
en_US: 'Chicken',
de_DE: 'Hähnchen'
}
...
}
And…

Combustible Pizza
- 315
- 1
- 2
- 8
9
votes
1 answer
How to extend a module from npm using TypeScript?
I'm using joi and @types/joi with TypeScript. Joi has an extend method which allows extending joi by returning a new instance without modifying original joi library. I created an extended instance with it.
To create definition for this extended…

özüm
- 1,166
- 11
- 23
9
votes
2 answers
Joi Schema should contain one field or another
I'm looking into using Joi for api validation.
I can't seem to confirm whether my schema is correct in that I want either the email or mobile to be required (but they both can't be empty/non existent) - is the below correct?
var…

userMod2
- 8,312
- 13
- 63
- 115
8
votes
1 answer
Joi field validation base on a set of values of another field
I am trying to validate field in an object based on a set of values of another field. Assuming my object has to fields, field1 and field2. Field one can take values A, B, C, D, E, F, H, I. Now say if field1 is any of the values; A, B, C, H, then…

cdaiga
- 4,861
- 3
- 22
- 42
8
votes
2 answers
Joi - Required based on value on other key
I want to validate an input which only has two fields(namely text and image). Both text and image are string and one of them must always be present. When one of the fields is absent then the other one can not be an empty string.
this is the…

Salar
- 5,305
- 7
- 50
- 78
8
votes
2 answers
.when() in joi validation, to validate one key based on another key
Admin will create users, and while doing so he will enter some random string as a password. When admin edit the existing user, he don't need to enter any password unless he wants to change it. If the admin doesn't enter any password, then the old…

Rajeshwar
- 2,290
- 4
- 31
- 41