How to validate array of objects using Joi?

前端 未结 5 1427
猫巷女王i
猫巷女王i 2020-12-29 01:28

I am getting an array of objects to backend, where each object contains a service name. The structure looks like below

[{\"serviceName\":\"service1\"},
{\"s         


        
相关标签:
5条回答
  • 2020-12-29 01:50
    const test = {
    body: Joi.array()
        .items({
            x: Joi.string().required(),
            y: Joi.string().required(),
            z: Joi.string().required(),
            date: Joi.string().required(),
        })
    

    };

    0 讨论(0)
  • 2020-12-29 01:51

    replacing ordered with items will work.

    let Joi = require('joi')
    let service = Joi.object().keys({
      serviceName: Joi.string().required(),
    })
    
    let services = Joi.array().items(service)
    
    let test = Joi.validate(
      [{ serviceName: 'service1' }, { serviceName: 'service2' }],
      services,
    )
    

    For reference click here

    0 讨论(0)
  • 2020-12-29 01:54

    A basic/ clearer example is as follows. To validate a JSON request like this:

       {
        "data": [
                {
            "keyword":"test",
            "country_code":"de",
            "language":"de",
            "depth":1
                }
            ]
        }
    

    Here is the Joi validation:

     seoPostBody: {
        body: {
          data: Joi.array()
            .items({
              keyword: Joi.string()
                .required(),
              country_code: Joi.string()
                .required(),
              language: Joi.string()
                .required(),
              depth: Joi.number()
                .required(),
            }),
        },
      };
    

    This is what I am doing in NodeJs, might need some slight changes for other platforms

    0 讨论(0)
  • 2020-12-29 01:57

    Just want to make it more clear. I'm currently using "@hapi/joi:16.1.7".

    Let's say you want your schema to validate this array of objects.

    const example = [
       {
          "foo": "bar",
          "num": 1,
          "is_active": true,
       }
    ];
    

    Then schema's rules should be:

    var validator = require('@hapi/joi');
    
    const rules = validator.array().items(
        validator.object(
            foo: validator.string().required(),
            num: validator.number().required(),
            is_active: validator.boolean().required(),
        ),
    );
    
    const { error } = rules.validate(example);
    
    0 讨论(0)
  • 2020-12-29 02:01

    For Joi you can use below which is working fine for me, this will validate that array must have at-least on object with key serviceName-

    const Joi = require('joi');
    const itemsArray = Joi.array().items(
                Joi.object({
                    serviceName: Joi.string().required(),
                })
            ).min(1).required();
    
            const itemSchema = Joi.array().items(itemsArray).when('checkout_type', {
                is: 'guest',
                then: Joi.array().required(),
            }).required();
    
    let schema = Joi.object().keys({
            items: Joi.alternatives().try(itemsArray, itemSchema).required(),
        });
    
    0 讨论(0)
提交回复
热议问题