Documentation
¶
Overview ¶
Package jsonschema derives, parses, and validates JSON Schema contracts.
Example ¶
package main
import (
"fmt"
"github.com/Tangerg/scope/core/jsonschema"
)
func main() {
type answer struct {
Value int `json:"value"`
}
schema, err := jsonschema.For[answer]()
if err != nil {
panic(err)
}
fmt.Println(schema.Validate([]byte(`{"value":42}`)))
}
Output: <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Constrained ¶ added in v0.54.0
type Constrained interface{ JSONSchemaConstraints() Constraints }
Constrained projects domain-owned limits onto a type or its JSON properties. Runtime validation and schema generation must derive from the same declarations. Implementations use a value receiver.
type Constraints ¶ added in v0.54.0
type Enumerator ¶ added in v0.47.0
type Enumerator interface {
JSONSchemaEnum() []string
}
Enumerator is implemented by named string types whose JSON value is one of a fixed set, so the schema derives the enum from the type that owns the vocabulary instead of restating it in field tags. Use a value receiver.
type Modeler ¶
type Modeler interface {
// JSONSchemaModel returns a non-nil typed value whose encoding/json/v2 wire shape
// exactly matches the receiver's custom encoding. The concrete model type is
// part of the schema contract and must remain stable across calls.
JSONSchemaModel() any
}
Modeler is implemented by values whose encoding/json/v2 representation is described by a separate typed model. Implementations must use a value receiver; nested and recursive models share one schema definition set. Each domain type retains its own constraints, so sharing a wire model never couples unrelated domain rules.
type Patterned ¶ added in v0.47.0
type Patterned interface {
JSONSchemaPattern() string
}
Patterned is implemented by named string types constrained to a regular expression, so the schema derives the pattern from the type that owns the rule instead of restating it in field tags. Use a value receiver.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema is an immutable, compiled JSON Schema. Its zero value is invalid and successfully constructed values are safe for concurrent validation.
func For ¶
For derives and compiles the JSON Schema contract for T. It follows encoding/json/v2 field names and options and the invopop/jsonschema tag dialect. Structs reject additional properties by default. Integer bounds preserve the Go representation even when field tags specify a wider semantic range. Named definitions include their Go package; moving a type changes its canonical schema and any deployment identities derived from it. Custom encodings require Modeler, except standard-library types with known wire contracts. Opaque Schema values cannot be derived from their Go fields. A string tag requires a custom encoding and Modeler: numeric constraints do not constrain JSON strings, and other ordinary types reject this tag in JSON v2.
func Parse ¶
Parse validates, compiles, and takes ownership of one self-contained JSON Schema document. References may resolve within the document or to standard metaschemas; implicit loading of external resources is prohibited.
func (Schema) Equal ¶ added in v0.54.0
Equal compares valid schemas by their canonical JSON contract.
func (Schema) JSON ¶
func (s Schema) JSON() json.RawMessage