简体   繁体   English

如何使用 Joi 验证 map object(映射键和 map 值)

[英]How to use Joi to validate map object (map keys and map values)

For example, there is the following map:比如有下面的map:

keys = type string, 5 characters long
values = type number

Example:例子:

test = {
   "abcde": 1
   "12345": 2
   "ddddd": 3
}

How to write Joi Scheme that validates key are of type string with 5 characters and values are of type number如何编写验证键的Joi Scheme是具有5个字符的字符串类型,值是数字类型

It looks like you're trying to validate an object with unknown keys, but you know what general pattern the object must match. 似乎您正在尝试使用未知密钥来验证对象,但是您知道该对象必须匹配哪种常规模式。 You can achieve this by using Joi's .pattern() method: 您可以使用Joi的.pattern()方法实现此.pattern()

object.pattern(pattern, schema)

Specify validation rules for unknown keys matching a pattern where: 为匹配模式的未知密钥指定验证规则,其中:

pattern - a pattern that can be either a regular expression or a joi schema that will be tested against the unknown key names. pattern一种模式,可以是正则表达式,也可以是joi模式,将针对未知的键名进行测试。

schema - the schema object matching keys must validate against. schema模式对象匹配键必须进行验证。

So for your instance: 因此,对于您的实例:

Joi.object().pattern(Joi.string().length(5), Joi.number());

Firstly, the input, "test", is not in the correct format.首先,输入“test”的格式不正确。 The input map should be as follows:输入 map 应如下所示:

test: [
  ["abcde", 1]
  ["12345", 2]
  ["ddddd", 3]
]

or或者

test: [
  ["abcde", {id:1, val: 10}]
  ["12345", {id:2, val:11}]
  ["ddddd", {id:3, val:12}]
]

Secondly, the joi schema can now validate this input coming from variety of sources as follows:其次,joi 模式现在可以验证来自各种来源的输入,如下所示:

const varsSchema = joi.object({  
  TEST: joi.array().items(joi.string().required().valid(...test.keys((k) => k)))
    .default(Array.from(test.keys()))
}).unknown()
  .required();

const { error, value: vars } = varsSchema.validate(test); const { 错误,值:vars } = varsSchema.validate(test); if (error) { throw new Error( validation error: ${error.message} ); if (error) { throw new Error( validation error: ${error.message} ); } }

The above schema will validate only for "keys" of map as this is the most used case.上述模式将仅验证 map 的“密钥”,因为这是最常用的情况。

Also note that the map data can come from environment variable or another object.另请注意,map 数据可以来自环境变量或其他 object。

Also, note that if you are using pm2 and ecosystem file to pass environment variables, then the "map" type data is not parsed correctly and it appears as string type data in your joi file.另外,请注意,如果您使用 pm2 和生态系统文件来传递环境变量,那么“map”类型的数据不会被正确解析,它会在您的 joi 文件中显示为字符串类型的数据。 This can be fixed as follows:这可以修复如下:

let envVars = { ...process.env };
if ('TEST' in envVars) { envVars['TEST'] = JSON.parse(envVars['TEST']); }

Consider the above code as casting the type from string to Map type.将上述代码视为将类型从字符串转换为 Map 类型。 Then the rest of your schema validation will work fine.然后您的架构验证的 rest 将正常工作。

Remember that the original process.env cannot be overwritten.请记住,原始 process.env 不能被覆盖。 Although no tool/compiler/interpreter will give you a warning, overwriting process.env will not work.尽管没有工具/编译器/解释器会给您警告,但覆盖 process.env 将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM