Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you create a joi schema to verify a json file format?

Tags:

json

schema

joi

If i had the below format for a json file which is supposed to model a chemistry experiment, how do i create a joy schema to validate the json file format? there are basic examples i've seen that cover the joi schema syntax for non-nested structures, but the one below is nested and i'm not sure how to format the joi schema. Any suggestions?

{
  "injection": {
    "volume": {
      "value": 20,
      "unit": "MicroLiter"
    },
    "number": 1,
    "location": {
      "vial": "1",
      "plate_row": "A",
      "plate_column": "1"
    }
like image 401
eni Avatar asked Sep 14 '25 22:09

eni


1 Answers

This is a very simple use case of joi schema validation. For the above JSON

the nested schema is:

Joi.object().keys({
  injection: Joi.object().keys({
    number: Joi.number().required(),
    volume: Joi.object().keys({
      value: Joi.number().required(),
      unit: Joi.string().required()
    }).required(),
    location: Joi.object().keys({
      vial: Joi.string().required(),
      plate_row: Joi.string().required(),
      plate_column: Joi.string().required()
    }).required()
  })
});

Please let me know if this works.

like image 60
Apurva jain Avatar answered Sep 16 '25 12:09

Apurva jain