jsonschema

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package jsonschema provides functionality for managing JSON Schema documents.

This package implements various strategies for generating JSON Schemas from different sources.

It also provides utilities for converting:

  • Go structs to JSON Schemas.
  • JSON Schemas to KCL Schemas.

Index

Constants

View Source
const (
	DefaultGeneratorType        GeneratorType = ""
	AutoGeneratorType           GeneratorType = "AUTO"
	ValueInferenceGeneratorType GeneratorType = "VALUE-INFERENCE"
	URLGeneratorType            GeneratorType = "URL"
	ChartPathGeneratorType      GeneratorType = "CHART-PATH"
	LocalPathGeneratorType      GeneratorType = "LOCAL-PATH"
	NoGeneratorType             GeneratorType = "NONE"

	DefaultValidatorType ValidatorType = ""
	KCLValidatorType     ValidatorType = "KCL"
	HelmValidatorType    ValidatorType = "HELM"
)
View Source
const EmptySchema string = `` /* 128-byte string literal not displayed */

Variables

View Source
var (
	DefaultAutoGenerator = NewAutoGenerator()
)
View Source
var (
	DefaultNoGenerator = NewNoGenerator()
)
View Source
var (
	// DefaultReaderGenerator is an opinionated [ReaderGenerator].
	DefaultReaderGenerator = NewReaderGenerator()
)
View Source
var (
	// DefaultValueInferenceGenerator is an opinionated [ValueInferenceGenerator].
	DefaultValueInferenceGenerator = NewValueInferenceGenerator(&ValueInferenceConfig{
		SkipRequired: true,
	})
)

Functions

func ConvertToKCLCompatibleJSONSchema

func ConvertToKCLCompatibleJSONSchema(jsonSchemaData []byte) ([]byte, error)

ConvertToKCLCompatibleJSONSchema converts a JSON schema to a JSON schema that is compatible with KCL schema generation (i.e. removing unsupported fields).

func ConvertToKCLSchema

func ConvertToKCLSchema(jsonSchemaData []byte, removeDefaults bool) ([]byte, error)

ConvertToKCLSchema converts a JSON schema to a KCL schema.

func GetFileFilter

func GetFileFilter(t GeneratorType) func(string) bool

Types

type AutoGenerator

type AutoGenerator struct{}

func NewAutoGenerator

func NewAutoGenerator() *AutoGenerator

func (*AutoGenerator) FromData

func (g *AutoGenerator) FromData(data []byte, refBasePath string) ([]byte, error)

func (*AutoGenerator) FromPaths

func (g *AutoGenerator) FromPaths(paths ...string) ([]byte, error)

type FileGenerator

type FileGenerator interface {
	FromPaths(paths ...string) ([]byte, error)
}

type GenOpt

type GenOpt func(*Reflected)

func Replace

func Replace(re *regexp.Regexp, repl string) GenOpt

type GeneratorType

type GeneratorType string

func GetGeneratorType

func GetGeneratorType(t string) GeneratorType

type NoGenerator

type NoGenerator struct{}

NoGenerator always returns an empty JSON Schema.

func NewNoGenerator

func NewNoGenerator() *NoGenerator

NewNoGenerator creates a new NoGenerator.

func (*NoGenerator) FromData

func (g *NoGenerator) FromData(_ []byte) ([]byte, error)

FromData returns an empty JSON Schema, regardless of the input data.

func (*NoGenerator) FromPaths

func (g *NoGenerator) FromPaths(_ ...string) ([]byte, error)

FromPaths returns an empty JSON Schema, regardless of the input paths.

type PropertyOpt

type PropertyOpt func(*invopopjsonschema.Schema)

func WithAllowAdditionalProperties

func WithAllowAdditionalProperties() PropertyOpt

func WithDefault

func WithDefault(defaultValue any) PropertyOpt

func WithEnum

func WithEnum(enum []any) PropertyOpt

func WithNoContent

func WithNoContent() PropertyOpt

func WithType

func WithType(t string) PropertyOpt

type ReaderGenerator

type ReaderGenerator struct{}

ReaderGenerator reads a JSON Schema from a given location and returns corresponding []byte representations.

func NewReaderGenerator

func NewReaderGenerator() *ReaderGenerator

