Schema does not enforce requirement of array items object
See original GitHub issueIn 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:
- Created 9 years ago
- Comments:15 (15 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 withcontains
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
anddefault
.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 withanyOf
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 ifitems
is not an assertion of array-ness.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.