schemavalidate

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 9 Imported by: 0

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

func NewValidator(schemaJSON []byte) (Validator, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL