admin管理员组

文章数量:1291009

For example, there is the following 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

For example, there is the following 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

Share Improve this question asked Feb 1, 2019 at 16:56 alexpovalexpov 1,1882 gold badges17 silver badges29 bronze badges 2
  • What have you tried so far in Joi? – AndrewL64 Commented Feb 1, 2019 at 16:59
  • @AndrewL64 I didn't find a way to achieve it, the closes is to use Joi.object().keys({}) but the keys in my example are not predefined and they them selfs need to be validated – alexpov Commented Feb 2, 2019 at 7:01
Add a ment  | 

2 Answers 2

Reset to default 10

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:

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.

schema - the schema object matching keys must validate against.

So for your instance:

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

Firstly, the input, "test", is not in the correct format. The input map should be as follows:

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 ing from variety of sources as follows:

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); 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.

Also note that the map data can e from environment variable or another 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. 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. Then the rest of your schema validation will work fine.

Remember that the original process.env cannot be overwritten. Although no tool/piler/interpreter will give you a warning, overwriting process.env will not work.

本文标签: javascriptHow to use Joi to validate map object (map keys and map values)Stack Overflow