Is `type` a required field for Schema Objects?
See original GitHub issueI am wondering how to interpret the following line of OpenAPI 3 (as of v3.0.1):
type - Value MUST be a string. Multiple types via an array are not supported.
Does this mean that the type
field is mandatory and must be a string, or that it is optional and if it exists it must be a string?
Issue Analytics
- State:
- Created 5 years ago
- Comments:28 (17 by maintainers)
Top Results From Across the Web
object — Understanding JSON Schema 2020-12 documentation
Objects are the mapping type in JSON. They map “keys” to “values”. In JSON, the “keys” must always be strings. Each of these...
Read more >Required Schema.org Fields
We often get asked which fields are required for a data type. The answer is that it depends on what you're trying to...
Read more >Schema validation reference for object types
The schema defines an array but does not define a corresponding items property that specifies a type for the array items.
Read more >Common Mistake in Specifying Required Fields in JSON ...
Design Center supports both JSON Schema Draft 3 and JSON Schema Draft 4. The method of specifying fields as required differs in Draft...
Read more >Mongoose Schema with nested optional object with required ...
field2.type is required if field2 exists (notice that the name of the field is "type" as mongoose reserved word for type definitions),; field2...
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
I don’t think I would support a proposal to make
type
required, even though it would make my life easier as a tool provider.Aside from being a breaking change, it would limit the use of OpenAPI to describe some APIs in the wild that actually do allow more than one data type for certain parameters or properties. I don’t think this is good API design, but OpenAPI should be able to describe bad APIs as well as good ones.
That said, optional typing is a legitimate challenge for code generators and implementation frameworks. So we can consider a range of possible ways to deal with this:
type
, or use type inference and default behaviors, along with appropriate configuration options and log information. This is what we’re doing today.type
required, among other things.type
should be specified, wherever practical, to confer a minimum level of precision required by a broad set of downstream consumers, including code generators, frameworks, etc.type
as a warning, while still remaining in full conformance to the OpenAPI specification.@silkentrance,
I should point out that OAS itself is not defined using a JSON schema. An OpenAPI document (i.e. a description of an API, conforming to the OAS) takes the form of an object graph that can be partially described and validated using JSON Schema.
But the written specification is the authoritative source. Any “official” or unofficial JSON Schema describing the OAS itself is supplementary, not considered definitive.
Getting lost in the meta-levels is an occupational hazard. 😉
That would be a further break from JSON Schema. The OpenAPI TSC wants to explore convergence with a future draft of JSON Schema, such that Schema Object fully supports the syntax and semantics of JSON Schema. The overall consensus seems to be that this would be valuable, though I don’t think they’ve committed to this yet.
@bbqsrc,
We currently treat it as a warning in our editors, though again this is a bit of a divergence from the OAS standard, so we plan to make that warning configurable.
A particularly common form of this is a schema that omits
type
, but specifiesproperties
. Strictly speaking, this does not mean that the value must be an object. It means that if the value is an object, and it includes any of those properties, the property values must conform to the corresponding property subschemas.In reality, this construct almost always means that the user intends
type: object
, and I think it would be reasonable for a code generator to assume this, maybe with avalidation: strict|lax
config option to control that behavior.There are lots of other cases where annotations like
format
, or assertions likemaxLength
ormultipleOf
can give a clue as to the intendedtype
. And assumingtype: string
as the default might even be reasonable in some cases. Just be aware that any behaviors like this are out of strict conformance with the spec, and are prone to misinterpretation.