Compare commits

...

4 Commits

Author SHA1 Message Date
Kévin Commaille 72ce0ed133
Simplify formats map
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
3 weeks ago
Kévin Commaille c0083de076
Improve OpenAPI Extensions docs
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
3 weeks ago
Kévin Commaille d099f534d7
Put regex in comment
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
3 weeks ago
Kévin Commaille aa79737243
Fix docs
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
3 weeks ago

@ -11,23 +11,23 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# The list of custom formats supported for the `format` key and the
# `x-pattern-format` extension (see `openapi_extensions.md` for more details).
# This file contains the list of custom formats supported for the `format` key
# and the `x-pattern-format` extension (see `openapi_extensions.md` for more
# details).
#
# Each entry must use the `mx-` prefix and have the form:
#
# mx-custom-key:
# title: The title rendered in the specification
# url: /url/to#definition
# pattern: "REGEX" (not used for rendering, but allows to define the common
# pattern to use for the format)
mx-user-id:
title: User ID
url: /appendices#user-identifiers
pattern: "^@"
# regex: "^@"
mx-event-id:
title: Event ID
url: /appendices#event-ids
pattern: "^\\$"
# regex: "^\\$"

@ -185,26 +185,24 @@
them later to differentiate schemas according to patterns.
*/}}
{{/* Use a Scratch to manipulate the map easily. */}}
{{ $scratch := newScratch }}
{{/*
Construct a map from format ID to the type string of the format.
*/}}
{{ $formatMap := newScratch }}
{{ range $pattern, $schema := .patternProperties }}
{{ $formatId := "string" }}
{{ with index $schema "x-pattern-format" }}
{{ $formatId = . }}
{{ end }}
{{ $formatId := or (index $schema "x-pattern-format") "string" }}
{{/* Initialize the map for the format if it doesn't exist. */}}
{{ if not (reflect.IsSlice ($scratch.Get $formatId)) }}
{{ $scratch.Set $formatId slice }}
{{ if $formatMap.Get $formatId }}
{{ errorf "'%s' pattern format is defined more than once for the same property" $formatId }}
{{ end }}
{{ $scratch.Add $formatId (partial "property-type" $schema) }}
{{ $formatMap.Set $formatId (partial "property-type" $schema) }}
{{ end }}
{{/* First generate the type string for each format. */}}
{{ $types := slice }}
{{ range $formatId, $formatTypes := $scratch.Values }}
{{ range $formatId, $formatType := $formatMap.Values }}
{{ $formatKey := "string" }}
{{ if ne $formatId "string" }}
{{ with index site.Data "custom-formats" $formatId }}
@ -214,7 +212,7 @@
{{ end }}
{{ end }}
{{ $formatString := printf "{%s: %s}" $formatKey (delimit $formatTypes "|") }}
{{ $formatString := printf "{%s: %s}" $formatKey $formatType }}
{{ $types = $types | append $formatString }}
{{ end }}

@ -34,9 +34,10 @@ whole example, we use it to compose examples from other examples.
## Custom `x-pattern-format` key and custom formats
In JSON Schema, `format` is a property to convey semantic information about a
schema. We define `x-pattern-format` as a key on the schemas under
`patternProperties` with the same use as `format`, but that applies to the
pattern of the property. We also define custom values for formats with the `mx-`
prefix in `data/custom-formats.yaml`. Those values are recognized in the
rendered specification and link to the definition of the format.
In JSON Schema, [`format`](https://json-schema.org/understanding-json-schema/reference/string#format)
is a property to convey semantic information about a schema. We define
`x-pattern-format` as a key on the schemas under `patternProperties` with the
same use as `format`, but that applies to the pattern of the property. We also
define custom values for formats with the `mx-` prefix in
`data/custom-formats.yaml`. Those values are recognized in the rendered
specification and link to the definition of the format.

Loading…
Cancel
Save