Documentation
¶
Overview ¶
Package validate exposes spec validation as a public library API. Validate runs kin-openapi's spec validator and returns a structured list of findings (rule IDs, severities, source locations) so any caller can surface validation results in the same shape as `oasdiff validate` does on the command line.
Index ¶
Constants ¶
const ( // MultipleOfNotPositiveID: JSON Schema validation, "multipleOf" MUST be a // number strictly greater than 0. MultipleOfNotPositiveID = "multiple-of-not-positive" // SubschemasEmptyID: allOf / anyOf / oneOf MUST be a non-empty array. SubschemasEmptyID = "subschemas-empty" // The remaining rules describe unsatisfiable bounds: min above max means no // instance can validate. MinimumExceedsMaximumID = "minimum-exceeds-maximum" MinLengthExceedsMaxLengthID = "min-length-exceeds-max-length" MinItemsExceedsMaxItemsID = "min-items-exceeds-max-items" MinPropertiesExceedsMaxPropsID = "min-properties-exceeds-max-properties" MinContainsExceedsMaxContainsID = "min-contains-exceeds-max-contains" // EnumEmptyID: JSON Schema says an "enum" array SHOULD have at least one // element; an empty one accepts nothing. EnumEmptyID = "enum-empty" // ConstNotInEnumID: "const" and "enum" together restrict to their // intersection, so a const outside the enum accepts nothing. ConstNotInEnumID = "const-not-in-enum" )
Schema-constraint coherence lints: a single schema whose own keywords contradict each other or violate a JSON Schema MUST. The parser accepts all of these (they are well-formed), but no instance can ever satisfy them, so they are authoring mistakes rather than choices. See oasdiff/oasdiff#927.
Severity follows the spec text. A literal MUST violation is an error; a combination the spec does not forbid outright but that no value can satisfy (or a SHOULD) is a warning.
const AmbiguousParameterSerializationID = "ambiguous-parameter-serialization"
AmbiguousParameterSerializationID flags a parameter whose schema type union mixes a structured type (array or object) with a scalar. Style serialization is defined per type and arrays/objects serialize differently from scalars, so for `type: [array, integer]` a server cannot tell whether ?token=5 is the array ["5"] or the integer 5. Valid JSON Schema, under-specified for OpenAPI serialization: an oasdiff-native SHOULD-level lint. See oasdiff/oasdiff#1055.
const DuplicateEnumValueID = "duplicate-enum-value"
DuplicateEnumValueID flags an enum array that contains the same value more than once. JSON Schema says enum elements SHOULD be unique (a recommendation, not a MUST), so kin-openapi does not reject them; this is an oasdiff-native SHOULD-level lint. See oasdiff/oasdiff#980.
const RequiredWithDefaultID = "required-with-default"
RequiredWithDefaultID flags a required parameter or property that also declares a default value. The two contradict each other: `required` means the value must always be present, so the `default` (the value assumed when it is absent) can never apply. It is dead and misleading, and usually a mistake, the author meant the field to be optional. Valid OpenAPI, an oasdiff-native SHOULD-level lint.
const TypeFormatMismatchID = "type-format-mismatch"
TypeFormatMismatchID flags a schema whose `format` is a known format defined for a different `type`, e.g. {"type": "integer", "format": "date-time"}. The format never applies to that instance type, so spec-compliant tooling silently ignores it: not a spec violation, almost always an authoring slip where the type or the format was changed without the other. See oasdiff/oasdiff#1019.
Variables ¶
This section is empty.
Functions ¶
func RuleDescription ¶ added in v1.27.0
RuleDescription returns a short description of what a validate rule reports, or "" for an unknown ID.
func RuleIDs ¶ added in v1.24.0
func RuleIDs() []string
RuleIDs returns every rule ID validate can emit, sorted.
func RuleLevel ¶ added in v1.27.0
RuleLevel returns the severity of a validate rule.
The version-gate rules (`<field>-field-for-3-1-plus` and friends) are WARN as a family rather than one entry each, matched the same way versionGateDescription describes them: a 3.1-only field in an older document is a portability problem, not a structural break. Deriving them keeps a newly gated field added upstream classified without a change here.
An unknown id gets ERR, the same default an unrecognised error takes at runtime.
func Validate ¶
func Validate(spec *openapi3.T, source string) formatters.Findings
Validate validates the spec against the OpenAPI and JSON Schema rules (kin-openapi's validator), returning a flat list of findings. Each finding carries a stable rule ID, severity, source location (when origin tracking is enabled on the loader), and a fingerprint for cross-spec matching.
source is the display name for the spec (typically its file path). It appears in each finding's Source.File so callers can render file:line:column anchors. Pass an empty string when there is no meaningful source name (e.g. specs loaded from memory).
A valid spec yields a non-nil empty Findings (nil only for the nil-spec guard), so the formatters' nil guard doesn't collapse `[]` to empty bytes.
Types ¶
This section is empty.