jsonschema

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package jsonschema provides a typed builder for JSON Schema documents.

It is a small subset focused on the shapes krit emits: krit.yml schema generation in internal/schema and the MCP tool inputSchema fields in internal/mcp. Where the previous code wrote

map[string]interface{}{"type": "object", "properties": ...}

callers now build a *Schema value directly. *Schema implements json.Marshaler via struct tags, so the wire format is unchanged.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Schema

type Schema struct {
	Schema      string `json:"$schema,omitempty"`
	ID          string `json:"$id,omitempty"`
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`

	Type   string `json:"type,omitempty"`
	Format string `json:"format,omitempty"`

	// Properties is ordered alphabetically by Marshal. Insertion order is not
	// preserved — callers that need stable output should not rely on it.
	Properties map[string]*Schema `json:"-"`

	// AdditionalProperties as a bool: nil means absent, &false means
	// "no extra keys allowed", &true means "allowed" (rarely emitted).
	AdditionalProperties *bool `json:"additionalProperties,omitempty"`

	Items    *Schema  `json:"items,omitempty"`
	Required []string `json:"required,omitempty"`
	Enum     []string `json:"enum,omitempty"`

	Default interface{} `json:"default,omitempty"`
}

Schema is a JSON Schema fragment. Only fields krit currently emits are modelled — extend on demand. All optional fields use omitempty.

Default is interface{} because JSON Schema "default" can be any JSON value (int, bool, string, array, object). The interface{} is confined to this single field rather than scattered through every literal.

func Array

func Array(item *Schema, description string) *Schema

Array returns a new array Schema whose items match the given Schema.

func Boolean

func Boolean(description string) *Schema

Boolean returns a new boolean Schema.

func Integer

func Integer(description string) *Schema

Integer returns a new integer Schema.

func Number

func Number(description string) *Schema

Number returns a new number Schema (float).

func Object

func Object(props map[string]*Schema) *Schema

Object returns a new object Schema with the given properties. Use AdditionalPropertiesFalse() / AdditionalPropertiesTrue() to set the stricture if desired.

func String

func String(description string) *Schema

String returns a new string Schema with an optional description.

func StringEnum

func StringEnum(values []string, description string) *Schema

StringEnum returns a new string Schema constrained to the given enum values.

func (*Schema) AdditionalPropertiesFalse

func (s *Schema) AdditionalPropertiesFalse() *Schema

AdditionalPropertiesFalse sets additionalProperties:false on s and returns s for chaining.

func (*Schema) MarshalJSON

func (s *Schema) MarshalJSON() ([]byte, error)

MarshalJSON renders a Schema with stable, alphabetically-sorted property keys so generated schemas produce deterministic output across runs.

func (*Schema) ToMap

func (s *Schema) ToMap() map[string]interface{}

ToMap converts the schema to a generic map[string]interface{} suitable for callers that still consume the legacy untyped representation. Producers should prefer working with *Schema directly and let json.Marshal handle serialisation.

func (*Schema) WithDefault

func (s *Schema) WithDefault(v interface{}) *Schema

WithDefault attaches a default value and returns s for chaining.

func (*Schema) WithDescription

func (s *Schema) WithDescription(d string) *Schema

WithDescription attaches a description and returns s for chaining.

func (*Schema) WithID

func (s *Schema) WithID(id string) *Schema

WithID attaches a $id reference and returns s for chaining.

func (*Schema) WithRequired

func (s *Schema) WithRequired(names ...string) *Schema

WithRequired sets the required field list and returns s for chaining.

func (*Schema) WithSchemaURI

func (s *Schema) WithSchemaURI(uri string) *Schema

WithSchemaURI attaches a $schema reference and returns s for chaining.

func (*Schema) WithTitle

func (s *Schema) WithTitle(t string) *Schema

WithTitle attaches a title and returns s for chaining.

Jump to

Keyboard shortcuts

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