schema

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package schema provides JSON Schema parsing, validation, and value coercion for Runware model request schemas. It is transport-agnostic and contains no CLI or HTTP dependencies.

Index

Constants

View Source
const (
	TypeString  = "string"
	TypeInteger = "integer"
	TypeArray   = "array"
	TypeObject  = "object"
)

JSON Schema "type" field values.

Variables

View Source
var ManagedFields = map[string]ManagedField{
	"model": {
		Protected: true,
		Hint:      "pass the model as the first positional argument",
	},
	"taskType": {
		Protected: true,
		Hint:      "use the --task-type flag instead",
	},
	"taskUUID": {
		Protected: false,
	},
	"deliveryMethod": {
		Protected: false,
	},
}

ManagedFields is the set of payload keys managed by the run command. Every entry is excluded from required-field checks and shell completions. Entries with Protected: true additionally reject user-supplied key=value args. deliveryMethod is not protected: it may be overridden via --delivery-method or a key=value argument.

Functions

func AllLeafsProvided

func AllLeafsProvided(prefix string, node Node, provided map[string]struct{}) bool

AllLeafsProvided reports whether every non-AutoField leaf path of node, rooted at prefix, is present in provided.

func DeepSet

func DeepSet(payload map[string]any, path []string, value any)

DeepSet writes value into payload at the location described by path, creating intermediate maps and slices as needed. Existing maps are merged rather than replaced.

func ExtractDeliveryMethod

func ExtractDeliveryMethod(node Node) (options []string, defaultVal string)

ExtractDeliveryMethod returns the allowed delivery method values and the schema-specified default for the model's deliveryMethod property.

func ExtractTaskType

func ExtractTaskType(node Node) (string, bool)

ExtractTaskType inspects node.Properties["taskType"] for a constant value using const → enum[0] → default precedence. Returns the task type string and true on success, or ("", false) when the schema does not encode a constant taskType.

func IsAuto

func IsAuto(field string) bool

IsAuto reports whether field is managed automatically by the run command and should be excluded from required-field validation and shell completions.

func IsNumeric

func IsNumeric(s string) bool

IsNumeric reports whether s is a non-negative integer string.

func IsProtected

func IsProtected(field string) (hint string, ok bool)

IsProtected reports whether field is a system-managed key that must not be supplied as a user key=value argument. When blocked, hint describes the correct mechanism to use instead.

func MustAtoi

func MustAtoi(s string) int

MustAtoi converts a numeric string to int. Callers must ensure IsNumeric(s).

func NormalizeProvidedKey

func NormalizeProvidedKey(arg string, node Node) string

NormalizeProvidedKey returns the canonical dot-notation key for a "key=value" completion argument by running it through ParseKV to apply auto-index sugar. Falls back to the verbatim key portion when ParseKV fails.

func ParseKV

func ParseKV(arg string, node Node) (path []string, value any, err error)

ParseKV splits a "key=value" argument on the first '=' and coerces the value to the Go type implied by the model schema. The key may use dot-notation to address nested fields (e.g. "speech.text=Hello" or "messages.0.role=user"). A non-numeric segment inside an array-typed parent is treated as index 0 (sugar for the common single-item case: "messages.role=user"). Returns a path slice (length ≥ 1) and the coerced value.

func ResolveDeliveryMethod

func ResolveDeliveryMethod(flagVal string, payload map[string]any, node Node) string

ResolveDeliveryMethod determines the delivery method to use for a request. Priority: payload value > flagVal > schema default > first schema option.

func ValidateAllOf

func ValidateAllOf(node Node, payload map[string]any) error

ValidateAllOf checks structural constraints expressed in the schema's allOf array: dependentRequired fields and oneOf const-property combinations.

func ValidateConstraints added in v0.13.1

func ValidateConstraints(node Node, payload map[string]any) error

ValidateConstraints checks the value constraints declared on each schema property against the corresponding payload value: numeric bounds, string length/pattern/format, and array length/uniqueness. It recurses into nested objects and array items, mirroring ValidateEnums.

func ValidateEnums added in v0.12.0

func ValidateEnums(node Node, payload map[string]any) error

ValidateEnums checks that every string value in payload that maps to a schema property with a string-enum constraint (oneOf consts or direct enum) is one of the allowed values. It recurses into nested objects and arrays.

func ValidateRequired

func ValidateRequired(node Node, payload map[string]any) error

ValidateRequired returns an error if any required schema field (excluding auto-injected fields) is absent from payload.

Types

type ManagedField

type ManagedField struct {
	// Protected indicates that the user must not supply this field via a
	// key=value CLI argument. When true, Hint is shown in the error message
	// directing the user to the correct mechanism.
	Protected bool
	// Hint is a short human-readable explanation shown when a protected field
	// is rejected. Empty for non-protected managed fields.
	Hint string
}

ManagedField describes a system-managed payload key that is injected automatically by the run command.

type Node

type Node struct {
	Title       string          `json:"title"`
	Description string          `json:"description"`
	Type        SchemaType      `json:"type"`
	Default     json.RawMessage `json:"default"`
	Properties  map[string]Node `json:"properties"`
	Required    []string        `json:"required"`
	// Enum and Const are used to extract constant values such as taskType.
	Enum  []json.RawMessage `json:"enum"`
	Const json.RawMessage   `json:"const"`
	// Items holds the schema for elements of an array-typed property.
	// Used to coerce values when dot-notation paths descend into arrays.
	Items *Node `json:"items"`
	// Minimum, Maximum, and MultipleOf carry numeric value constraints. Pointers
	// so an absent keyword is distinguishable from a real zero bound (e.g. a
	// minimum of 0 is meaningful and must not be treated as "no minimum").
	Minimum          *float64 `json:"minimum"`
	Maximum          *float64 `json:"maximum"`
	MultipleOf       *float64 `json:"multipleOf"`
	ExclusiveMinimum *float64 `json:"exclusiveMinimum"`
	ExclusiveMaximum *float64 `json:"exclusiveMaximum"`
	// String constraints. Pattern and Format are absent when empty.
	MinLength *int   `json:"minLength"`
	MaxLength *int   `json:"maxLength"`
	Pattern   string `json:"pattern"`
	Format    string `json:"format"`
	// Array constraints.
	MinItems    *int `json:"minItems"`
	MaxItems    *int `json:"maxItems"`
	UniqueItems bool `json:"uniqueItems"`
	// AllOf, OneOf, and DependentRequired support structural constraints used by
	// model schemas to express mutually-exclusive option sets (e.g. dimension
	// combinations) and co-dependent fields.
	AllOf             []Node              `json:"allOf"`
	OneOf             []Node              `json:"oneOf"`
	DependentRequired map[string][]string `json:"dependentRequired"`
}

Node is a minimal representation of a JSON Schema node used for task-type extraction, input validation, and value coercion. It covers all fields needed by the run command; it intentionally omits fields only used by model schema display.

type SchemaType added in v0.11.0

type SchemaType string

SchemaType holds the JSON Schema "type" value. JSON Schema allows "type" to be either a string or an array of strings (e.g. ["string", "null"] for nullable fields). UnmarshalJSON normalizes both forms to the first non-"null" type string.

func (*SchemaType) UnmarshalJSON added in v0.11.0

func (t *SchemaType) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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