【问题标题】:A Joi schema to validate multiple dynamic key objects用于验证多个动态键对象的 Joi 模式
【发布时间】:2019-07-31 07:01:56
【问题描述】:

Joi v14.3.1

有一个对象:

const obj = {
  actions: {
    dynamic_key_0: {
      email: {
        to: 'batman@mail.com'
      }
    },
    dynamic_key_1: {
      webhook: {
        host: 'batman.com',
        method: 'get'
      }
    }
  }
};

其中dynamic_key_0dynamic_key_1 键名可以是任何名称。此外,可以有很多这样的键。

我试过.pattern()

const Joi = require('joi');

const obj = {
  actions: {
    dynamic_key_0: {
      email: {
        to: 'batman@mail.com'
      }
    },
    dynamic_key_1: {
      webhook: {
        host: 'batman.com',
        method: 'get'
      }
    }
  }
};

const emailSchema = Joi.object().pattern(Joi.string(), Joi.object({
  email: Joi.object({
    to: Joi.string().email()
  })
}));

const webhookSchema = Joi.object().pattern(Joi.string(), Joi.object({
  webhook: Joi.object({
    host: Joi.string(),
    method: Joi.string()
  })
}));

const objSchema = Joi.object({
  actions: Joi.object()
    .concat(emailSchema)
    .concat(webhookSchema)
});

console.log(Joi.validate(obj, objSchema));

并得到以下验证错误。不允许使用密钥 email

{ error:
   { ValidationError: child "actions" fails because [child "dynamic_key_0" fails because ["email" is not allowed]]
       at Object.exports.process (/path/node_modules/joi/lib/errors.js:203:19)
       at internals.Object._validateWithOptions (/path/node_modules/joi/lib/types/any/index.js:764:31)
       at module.exports.internals.Any.root.validate (/path/node_modules/joi/lib/index.js:147:23)
       at Object.<anonymous> (/path/server/schema/test.js:38:17)
       at Module._compile (internal/modules/cjs/loader.js:689:30)
       at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
       at Module.load (internal/modules/cjs/loader.js:599:32)
       at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
       at Function.Module._load (internal/modules/cjs/loader.js:530:3)
       at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
     isJoi: true,
     name: 'ValidationError',
     details: [ [Object] ],
     _object: { actions: [Object] },
     annotate: [Function] },
  value:
   { actions: { dynamic_key_0: [Object], dynamic_key_1: [Object] } },
  then: [Function: then],
  catch: [Function: catch] }

如何定义正确的架构来验证obj

【问题讨论】:

    标签: javascript hapijs joi


    【解决方案1】:

    我想出了以下似乎可以正常工作的代码

    const Joi = require('joi');
    
    const obj = {
      actions: {
        dynamic_key_0: {
          email: {
            to: 'batman@mail.com'
          }
        },
        dynamic_key_1: {
          webhook: {
            host: 'batman.com',
            method: 'get'
          }
        }
      }
    };
    
    const emailSchema = Joi.object({
      email: Joi.object({
        to: Joi.string().email().required()
      })
    });
    
    const webhookSchema = Joi.object({
      webhook: Joi.object({
        host: Joi.string().required(),
        method: Joi.string()
      })
    });
    
    const objSchema = Joi.object({
      actions: Joi.object().pattern(
        Joi.string(),
        Joi.object()
          .concat(emailSchema)
          .concat(webhookSchema)
      )
    });
    
    console.log(Joi.validate(obj, objSchema));
    

    结果:

    { error: null,
      value:
       { actions: { dynamic_key_0: [Object], dynamic_key_1: [Object] } },
      then: [Function: then],
      catch: [Function: catch] }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-05
      • 2021-10-10
      • 2017-08-20
      • 2019-01-20
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      相关资源
      最近更新 更多