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 ¶
- type Schema
- func Array(item *Schema, description string) *Schema
- func Boolean(description string) *Schema
- func Integer(description string) *Schema
- func Number(description string) *Schema
- func Object(props map[string]*Schema) *Schema
- func String(description string) *Schema
- func StringEnum(values []string, description string) *Schema
- func (s *Schema) AdditionalPropertiesFalse() *Schema
- func (s *Schema) MarshalJSON() ([]byte, error)
- func (s *Schema) ToMap() map[string]interface{}
- func (s *Schema) WithDefault(v interface{}) *Schema
- func (s *Schema) WithDescription(d string) *Schema
- func (s *Schema) WithID(id string) *Schema
- func (s *Schema) WithRequired(names ...string) *Schema
- func (s *Schema) WithSchemaURI(uri string) *Schema
- func (s *Schema) WithTitle(t string) *Schema
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 Object ¶
Object returns a new object Schema with the given properties. Use AdditionalPropertiesFalse() / AdditionalPropertiesTrue() to set the stricture if desired.
func StringEnum ¶
StringEnum returns a new string Schema constrained to the given enum values.
func (*Schema) AdditionalPropertiesFalse ¶
AdditionalPropertiesFalse sets additionalProperties:false on s and returns s for chaining.
func (*Schema) MarshalJSON ¶
MarshalJSON renders a Schema with stable, alphabetically-sorted property keys so generated schemas produce deterministic output across runs.
func (*Schema) ToMap ¶
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 ¶
WithDefault attaches a default value and returns s for chaining.
func (*Schema) WithDescription ¶
WithDescription attaches a description and returns s for chaining.
func (*Schema) WithRequired ¶
WithRequired sets the required field list and returns s for chaining.
func (*Schema) WithSchemaURI ¶
WithSchemaURI attaches a $schema reference and returns s for chaining.