jschema

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package jschema provides JSON Schema loading, parsing, and traversal utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractKeyOrder

func ExtractKeyOrder(s *jsonschema.Schema) (map[string][]string, error)

ExtractKeyOrder parses raw JSON and extracts the order of keys for all "properties" objects.

func ExtractKeyOrderFromJSON added in v0.2.1

func ExtractKeyOrderFromJSON(data []byte) (map[string][]string, error)

ExtractKeyOrderFromJSON extracts the order of keys for all "properties" objects directly from raw JSON bytes, preserving the original file order.

func ExtractKeyOrderFromYAML added in v0.2.1

func ExtractKeyOrderFromYAML(data []byte) (map[string][]string, error)

ExtractKeyOrderFromYAML extracts the order of keys for all "properties" objects from raw YAML bytes, using yaml.Node which preserves insertion order.

func ExtractYAMLNodeKeyOrder added in v0.2.1

func ExtractYAMLNodeKeyOrder(node *yaml.Node, path string, result map[string][]string)

ExtractYAMLNodeKeyOrder recursively extracts key ordering from a yaml.Node tree. It collects the ordered keys for all mapping nodes whose path ends with "properties".

func IsFileRef

func IsFileRef(ref string) bool

IsFileRef returns true if ref is an external file reference. File refs do not start with "#/".

func RewriteRefs

func RewriteRefs(schema *jsonschema.Schema)

RewriteRefs rewrites all $ref attributes to use #/$defs/Name format. It handles both #/components/schemas/Name and #/definitions/Name refs.

func SetPropertyOrder added in v0.2.1

func SetPropertyOrder(schema *jsonschema.Schema, keyOrder map[string][]string)

SetPropertyOrder walks the schema tree and sets PropertyOrder on each node that has properties, using the key order extracted from raw bytes.

func Traverse

func Traverse(schema *jsonschema.Schema, resolver RefResolver) iter.Seq[*jsonschema.Schema]

Traverse returns an iterator over all schemas in the tree. It handles cycles by tracking visited schemas. If resolver is provided, it follows $ref links to their targets.

func TraverseDefs

func TraverseDefs(schema *jsonschema.Schema) iter.Seq2[string, *jsonschema.Schema]

TraverseDefs returns an iterator over $defs in topological order. Dependencies are yielded before the schemas that reference them. Each iteration yields (name, schema) pairs.

Types

type Loader

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

Loader loads schemas from a filesystem.

func NewLoader

func NewLoader(fsys fs.FS) *Loader

NewLoader creates a Loader that reads from the given filesystem.

func (*Loader) LoadFile

func (l *Loader) LoadFile(filePath string) (*jsonschema.Schema, error)

LoadFile loads and parses a schema file. The format is determined from the file extension.

func (*Loader) ResolveRefs

func (l *Loader) ResolveRefs(schema *jsonschema.Schema, basePath string) error

ResolveRefs resolves all external file $refs in the schema tree in-place. It recursively loads referenced schemas and replaces the ref with the loaded content. Internal refs (starting with #/) are left unchanged.

type RefResolver

type RefResolver func(ref string) *jsonschema.Schema

RefResolver resolves $ref strings to schemas. Return nil if the ref cannot be resolved.

Jump to

Keyboard shortcuts

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