NewReaderGenerator creates a new ReaderGenerator.

func (*ReaderGenerator) FromData

func (g *ReaderGenerator) FromData(data []byte, refBasePath string) ([]byte, error)

func (*ReaderGenerator) FromFile

func (g *ReaderGenerator) FromFile(path string) ([]byte, error)

func (*ReaderGenerator) FromPaths

func (g *ReaderGenerator) FromPaths(paths ...string) ([]byte, error)

FromPaths reads a JSON Schema from at least one of the given file paths or URLs and returns the corresponding []byte representation. It will return an error only if none of the paths provide a valid JSON Schema.

func (*ReaderGenerator) FromReader

func (g *ReaderGenerator) FromReader(r io.Reader, refBasePath string) ([]byte, error)

func (*ReaderGenerator) FromURL

func (g *ReaderGenerator) FromURL(schemaURL *url.URL) ([]byte, error)

type Reflected

type Reflected struct {
	Schema *invopopjsonschema.Schema
	// contains filtered or unexported fields
}

func (*Reflected) GenerateKCL

func (r *Reflected) GenerateKCL(w io.Writer, opts ...GenOpt) error

func (*Reflected) RemoveProperty

func (r *Reflected) RemoveProperty(key string)

func (*Reflected) SetOrRemoveProperty

func (r *Reflected) SetOrRemoveProperty(key string, setProperty bool, opts ...PropertyOpt)

func (*Reflected) SetProperty

func (r *Reflected) SetProperty(key string, opts ...PropertyOpt)

type Reflector

type Reflector struct {
	Reflector *invopopjsonschema.Reflector
}

func NewReflector

func NewReflector() *Reflector

func (*Reflector) AddGoComments

func (r *Reflector) AddGoComments(pkg, path string) error

func (*Reflector) Reflect

func (r *Reflector) Reflect(t reflect.Type, opts ...PropertyOpt) *Reflected

type ValidatorType

type ValidatorType string

func GetValidatorType

func GetValidatorType(t string) ValidatorType

type ValueInferenceConfig

type ValueInferenceConfig struct {
	// Consider yaml which is commented out.
	UncommentYAMLBlocks bool `json:"uncommentYAMLBlocks,omitempty"`
	// Parse and use helm-docs comments.
	HelmDocsCompatibilityMode bool `json:"helmDocsCompatibilityMode,omitempty"`
	// Keep the helm-docs prefix (--) in the schema.
	KeepHelmDocsPrefix bool `json:"keepHelmDocsPrefix,omitempty"`
	// Keep the whole leading comment (default: cut at empty line).
	KeepFullComment bool `json:"keepFullComment,omitempty"`
	// Remove the global key from the schema.
	RemoveGlobal bool `json:"removeGlobal,omitempty"`
	// Skip auto-generation of Title.
	SkipTitle bool `json:"skipTitle,omitempty"`
	// Skip auto-generation of Description.
	SkipDescription bool `json:"skipDescription,omitempty"`
	// Skip auto-generation of Required.
	SkipRequired bool `json:"skipRequired,omitempty"`
	// Skip auto-generation of Default.
	SkipDefault bool `json:"skipDefault,omitempty"`
	// Skip auto-generation of AdditionalProperties.
	SkipAdditionalProperties bool `json:"skipAdditionalProperties,omitempty"`
}

type ValueInferenceGenerator

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

ValueInferenceGenerator is a generator that infers a JSON Schema from one or more Helm values files.

func NewValueInferenceGenerator

func NewValueInferenceGenerator(c *ValueInferenceConfig) *ValueInferenceGenerator

NewValueInferenceGenerator creates a new ValueInferenceGenerator using the given ValueInferenceConfig.

func (*ValueInferenceGenerator) FromData

func (g *ValueInferenceGenerator) FromData(data []byte) ([]byte, error)

func (*ValueInferenceGenerator) FromPaths

func (g *ValueInferenceGenerator) FromPaths(paths ...string) ([]byte, error)

FromPaths generates a JSON Schema from one or more file paths pointing to Helm values files. If multiple file paths are provided, the schemas are merged into a single schema, using defaults from the file matching DefaultFileRegex (usually `values.yaml`).

Jump to

Keyboard shortcuts

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