Udit Kumawat
Udit Kumawat

Reputation: 684

Joi.array().includes is not a function error

I am validating payload data of my API with Joi validation. I have made one schema like this:

'use strict';

let mongoose = require('mongoose');

let Schema = mongoose.Schema;

let tokenSchema = new Schema({

    level : {type : Number},
    tokenValues : [{
        level : {type : Number},
        amount : {type : Number}
    }]
});

module.exports = mongoose.model('Tokens',tokenSchema,'tokens');

I have written Joi validation:

validate: {
            payload: {
                level: Joi.number().required(),
                tokensValues: Joi.array().includes({
                    level : Joi.number().required(),
                    amount : Joi.number().required()
                })
            }
}

This above code is giving error like this :

TypeError: Joi.array(...).includes is not a function

Please suggest some other method if this is the wrong practice.

Upvotes: 1

Views: 5106

Answers (2)

neeraj-dixit27
neeraj-dixit27

Reputation: 2892

Joi.types.Array().includes(Joi.types.Array().valid(["pass array strings here"]))

if you are using express-joi

Upvotes: 0

Udit Kumawat
Udit Kumawat

Reputation: 684

I got the solution :

validate: {
            payload: {
                level: Joi.number().required(),
                tokensValues: Joi.array().items(Joi.object().keys({
                    level : Joi.number().required(),
                    amount : Joi.number().required()
                }))
            }
}

Upvotes: 1

Related Questions