jsonschema

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package jsonschema provides JSON Schema validation for go-config.

This package implements the config.Validator interface using the kaptinlin/jsonschema library, supporting JSON Schema draft-2020-12 and earlier drafts.

Basic Usage

schema := []byte(`{
    "type": "object",
    "properties": {
        "port": {"type": "integer", "minimum": 1, "maximum": 65535}
    },
    "required": ["port"]
}`)

validator, err := jsonschema.New(schema)
if err != nil {
    log.Fatal(err)
}

cfg, errs := config.NewBuilder().
    AddCollector(myCollector).
    WithValidator(validator).
    Build()

Using WithJSONSchema Convenience Method

schemaFile, _ := os.Open("schema.json")
defer schemaFile.Close()

builder, err := config.NewBuilder().
    AddCollector(myCollector).
    WithJSONSchema(schemaFile)
if err != nil {
    log.Fatal(err)
}

cfg, errs := builder.Build(context.Background())

Error Handling

Validation errors are returned as []config.ValidationError, each containing:

  • Path: The KeyPath to the invalid field
  • Code: Machine-readable error code (e.g., "type", "required", "minimum")
  • Message: Human-readable error description
  • Range: Source position (when position tracking is available)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates configuration against JSON Schema.

func New

func New(schemaData []byte) (*Validator, error)

New creates a validator from schema bytes.

func NewFromReader

func NewFromReader(r io.Reader) (*Validator, error)

NewFromReader creates a validator from an io.Reader.

func (*Validator) SchemaType

func (v *Validator) SchemaType() string

SchemaType returns JSONSchema string.

func (*Validator) Validate

func (v *Validator) Validate(root *tree.Node) []validator.ValidationError

Validate implements validator.Validator.

Jump to

Keyboard shortcuts

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