schema

package
v0.8.69 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package schema validates Meshery documents against the embedded schemas published by github.com/meshery/schemas.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidRegistrationCode  = "meshkit-11320"
	ErrDetectSchemaVersionCode  = "meshkit-11321"
	ErrResolveSchemaCode        = "meshkit-11322"
	ErrDecodeDocumentCode       = "meshkit-11323"
	ErrCompileSchemaCode        = "meshkit-11324"
	ErrValidateDocumentCode     = "meshkit-11325"
	ErrDecodeTypedDocumentCode  = "meshkit-11326"
	ErrUnmarshalSchemaAssetCode = "meshkit-11327"
)
View Source
var RegexpMatchStringErrorf = log.Printf

RegexpMatchStringErrorf is called when regexp2 pattern matching fails at runtime (e.g. due to a match timeout). It defaults to log.Printf. Library consumers may replace it with their own structured logger before the Validator is used, for example:

schema.RegexpMatchStringErrorf = myLogger.Errorf

Functions

func DecodeAndValidate

func DecodeAndValidate[T any](data []byte) (T, error)

DecodeAndValidate validates the supplied document after auto-detecting its schema and then decodes it into T.

func DecodeAndValidateWithRef

func DecodeAndValidateWithRef[T any](ref Ref, data []byte) (T, error)

DecodeAndValidateWithRef validates the supplied document against the explicitly identified schema and then decodes it into T.

func DecodeAndValidateWithValidator

func DecodeAndValidateWithValidator[T any](validator *Validator, ref Ref, data []byte) (T, error)

DecodeAndValidateWithValidator validates the supplied document using the provided validator and then decodes it into T.

func ErrCompileSchema

func ErrCompileSchema(location string, err error) error

func ErrDecodeDocument

func ErrDecodeDocument(err error) error

func ErrDecodeTypedDocument

func ErrDecodeTypedDocument(err error) error

func ErrDetectSchemaVersion

func ErrDetectSchemaVersion() error

func ErrInvalidRegistration

func ErrInvalidRegistration(err error) error

func ErrResolveSchema

func ErrResolveSchema(ref Ref) error

func ErrUnmarshalSchemaAsset

func ErrUnmarshalSchemaAsset(assetPath string, err error) error

func ErrValidateDocument

func ErrValidateDocument(details ValidationDetails) error

func Validate

func Validate(data []byte) error

Validate validates the supplied document after auto-detecting the schema from schemaVersion.

func ValidateAs

func ValidateAs(documentType DocumentType, data []byte) error

ValidateAs validates the supplied document against the schema registered for the given document type.

func ValidateWithRef

func ValidateWithRef(ref Ref, data []byte) error

ValidateWithRef validates the supplied document against the explicitly identified schema.

Types

type DocumentType

type DocumentType string

DocumentType identifies a Meshery document family.

const (
	TypeComponent    DocumentType = "component"
	TypeConnection   DocumentType = "connection"
	TypeDesign       DocumentType = "design"
	TypeEnvironment  DocumentType = "environment"
	TypeModel        DocumentType = "model"
	TypeRelationship DocumentType = "relationship"
)

type Ref

type Ref struct {
	SchemaVersion string       `json:"schemaVersion,omitempty" yaml:"schemaVersion,omitempty"`
	Type          DocumentType `json:"type,omitempty" yaml:"type,omitempty"`
}

Ref identifies which schema should be used to validate a document.

func DetectRef

func DetectRef(data []byte) (Ref, error)

DetectRef reads the schemaVersion from the supplied document and returns the corresponding reference.

func (Ref) IsZero

func (r Ref) IsZero() bool

type Registration

type Registration struct {
	Ref      Ref
	Location string
	// AssetVersion is the schema asset version segment derived from the embedded
	// schemas path (for example, "v1beta1"). It is used to resolve schemaVersion
	// strings even when the underlying schema asset does not embed a literal
	// schemaVersion constant/default.
	AssetVersion string
}

Registration associates a schema reference with an embedded schema location.

type ValidationDetails

type ValidationDetails struct {
	Ref            Ref         `json:"ref"`
	SchemaLocation string      `json:"schemaLocation"`
	Violations     []Violation `json:"violations,omitempty"`
}

ValidationDetails contains field-level validation failures for a rejected document.

func ValidationDetailsFromError

func ValidationDetailsFromError(err error) (ValidationDetails, bool)

ValidationDetailsFromError extracts structured validation details from a MeshKit validation error.

type Validator

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

Validator validates Meshery documents against embedded schemas from github.com/meshery/schemas.

func Default

func Default() *Validator

Default returns the process-wide validator backed by the embedded schemas module.

func MustNew

func MustNew() *Validator

MustNew returns a validator preloaded with the built-in Meshery schema registrations. It panics if any builtin registration fails.

func New

func New() (*Validator, error)

New returns a validator preloaded with the built-in Meshery schema registrations.

func (*Validator) Register

func (v *Validator) Register(registration Registration) error

func (*Validator) Validate

func (v *Validator) Validate(data []byte) error

Validate validates the supplied document after auto-detecting the schema from schemaVersion. The document bytes are decoded exactly once; the resulting value is reused for both schema-version detection and validation, avoiding a redundant unmarshal.

func (*Validator) ValidateAny

func (v *Validator) ValidateAny(ref Ref, value any) error

ValidateAny validates the supplied value against the explicitly identified schema.

func (*Validator) ValidateBytes

func (v *Validator) ValidateBytes(ref Ref, data []byte) error

ValidateBytes validates the supplied document against the explicitly identified schema.

type Violation

type Violation struct {
	InstancePath string `json:"instancePath"`
	SchemaPath   string `json:"schemaPath"`
	Keyword      string `json:"keyword"`
	Message      string `json:"message"`
}

Violation is a field-level validation failure reported by the underlying schema validator.

Jump to

Keyboard shortcuts

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