2

I am trying to define a schema to validate this type of data array:

[ 
'item1 string',         // item1, being a given string
2,                      // item2, an integer
'item3 string',         // item3, a variable string, repeated N times
'another item3 string', // ...
...
'last item3 string',    //
1.1,                    // item4, a number
]

item3 is a collection of strings, which varies. Without the repetition, a simplified version of my schema is:

{
  type: 'array',
  minItems: 4,
  maxItems: 4,
  items: [
    { $ref: '#/definitions/item1' },
    { $ref: '#/definitions/item2' },
    { $ref: '#/definitions/item3' },
    { $ref: '#/definitions/item4' },
  ],
  definitions: {
    item1: { type: 'string' },
    item2: { type: 'integer' },
    item3: { type: 'string' },
    item4: { type: 'number' },
  },
}

I want my "items" section to be an array, as I want to make sure the items are ordered in the right way. But I don't see how to describe the repetition of "item3" elements.

I actually know in advance the number of "item3" occurrences I am expecting, so right now I rebuild the schema for each check (adjusting the mix/maxItems, and repeating the item3 section).

But it there a more efficient way of going about this? This schema is used in nodejs / ajv, is there an ajv feature that would help if this is not doable inside a schema? Thank you.

7
  • Hi Will, I'm interested to hear what your use case is here, by which I mean what is the actual data / reason for wanting this? When evaluating what things to add to JSON Schema, we often require real life examples, so we ground changes based on real requirements and not academic ideals. Thanks. Commented Aug 3, 2020 at 8:49
  • Hi. Maybe I am not using the ideal tool for this, but here I use ajv to validate some large sql query result. The sql query varies in length, instructions are built and added based on the case at hand. The middle instructions are all the same, they are the ones corresponding to my example's "item3", whose results are all checked using the same definition. I cannot reorder the instructions, instructions before and after these repeated instructions have to be in that order (there are more than in this example, and the schema is of course more complex). Let me know if you have questions, thanks. Commented Aug 3, 2020 at 9:19
  • I've just had a thought. This might be possible if you're willing and able to change implementations to (JASON Desrosiers own) Hyper Jump for JSON Schema validation, and update to use JSON Schema draft 2019-09... I'll be able to give you a full explanation and example later today! =] Commented Aug 3, 2020 at 9:23
  • Wait, no, I'm going crazy. Nevermind... =[ Commented Aug 3, 2020 at 9:26
  • @Relequestual well thanks for looking into this :-) I guess what I'm looking for is a sort of "regex", but to put together json schemas, not string checks... Maybe it will be part of a future schema spec! Commented Aug 3, 2020 at 9:29

1 Answer 1

2

This is not something that can be expressed with JSON Schema. If the variable length array items came last, it could be done. Assuming it would be ok to move item4 before item3, you could do this...

{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/item1" },
    { "$ref": "#/definitions/item2" },
    { "$ref": "#/definitions/item4" }
  ],
  "additionalItems": { "$ref": "#/definitions/item3" }
}

If you can't move the variable items to the end, you're out of luck.

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was afraid of. Thanks for taking the time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.