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.

Schema does not enforce requirement of array items object

See original GitHub issue

In the 1.2 and 2.0 specifications, it says this regarding the items property: Required if type is "array". The problem is I can create a property like this without the JSON schema validator throwing an error:

"ids": {
  "type": "array"
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
handrewscommented, Oct 27, 2017

Not sure if this is still an active concern. if anyone is still curious as to why JSON Schema works this way, it has to do with the difference between assertions, annotations, and data model keywords (which is discussed more clearly in the soon-to-be-published draft-07).

items is a data model keyword. It applies a subschema to part of an instance based on the data model (in particular, if the instance is an array, it applies the subschema to ever element of an array and ANDs the results- contrast with contains which applies the subschema and ORs the results) Note the “if it’s an array, then apply the subschema” approach, with the implicit “else don’t do anything for this keyword” clause.

type is an assertion. It evaluates a constraint against the instance and produces a boolean result.

The third category, annotations, include keywords like title and default.

Data model keywords themselves never cause validation to fail. They apply subschemas which have assertions, and it is the assertions in the subschemas which cause the pass or fail.

This seems weird for use cases like OpenAPI. But consider using JSON Schema to scan arbitrary data for objectionable language with a regex. You don’t know or care what data types you’ll get. Objects, arrays, strings, numbers, booleans, nulls, nested structures of arbitrary complexity, etc. You just want to apply your regex using pattern to every string in the document.

If item behaved as an assertion, this would be very hard. You could do it with anyOf but it would be very complicated. Preserving the data model vs assertion split makes it easy to do this sort of “validation” with JSON Schema.

FWIW, JSON Schema does in fact treat a lack of items the same as "items": {}, which only works if items is not an assertion of array-ness.

0reactions
handrewscommented, Feb 14, 2020

We decided not to fix this in the other issue since the schema is not intended to catch all errors, and this is against old OAS versions that aren’t being worked on anymore anyway. I think this is safe to close.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't the schema see the required array? - Stack Overflow
It seems that you are missing a required array within the Envelope object. You have a required array at the root level object,...
Read more >
JSON schema to PL/I mapping - IBM
A JSON schema specifies that a variable is optional if it does not appear in the "required" keyword array that is associated with...
Read more >
Getting Started Step-By-Step - JSON Schema
The uniqueItems validation keyword notes all of the items in the array must be unique relative to one another. We did not add...
Read more >
17.7.0 API Reference - joi.dev
Note that joi schema objects are immutable which means every additional rule added ... Validation is not performed automatically for performance reasons.
Read more >
Schema Types — Atlas App Services - MongoDB
A mixed field may contain any schema type except for arrays, embedded objects, sets, and dictionaries. App Services does not enforce a consistent...
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