jsonschema

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: BSD-2-Clause Imports: 10 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

View Source
var DefaultNullCoercion = NullLeave

DefaultNullCoercion is the policy applied by validators created without an explicit WithNullCoercion option. Set it once at startup to change the behaviour globally.

Functions

This section is empty.

Types

type NullCoercion added in v1.5.0

type NullCoercion int

NullCoercion controls how a JSON null (produced by an empty YAML value such as `key:`) is treated, just before validation, when the schema at that location expects a scalar type (string, number, integer or boolean).

Null values whose schema expects an object or an array are ALWAYS coerced to an empty object ({}) or empty array ([]) respectively, regardless of this setting: an empty mapping/sequence is the unambiguous YAML intent there. This knob only governs the genuinely ambiguous scalar case.

const (
	// NullLeave keeps a scalar null as null. This is the JSON-Schema-pure
	// behaviour: if the schema does not declare the field nullable
	// (type includes "null"), validation will reject the null. Default.
	NullLeave NullCoercion = iota
	// NullDrop removes a null-valued scalar key from its parent object,
	// treating an empty value as "unset" so the field falls back to its
	// default. A required-but-empty field then fails as missing.
	NullDrop
	// NullZero replaces a scalar null with the zero value of the schema's
	// declared type: "" for string, 0 for number/integer, false for boolean.
	NullZero
)

type Option added in v1.5.0

type Option func(*Validator)

Option configures a Validator.

func WithNullCoercion added in v1.5.0

func WithNullCoercion(policy NullCoercion) Option

WithNullCoercion sets the scalar-null coercion policy for this validator, overriding DefaultNullCoercion. See NullCoercion for details.

type Validator

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

Validator validates configuration against JSON Schema.

func New

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

New creates a validator from schema bytes.

func NewFromReader

func NewFromReader(r io.Reader, opts ...Option) (*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