Подтвердить что ты не робот

Проверка достоверности массива Joi

пытается проверить, что массив имеет ноль или более строк в одном случае и что он имеет ноль или более объектов в другом, борясь с Joi docs: (

validate: {
        headers: Joi.object({
                'content-type': "application/vnd.api+json",
                accept: "application/vnd.api+json"
        }).options({ allowUnknown: true }),
        payload : Joi.object().keys({
            data : Joi.object().keys({
                type: Joi.any().allow('BY_TEMPLATE').required(),
                attributes: Joi.object({
                    to : Joi.string().email().required(),
                    templateId : Joi.string().required(),
                    categories : Joi.array().items( //trying to validate here that each element is a string),
                    variables : Joi.array({
                        //also trying to validate here that each element is an Object with one key and value
                    })
                })
            }).required()
        })
    }
4b9b3361

Ответ 1

Joi.array().items() принимает другую схему Joi для использования с элементами массива. Таким образом, массив строк легко:

Joi.array().items(Joi.string())

То же самое для массива объектов; просто передайте объектную схему на items():

Joi.array().items(Joi.object({
    // Object schema
}))

Ответ 2

Вы можете попробовать это:

function(data){
const Schema = {

categories: Joi.array().items(Joi.string()),
variables: Joi.array().items(Joi.object().keys().min(1))

}

return Joi.validate(data, Schema)

}

Ответ 3

Joi.array().items(Joi.string().required(), Joi.number().required()); нашел его здесь here

Ответ 4

validate: {
        headers: Joi.object({
                'content-type': "application/vnd.api+json",
                accept: "application/vnd.api+json"
        }).options({ allowUnknown: true }),
        payload : Joi.object().keys({
            data : Joi.object().keys({
                type: Joi.any().allow('BY_TEMPLATE').required(),
                attributes: Joi.object({
                    to : Joi.string().email().required(),
                    templateId : Joi.string().required(),
                    categories : Joi.array().items(Joi.string()),
                    variables : Joi.array().items(Joi.object().keys().max(1))
                })
            }).required()
        })
    }