Compare commits

...

4 Commits

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

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

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

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

Loading…
Cancel
Save