hapijs - Joi-taulukon validointi

original title: "hapijs - Joi validation of array"


Translate

trying to validate that an array has zero or more strings in one case and that it has zero or more objects in another , struggling with 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()
        })
    }


yrittää vahvistaa, että taulukossa on nolla tai enemmän merkkijonoja yhdessä tapauksessa ja että siinä on nolla tai enemmän objekteja toisessa, kamppailee Joi docs :( validoi: {otsikot ...

Tämä on yhteenveto käännöksen jälkeen. Jos haluat tarkastella koko käännöstä, napsauta käännä-kuvaketta


Kaikki vastaukset
  • Translate

    Joi.array().items() accepts another Joi schema to use against the array elements. So an array of strings is this easy:

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

    Same for an array of objects; just pass an object schema to items():

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

  • Translate

    you can try this:

    function(data) {
     const Schema = {
       categories: Joi.array().items(Joi.string()),
       variables: Joi.array().items(Joi.object().keys().min(1))
     }
     return Joi.validate(data, Schema)
    }
    

  • Translate

    You can also try this way, I have been using it for long time and working fine for me.

    If your array is array of objects than use below:

    const Joi = require('joi');
    
    let schema = Joi.object().keys({
                items: Joi.array().items(
                    Joi.object({
                        item_id: Joi.number().required(),
                        item_qty: Joi.number().required(),
                        item_type_id: Joi.number().required(),
                        item_qty: Joi.number().required(),
                        base_price: Joi.number().required(),
                        actual_price: Joi.number().required(),
                    })
                ).required(),
            });
    
            let errors = Joi.validate(req.body, schema);
    

    if you array is simple array than:

    let schema = Joi.object().keys({
                    items: Joi.array().items(
                    Joi.number().required()
                ).min(10).required(),
                });
    

  • Translate

    Joi.array().items(Joi.string().required(), Joi.number().required()); found it here


  • Translate
    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()
            })
        }