Documentation
¶
Overview ¶
Package schemavalidate provides JSON Schema validation for generated HTTP handlers.
It loads a JSON Schema (draft 2020-12) from raw bytes, compiles it once at handler construction time, and validates request bodies on every request. Validation errors are mapped to errcode.ErrValidationFailed and written via httputil.WriteError. Error messages expose field names but never expose schema-internal details (lengths, ranges, patterns) to prevent oracle attacks.
ref: santhosh-tekuri/jsonschema/v6 (already in go.mod via contracttest) ref: deepmap/oapi-codegen security examples (request validation patterns)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteValidationError ¶
func WriteValidationError(ctx context.Context, w http.ResponseWriter, err error)
WriteValidationError writes an HTTP 400 response with the error from Validate. If err is not an *errcode.Error it is wrapped as ErrValidationFailed.
Types ¶
type Validator ¶
type Validator interface {
// Validate validates body against the compiled schema.
// Returns nil on success. Returns *errcode.Error (code=ErrValidationFailed)
// on schema violation. Error messages contain field names but never
// expose schema internals (lengths, ranges, regex patterns).
Validate(ctx context.Context, body []byte) error
}
Validator validates a JSON payload against a compiled JSON Schema.
func NewValidator ¶
NewValidator compiles schemaJSON as a JSON Schema (draft 2020-12) and returns a Validator. The compilation cost is paid once at construction time; each call to Validate is schema-free.
Returns error if schemaJSON is not valid JSON or is not a compilable schema.