schema

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package schema 定义 FlowLink 使用的 JSON Schema 子集和校验工具。

该包用于配置结构校验和前端表单生成,避免各层手写不一致的 schema map。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicValidator

type BasicValidator struct{}

BasicValidator 是 FlowLink 内置的 JSON Schema 子集校验器。

func NewBasicValidator

func NewBasicValidator() *BasicValidator

NewBasicValidator 创建默认 schema 校验器。

func (*BasicValidator) ValidateSchema

func (v *BasicValidator) ValidateSchema(_ context.Context, s *Schema) []*ValidationError

ValidateSchema 校验 schema 定义自身是否合法。

func (*BasicValidator) ValidateValue

func (v *BasicValidator) ValidateValue(_ context.Context, s *Schema, value interface{}) []*ValidationError

ValidateValue 校验一个值是否满足给定 schema。

type Schema

type Schema struct {
	Schema               string                 `json:"$schema,omitempty"`
	Title                string                 `json:"title,omitempty"`
	Description          string                 `json:"description,omitempty"`
	Type                 Type                   `json:"type,omitempty"`
	Properties           map[string]*Schema     `json:"properties,omitempty"`
	Items                *Schema                `json:"items,omitempty"`
	Required             []string               `json:"required,omitempty"`
	Enum                 []interface{}          `json:"enum,omitempty"`
	Default              interface{}            `json:"default,omitempty"`
	Examples             []interface{}          `json:"examples,omitempty"`
	Format               string                 `json:"format,omitempty"`
	Pattern              string                 `json:"pattern,omitempty"`
	Minimum              *float64               `json:"minimum,omitempty"`
	Maximum              *float64               `json:"maximum,omitempty"`
	MinLength            *int                   `json:"minLength,omitempty"`
	MaxLength            *int                   `json:"maxLength,omitempty"`
	MinItems             *int                   `json:"minItems,omitempty"`
	MaxItems             *int                   `json:"maxItems,omitempty"`
	AdditionalProperties interface{}            `json:"additionalProperties,omitempty"`
	UI                   map[string]interface{} `json:"x-ui,omitempty"`
}

Schema 是前后端共同使用的 JSON Schema 子集。 第一版只约束常用字段,避免前端表单和后端校验理解不一致。

func Boolean

func Boolean(title string) *Schema

Boolean 创建布尔类型 schema。

func Integer

func Integer(title string) *Schema

Integer 创建整数类型 schema。

func Object

func Object(properties map[string]*Schema, required ...string) *Schema

Object 创建对象类型 schema。

func String

func String(title string) *Schema

String 创建字符串类型 schema。

type Type

type Type string

Type 是 FlowLink 支持的 JSON Schema 类型枚举。

const (
	TypeString  Type = "string"
	TypeNumber  Type = "number"
	TypeInteger Type = "integer"
	TypeBoolean Type = "boolean"
	TypeObject  Type = "object"
	TypeArray   Type = "array"
	TypeNull    Type = "null"
)

type ValidationError

type ValidationError struct {
	Path    string `json:"path"`
	Message string `json:"message"`
}

ValidationError 描述 schema 或值校验失败的位置和原因。

func (*ValidationError) Error

func (e *ValidationError) Error() string

type Validator

type Validator interface {
	ValidateSchema(ctx context.Context, s *Schema) []*ValidationError
	ValidateValue(ctx context.Context, s *Schema, value interface{}) []*ValidationError
}

Validator 校验 schema 自身以及某个值是否满足 schema。

Jump to

Keyboard shortcuts

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