schema

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 9 Imported by: 0

Documentation

Overview

Package schema provides schema parsing and validation utilities for HTTP body validation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanOptionalMarkers

func CleanOptionalMarkers(schema *JSONSchema)

CleanOptionalMarkers removes optional markers from the schema.

func IsOptional

func IsOptional(schema *JSONSchema) bool

IsOptional checks if a schema was marked as optional during Zod parsing.

func SchemaToMap

func SchemaToMap(schema *JSONSchema) (map[string]any, error)

SchemaToMap converts a JSONSchema to a map for JSON serialization.

Types

type GoStructParser

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

GoStructParser parses Go struct definitions into JSON Schema.

type JSONSchema

type JSONSchema struct {
	Type                 string                 `json:"type,omitempty"`
	Properties           map[string]*JSONSchema `json:"properties,omitempty"`
	Items                *JSONSchema            `json:"items,omitempty"`
	Required             []string               `json:"required,omitempty"`
	AdditionalProperties *bool                  `json:"additionalProperties,omitempty"`
	AnyOf                []*JSONSchema          `json:"anyOf,omitempty"`
	Ref                  string                 `json:"$ref,omitempty"`
	Definitions          map[string]*JSONSchema `json:"$defs,omitempty"`
}

JSONSchema represents a JSON Schema object. This is a simplified representation that can be marshaled to standard JSON Schema.

func ParseGoStruct

func ParseGoStruct(input string) (*JSONSchema, error)

ParseGoStruct parses one or more Go struct definitions and returns a JSON Schema. The first struct defined is treated as the root schema. Nested structs are supported through $defs references.

Returns an error if the schema contains forbidden types (any, interface{}). Returns warnings for types that allow arbitrary data (json.RawMessage, []byte).

Example input:

type Response struct {
    Status string `json:"status"`
    Data   []Item `json:"data"`
}
type Item struct {
    ID   int    `json:"id"`
    Name string `json:"name,omitempty"`
}

func ParseZodSchema

func ParseZodSchema(input string) (*JSONSchema, error)

ParseZodSchema parses a Zod schema definition and returns a JSON Schema.

Supported Zod types:

  • z.string()
  • z.number()
  • z.boolean()
  • z.null()
  • z.array(schema)
  • z.object({ ... })
  • z.record(schema) - becomes object with additionalProperties

Supported modifiers:

  • .optional()
  • .nullable()
  • .default(...)

Returns an error if the schema contains forbidden types (z.any(), z.unknown()).

Example input:

z.object({
  status: z.string(),
  data: z.array(z.object({
    id: z.number(),
    name: z.string().optional()
  }))
})

func PostProcessZodSchema

func PostProcessZodSchema(schema *JSONSchema) *JSONSchema

PostProcessZodSchema cleans up a Zod-parsed schema. It removes optional markers and updates required arrays.

type ParseResult

type ParseResult struct {
	Schema   *JSONSchema
	Warnings []string
}

ParseResult contains the parsed schema and any warnings encountered.

func ParseGoStructWithWarnings

func ParseGoStructWithWarnings(input string) (*ParseResult, error)

ParseGoStructWithWarnings parses Go struct definitions and returns the schema along with warnings. Returns an error if the schema contains forbidden types (any, interface{}). Warnings are returned for types that allow arbitrary data (json.RawMessage, []byte).

type Validator

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

Validator validates JSON data against a schema.

func NewValidator

func NewValidator(schemaStr string, format types.SchemaFormat) (*Validator, error)

NewValidator creates a new validator from a schema definition.

func NewValidatorFromJSONSchema

func NewValidatorFromJSONSchema(schema *JSONSchema) (*Validator, error)

NewValidatorFromJSONSchema creates a validator from a pre-parsed JSONSchema.

func (*Validator) GetSchema

func (v *Validator) GetSchema() *jsonschema.Schema

GetSchema returns the JSON Schema used by this validator.

func (*Validator) Validate

func (v *Validator) Validate(data []byte) *types.ValidationResult

Validate validates a JSON value against the schema.

func (*Validator) ValidateValue

func (v *Validator) ValidateValue(value any) *types.ValidationResult

ValidateValue validates an already-parsed value against the schema.

Jump to

Keyboard shortcuts

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