When using JSON Schema and Open API specification (OAS) to document a REST API, how do I define the UUID property?
相关问题
- How to refer to an external JSON file containing r
- How to convert json schema to avro schema
- Cannot reference a component schema defined in a s
- How to extend FastAPI docs with another swagger do
- How can I define the sequence of properties in JSO
相关文章
- How do I do a nested list (array) of schema refere
- JSON Schema oneOf properies filled
- How to define UUID property in JSON Schema and Ope
- How to generate JSON schema from a JAXB annotated
- Android, Gradle and duplicate files error during p
- Is it possible in json schema to define a constrai
- BeanConfig (or similar?) in Swagger 2.0 (OpenApi 3
- How to generate JSON examples from OpenAPI 3.0 yam
Since the question was originally asked the JSON Schema spec has been extended to provide built-in support for specifying and validating that a JSON field of type string is a UUID - specifically that it adheres to the format of a UUID as defined by RFC4122, e.g. “f81d4fae-7dec-11d0-a765-00a0c91e6bf6”.
The support was added in JSON Schema spec version 2019-09 (previously known as draft-08). The JSON Schema Validation component spec was extended such that the existing ‘format' keyword that can be specified for schema fields of type string now supports a new built-in format named "uuid".
The example JSON schema below declares a (mandatory) field named "id" of type string that must be formatted as UUID -
Note that at the time of writing, the section of the JSON Schema user guide ("Understanding JSON Schema") covering examples of built-in string validation - JSON Schema Reference > Type-specific keywords > string > Format - doesn’t mention UUID supports, as it’s out of date - it currently only describes JSON Schema draft-7.
For the Java developers among you, the RFC4122 format used by JSON schema is compatible with the string representation of Java’s UUID class - it’s Javadoc also mentions RFC 4122.
For more details see -
The only way I found so far is to manually specify the RegEx pattern as reusable schema component:
But, I would definitely want to use a more standardized approach.
There's no built-in
type
for UUID, but the OpenAPI Specification suggests usingFrom the Data Types section (emphasis mine):
For example, Swagger Codegen maps
format: uuid
toSystem.Guid
in C# orjava.util.UUID
in Java. Tools that don't supportformat: uuid
will handle it as justtype: string
.