1

I have a Joi schema called directorySchema, and, among others keys, this schema has a key called parentDirectory, which type is also a directorySchema and could be null if the directory is a root/head one, and another key called directories, which type is an array of directorySchemas too. Basically this schema represents a doubly linked list.

Follow the example:

const Joi = require('@hapi/joi')

const directorySchema = Joi.object({
  name: Joi.string().required(),
  path: Joi.string().required(),
  size: Joi.number().min(0).required(),
  directories: Joi.array().items(...) // how to reference "directorySchema" here
  parentDirectory: ... // and here?
})

module.exports = directorySchema

This image explains the concept of doubly linked list, the box objects represents the directories.

In this image that explains the concept of doubly linked list, the box objects represents the directories.

I would like to know, is it possible to create a double linked list to validate my objects in Joi?

j3ff
  • 5,719
  • 8
  • 38
  • 51
Gabriel Mochi
  • 509
  • 1
  • 4
  • 10
  • I personally don't have much experience with Joi directly, but it sounds like you want to create a Schema in where you have attributes referring to other objects of the same schema. Take a look at this SO [answer](https://stackoverflow.com/questions/33770804/hapi-joi-validation-with-nested-object) for suggestions on how you could do this. – xinkecf35 Apr 12 '20 at 05:21

1 Answers1

1

You can use joi links:

const Joi = require('@hapi/joi')

const directorySchema = Joi.object({
  name: Joi.string().required(),
  path: Joi.string().required(),
  size: Joi.number().min(0).required(),
  directories: Joi.array().items(Joi.link("#directory"))
}).id("directory")

module.exports = directorySchema

Link to documentation: https://hapi.dev/module/joi/api/?v=17.1.1#linkref

John Schmitz
  • 108
  • 1
  • 10