Documentation
¶
Overview ¶
Package validations owns cross-builder validation and coercion concerns shared by the schema, parameters, responses, and items/headers code paths.
The package has two halves:
- Value coercion (this file) — turns raw annotation text into the Go value implied by the target schema's type+format, for keywords whose payload is a primitive literal (default:, example:, enum:).
- Shape legality (shape.go) — answers whether a given keyword is legal on a schema of a given Swagger type.
Details ¶
See [§contract](./README.md#contract) — why these helpers live in the builder layer rather than in the grammar parser.
Index ¶
- func CoerceEnum(val string, s *spec.SimpleSchema) []any
- func CoerceJSONOrString(s string) any
- func CoerceValue(s string, schema *spec.SimpleSchema) (any, error)
- func IsFormatCompatible(schemaType, format string) (ok bool, hint string)
- func IsLegalForType(keyword grammar.Keyword, schemaType string) (ok bool, hint string)
- func ParseDefault(s, schemaType, schemaFormat string) (any, error)
- func ParseEnumValues(val, schemaType, schemaFormat string) []any
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CoerceEnum ¶
func CoerceEnum(val string, s *spec.SimpleSchema) []any
CoerceEnum turns an `enum: …` annotation value into a typed []any. Accepts the JSON-array form (`enum: ["a","b"]`) and the comma-list form (`enum: a, b`). Per-value typing is applied via CoerceValue against the target schema.
Details ¶
See [§enum-shapes](./README.md#enum-shapes) — how the two input forms are detected and how each element is normalised before per-value coercion.
func CoerceJSONOrString ¶ added in v0.35.0
CoerceJSONOrString coerces a default:/example: value whose target type is unknown — the case of a $ref override arm, which carries no Type of its own (the type lives on the referenced definition, not on the allOf sibling). A JSON object- or array-literal body is parsed structurally so it matches the direct-field path (quirk G3): `example: {"k":"v"}` yields an object on a $ref'd field just as it does on a plain map field. Any other value is treated as a string scalar, with surrounding double quotes stripped (mirroring CoerceValue's "string" arm).
Never errors: a malformed JSON literal falls back to the unquoted string. Scalar literals (numbers, booleans) are intentionally NOT coerced here — the referenced type is unknown, so a bare `example: 42` against a string-format $ref must not be silently turned into a number.
func CoerceValue ¶
func CoerceValue(s string, schema *spec.SimpleSchema) (any, error)
CoerceValue converts a raw annotation value to the Go representation implied by the target schema's Type/Format. Used by default:/example: setters where the annotation body is a primitive literal whose meaning depends on the target: `default: 3` becomes int(3) against `Type: "integer"`, "3" against `Type: "string"`, and so on.
A nil schema yields the raw string unchanged. Numeric and boolean parsing errors are surfaced to the caller; JSON parse failures on object/array targets are absorbed and the raw string is returned.
Dispatch is on spec.SimpleSchema.TypeName — Format wins when set, otherwise Type is consulted.
Details ¶
See [§coercion-dispatch](./README.md#coercion-dispatch) — the per-type dispatch table, and the rationale for absorbing object/array JSON parse failures.
func IsFormatCompatible ¶ added in v0.35.0
IsFormatCompatible reports whether a `format` is meaningful on a schema whose resolved Swagger `type` is already fixed (e.g. by a `swagger:type` override). It answers the question "may this strfmt format ride on top of this type?" — the supplementary-format rule for the swagger:type + swagger:strfmt combination, where swagger:type wins on the type axis and the format applies only when it is consistent with that type.
Rules (see README §format-compat):
- type "string": any format applies (strfmt is string-oriented).
- type "integer": the integer formats int{n} and the swagger-extension uint{n} apply.
- type "number": the integer formats, the uint{n} extension, AND the float widths apply.
- any other type (boolean / object / array / file): no format applies.
An empty format is trivially compatible (there is nothing to apply). An empty/unknown schemaType is accepted best-effort, mirroring IsLegalForType — the caller has no type to check against. A false result carries a human-readable hint suitable for a diagnostic message.
func IsLegalForType ¶
IsLegalForType reports whether keyword is legal on a schema with the given resolved Swagger type. Returns ok=true with empty hint when the keyword has no type constraint or the type matches. Returns ok=false with a human-readable hint when the type mismatches the keyword's domain.
Empty schemaType is treated as "type unknown" and accepted; the caller decides whether to apply the keyword to a typeless schema. Format is not consulted — the domain rules apply at the type level.
Details ¶
See [§empty-type](./README.md#empty-type) — the best-effort-apply rule for unknown schema types, and why Format is intentionally kept off this axis.
func ParseDefault ¶
ParseDefault is the two-axis entry point for default:/example: coercion. It dispatches on schemaType alone — schemaFormat is accepted for surface stability but not consulted today.
Details ¶
See [§coercion-dispatch](./README.md#coercion-dispatch) and [§format-axis](./README.md#format-axis) — why the two-axis surface exists alongside CoerceValue and which refinements the schemaFormat argument is reserved for.
func ParseEnumValues ¶
ParseEnumValues is the two-axis entry point for enum: coercion. Mirrors ParseDefault — dispatches on schemaType, with schemaFormat reserved for future per-format paths.
Types ¶
This section is empty.