How to validate array of objects using Joi?

前端 未结 5 1434
猫巷女王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: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

提交回复
热议问题