Object schema description language and validator for JavaScript objects.
Questions tagged [joi]
1003 questions
16
votes
2 answers
Is it possible to require at least one field from a set of defined fields?
Given a definition like this:
const querySchema = joi.object({
one: joi.string().min(1).max(255),
two: joi.string().min(1).max(255),
three: joi.string().min(1).max(255),
});
Is there a way to require at least one of those fields? I don't care…

FLC
- 786
- 1
- 6
- 18
16
votes
2 answers
hapi route joi validation of password confirmation
How do I check that password and password_confirmation are the same ?
var Joi = require('joi'),
S = Joi.string().required().min(3).max(15);
exports.create = {
payload: {
username: S,
email: Joi.string().email(),
…

Whisher
- 31,320
- 32
- 120
- 201
15
votes
1 answer
Best way to maintain only one schema between Mongoose and Joi
I'm using Hapi to develop a web service, with Mongoose as ODM and Joi as validator.
I really like Joi's validation and the way it connects with HAPI (I need Joi's description function to display some description in swagger) but I don't want to…

Manuel Bitto
- 5,073
- 6
- 39
- 47
15
votes
1 answer
Joi validation schema - two fields must not have the same value
Assume I have the following schema:
var schema = {
fieldOne: Joi.string().required(),
fieldTwo: Joi.string().required()
};
Is it possible to set a validation rule that checks if both fields have different values?

Bravo
- 1,944
- 4
- 29
- 53
15
votes
4 answers
Stripping unknown keys when validating with Joi
I'm using Joi to validate a JavaScript object in the server. The schema is like the following:
var schema = Joi.object().keys({
displayName: Joi.string().required(),
email: Joi.string().email(),
enabled: Joi.boolean().default(false,…

Rafael Maiolla
- 373
- 1
- 3
- 13
14
votes
1 answer
Using Joi, validating that a boolean is true
Is it possible to validate that a boolean is true using Joi? I've tried using allow, valid and invalid without any luck.

anthonator
- 4,915
- 7
- 36
- 50
13
votes
9 answers
How can I validate "number of digits" from joi using nodejs?
i want that if the number of digits in the input field in less/more than 14(for example) then joi should return an error.
how can i do this with the type of number not for string.

Ravi Singh
- 1,049
- 1
- 10
- 25
13
votes
8 answers
How to validate for ObjectID
Using Joi schema validation, is it possible to validate against MongoDB ObjectID's?
Something like this could be great:
_id: Joi.ObjectId().required().error(errorParser),

Raz Buchnik
- 7,753
- 14
- 53
- 96
13
votes
1 answer
Is there a way to validate dynamic key names in a Joi schema?
Is there a way I can validate a value like this with Joi so that I can verify it is an object with zero or more keys (of any name) and that each have values of either a string, number or boolean?
{
dynamicallyNamedKey1: 'some value',
…

1977
- 2,580
- 6
- 26
- 37
13
votes
1 answer
How to validate string with Joi?
I am using Node Joi for validation. I am new in node I want to validate env to accept only 2 words "Yes" or "No" What changes I have to make in the following code
schema = Joi.object().keys({
app_id: Joi.string().required(),
env:…

Farhan Yaseen
- 2,507
- 2
- 22
- 37
12
votes
2 answers
Joi Validation Regex or pattern
I want to joi use regex pattern which define in variable
I have a variable pattern which contains regex
i.e
pattern = "/^[0-9+]{7}-[0-9+]{1}$/"
and this pattern send to Joi module and want to confirm
module.exports = {
save: {
body: {
…

Imran Rafiq
- 308
- 2
- 5
- 15
12
votes
2 answers
Allow optional parameters in Joi without specifying them
I'm fairly new to using Joi to validate request payloads in hapi. My question is the following. I have this defined route:
{
method: 'POST',
path: '/foo/bar',
config: {
description: 'foo.bar',
handler: handlers.foo,
…

João Minhós Rodrigues
- 173
- 1
- 2
- 10
11
votes
1 answer
Generate Joi Schema from Typescript types/interfaces
I want to generate some joi schema object from Typescript types or interfaces. In my initial searching I found some things that do the opposite (generate Typescript types/interfaces from joi schemas), and ts-interface-builder + ts-interface-checker…

n8jadams
- 991
- 1
- 9
- 21
11
votes
2 answers
Joi - make everything required by default?
I'm building out a Node/Express API and using Joi for validation. It's a great package and has been incredibly useful. However, we're getting tired of doing things like:
const mySchema = joi.object({
thing1: joi.string().required(),
thing2:…

john maccarthy
- 5,463
- 3
- 11
- 14
11
votes
4 answers
Set default value of validation
I am using Joi to validate a payload of a service in my node.js server using hapijs framework. It used to look like this (in my typescript code as well as after compiling to javascript):
payload: {
para1: Joi.number().required(),
para2:…

zhangjinzhou
- 2,461
- 5
- 21
- 46