question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Arrays in schemas show up as a separate model

See original GitHub issue

v7.1.0

If I have:

"topten": Joi.array().items(
    Joi.object({
        name: Joi.string().required().label('name').description('user name'),
        score: Joi.number().required().min(0).label('score')
    }).label('TopTen')

I get something like:

{
  topten (topten, optional)
}

topten [
    TopTen
]

TopTen {
    name (string): user name ,
    score (number),
}

I would like to get something like:

{
  topten (Array of TopTen, optional)
}

TopTen {
    name (string): user name ,
    score (number),
}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
moandercommented, Mar 1, 2019

Hi guys,

Is it possible to set an option to avoid each array becoming a separate model? In my case it will reduce the number of models significantly.

Joi.object({
    rows: Joi.array().items(Joi.object({
        name: Joi.string()
    }).label('Row'))
}).label('RowList')

The object above shows up like this in the swagger.json:

"Row": {
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        }
    }
},
"rows": {
    "type": "array",
    "items": {
        "$ref": "#/definitions/Row"
    }
},
"RowList": {
    "type": "object",
    "properties": {
        "rows": {
            "$ref": "#/definitions/rows"
        }
    }
},

What I want is to reduce it to the following

"Row": {
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        }
    }
},
"RowList": {
    "type": "object",
    "properties": {
        "rows": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/Row"
            }
        }
    }
},
1reaction
irothschildcommented, Sep 9, 2016

Hi Glenn,

First let me thank you for all the hard work you’ve put into this over what now amounts to years. I understand the challenge of keeping things in sync with the fast moving Hapi and Joi targets, let along the swagger and swagger-ui changes.

The real goal for me in using this module is to generate clear, understandable API documentation for end-user developers. Unfortunately, we’re not there yet and in some ways it seems to be moving away from my goal. I don’t claim to understand all the ways people are using this module and for what purposes (code generation seems to be one) but they seem to be different from mine.

For documentation purposes, I don’t think it makes sense to break arrays out as separate objects. It’s just confusing. As well, I don’t understand the use of object definition equality/hashing in the module. For me, if Joi objects have different labels, they are different classes.

Right now if I do:

    a: Joi.object({name: Joi.string().required().label('name')}).required().label('A'),
    b: Joi.object({name: Joi.string().required().label('name')}).required().label('B'),

In the Swagger UI I see:

a (A, optional),
b (A, optional),

A {
  name (string)
}

Apart from them being mislabeled as optional, both are marked as class A when b should be B.

Again, I don’t understand how people are using this module but I think a lot can be simplified in new versions by forcing people to be explicit in their labeling and using those labels without doing any kind of equality tests.

I don’t know where that leaves me. I see three options:

  1. Make some changes and do a PR. This might entail more configuration options to avoid breaking things for people who want/need currently functionality. I know you want to avoid adding more options so maybe you’ll reject this idea.
  2. Do a “hard-fork”.
  3. Generate a swagger.json, either manually editing the hapi-swagger output or using another swagger generation tool.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Arrays in schemas show up as a separate model #331 - GitHub
Is it possible to set an option to avoid each array becoming a separate model? In my case it will reduce the number...
Read more >
How can a json schema represent an array with different ...
This schema explicitly states that there can be a heartrate object. You say you're familiar with arrays of a single object type, but...
Read more >
array — Understanding JSON Schema 2020-12 documentation
Arrays are used for ordered elements. In JSON, each element in an array may be of a different type. Language-specific info: Python; Ruby....
Read more >
Mongoose v6.8.2: SchemaTypes
You can think of a Mongoose schema as the configuration object for a Mongoose model. A SchemaType is then a configuration object for...
Read more >
Express Tutorial Part 3: Using a Database (with Mongoose)
It explains how object schema and models are declared, the main field ... The examples above show an array of objects without a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found