Documentation
¶
Overview ¶
Package yamlvalidator validates YAML against native schemas and Go struct tags. Its public API is a facade over the shared internal validation engine.
Index ¶
- Constants
- Variables
- func AppendIndexPath(base string, index int) (string, error)
- func AppendPropertyPath(base, property string) string
- func FormatErrorWithSource(err ValidationError, lines []string) string
- func FormatPathTokens(tokens []PathToken) string
- func Marshal(in any) ([]byte, error)
- func Ptr[T any](v T) *T
- func RenderLineWithCaret(line string, byteCol int) (string, int, int)
- func Unmarshal(data []byte, out any) error
- func ValidateFieldSchema(schema *FieldSchema) error
- type ConditionalRule
- type DefinitionValidator
- type DependencyDetails
- type ErrorCollector
- type ErrorLevel
- type FieldNaming
- type FieldSchema
- func CompileJSONSchema(data []byte) (*FieldSchema, error)
- func CompileJSONSchemaContext(ctx context.Context, data []byte) (*FieldSchema, error)
- func CompileJSONSchemaContextWithOptions(ctx context.Context, data []byte, opts JSONSchemaCompileOptions) (*FieldSchema, error)
- func CompileJSONSchemaWithOptions(data []byte, opts JSONSchemaCompileOptions) (*FieldSchema, error)
- type ItemCountDetails
- type JSONSchemaCachingResolver
- type JSONSchemaCompileOptions
- type JSONSchemaContentEncoding
- type JSONSchemaContentMediaType
- type JSONSchemaDraft
- type JSONSchemaFileResolver
- type JSONSchemaFormat
- type JSONSchemaKeyword
- type JSONSchemaKeywordCompileContext
- type JSONSchemaKeywordCompileContextFunc
- type JSONSchemaKeywordCompileFunc
- type JSONSchemaKeywordFunc
- type JSONSchemaKeywordValidateFunc
- type JSONSchemaKeywordValidationContext
- type JSONSchemaResolver
- type JSONSchemaResolverChain
- type JSONSchemaResolverFunc
- type JSONSchemaResourceMap
- type JSONSchemaSubschema
- type JSONSchemaSubschemaPath
- type JSONSchemaSubschemaPosition
- type JSONSchemaVocabulary
- type KeyValidator
- type KeyValidatorCloner
- type KeyValidatorFactory
- type Limits
- type MarshalOptions
- type NodeType
- type NumericRangeDetails
- type PathToken
- type PathTokenKind
- type Registry
- type RegistryConfig
- type RequiredDetails
- type SchemaError
- type TypeBinding
- type TypeMismatchDetails
- type UnknownKeyDetails
- type UnknownKeyPolicy
- type UnmarshalOptions
- type ValidationContext
- type ValidationError
- type ValidationErrors
- type ValidationResult
- type Validator
- type ValueValidator
- type ValueValidatorCloner
- type ValueValidatorFactory
Constants ¶
const ( // FieldNamingYAML uses yaml.v3's lowercase default names. FieldNamingYAML = engine.FieldNamingYAML // FieldNamingGo preserves Go field names unless a YAML tag names the field. FieldNamingGo = engine.FieldNamingGo )
const JSONSchemaDraft2019 = engine.JSONSchemaDraft2019
const JSONSchemaDraft2020 = engine.JSONSchemaDraft2020
const JSONSchemaDraft4 = engine.JSONSchemaDraft4
const JSONSchemaDraft6 = engine.JSONSchemaDraft6
const JSONSchemaDraft7 = engine.JSONSchemaDraft7
const LevelError = engine.LevelError
LevelError indicates a critical validation failure.
const LevelWarning = engine.LevelWarning
LevelWarning indicates a non-critical issue (e.g., deprecated field, unknown key in permissive mode).
const PathTokenDocument = engine.PathTokenDocument
PathTokenDocument represents the reserved leading doc[N] prefix used for multi-document YAML diagnostics.
const PathTokenIndex = engine.PathTokenIndex
const PathTokenProperty = engine.PathTokenProperty
const TypeAny = engine.TypeAny
TypeAny accepts any type.
const TypeBool = engine.TypeBool
TypeBool represents boolean values.
const TypeFloat = engine.TypeFloat
TypeFloat represents floating-point values (also accepts int).
const TypeInt = engine.TypeInt
TypeInt represents integer values.
const TypeMap = engine.TypeMap
TypeMap represents mapping nodes.
const TypeNull = engine.TypeNull
TypeNull represents null/nil values.
const TypeSequence = engine.TypeSequence
TypeSequence represents sequence/array nodes.
const TypeString = engine.TypeString
TypeString represents string values.
const UnknownKeyError = engine.UnknownKeyError
UnknownKeyError treats unknown keys as errors.
const UnknownKeyIgnore = engine.UnknownKeyIgnore
UnknownKeyIgnore silently ignores unknown keys.
const UnknownKeyInherit = engine.UnknownKeyInherit
UnknownKeyInherit uses ctx.StrictKeys to decide:
StrictKeys=true -> error StrictKeys=false -> warning
const UnknownKeyWarn = engine.UnknownKeyWarn
UnknownKeyWarn treats unknown keys as warnings.
Variables ¶
var ErrJSONSchemaResourceNotFound = engine.ErrJSONSchemaResourceNotFound
ErrJSONSchemaResourceNotFound allows resolver chains to distinguish a miss from an actual resolver failure.
var ErrJSONSchemaResourceOutsideRoot = engine.ErrJSONSchemaResourceOutsideRoot
ErrJSONSchemaResourceOutsideRoot is returned by JSONSchemaFileResolver when a file URL escapes its configured root.
Functions ¶
func AppendIndexPath ¶ added in v0.6.0
AppendIndexPath appends a non-negative sequence index to a stable display path. It returns an error for negative indexes.
func AppendPropertyPath ¶ added in v0.6.0
AppendPropertyPath appends a mapping property to a stable display path. Ambiguous property names are quoted automatically.
func FormatErrorWithSource ¶
func FormatErrorWithSource(err ValidationError, lines []string) string
FormatErrorWithSource formats an error with source context. Correctly handles tabs and Unicode.
func FormatPathTokens ¶ added in v0.4.0
FormatPathTokens formats typed path tokens using the library's stable display syntax.
func Marshal ¶ added in v1.1.0
Marshal encodes and validates a Go value, returning the exact validated bytes.
func Ptr ¶
func Ptr[T any](v T) *T
Ptr returns a pointer to the value. Useful for setting Min/Max fields.
func RenderLineWithCaret ¶
RenderLineWithCaret is an exported wrapper useful for external tests and tools.
func ValidateFieldSchema ¶ added in v0.3.0
func ValidateFieldSchema(schema *FieldSchema) error
ValidateFieldSchema checks FieldSchema invariants, including nested schemas, invalid constraints, non-progressing recursion, and validator definitions.
Types ¶
type ConditionalRule ¶
type ConditionalRule = engine.ConditionalRule
ConditionalRule defines conditional validation logic. When ConditionField equals ConditionValue, additional requirements apply.
type DefinitionValidator ¶ added in v0.3.0
type DefinitionValidator = engine.DefinitionValidator
DefinitionValidator may be implemented by value/key validators that can validate their own configuration during FieldSchema compilation.
type DependencyDetails ¶ added in v0.4.0
type DependencyDetails = engine.DependencyDetails
DependencyDetails describes a field required by another field.
type ErrorCollector ¶
type ErrorCollector = engine.ErrorCollector
ErrorCollector accumulates validation errors and warnings.
func NewErrorCollector ¶
func NewErrorCollector() *ErrorCollector
NewErrorCollector creates a new empty ErrorCollector.
type ErrorLevel ¶
type ErrorLevel = engine.ErrorLevel
ErrorLevel defines the severity of a validation error.
type FieldNaming ¶ added in v1.2.0
type FieldNaming = engine.FieldNaming
FieldNaming selects the default name of exported Go fields in YAML.
type FieldSchema ¶
type FieldSchema = engine.FieldSchema
FieldSchema defines the validation rules for a field.
func CompileJSONSchema ¶ added in v0.3.0
func CompileJSONSchema(data []byte) (*FieldSchema, error)
CompileJSONSchema compiles a JSON Schema using draft 2020-12 as the default dialect. Schemas declaring $schema are compiled using their declared dialect.
func CompileJSONSchemaContext ¶ added in v0.5.0
func CompileJSONSchemaContext(ctx context.Context, data []byte) (*FieldSchema, error)
CompileJSONSchemaContext is the context-aware form of CompileJSONSchema. Context cancellation is observed by library-controlled compilation work and by JSONSchemaResolver implementations.
func CompileJSONSchemaContextWithOptions ¶ added in v0.5.0
func CompileJSONSchemaContextWithOptions( ctx context.Context, data []byte, opts JSONSchemaCompileOptions, ) (*FieldSchema, error)
CompileJSONSchemaContextWithOptions is the context-aware form of CompileJSONSchemaWithOptions.
func CompileJSONSchemaWithOptions ¶ added in v0.3.0
func CompileJSONSchemaWithOptions(data []byte, opts JSONSchemaCompileOptions) (*FieldSchema, error)
CompileJSONSchemaWithOptions compiles a standards-compliant JSON Schema and returns a FieldSchema adapter that validates YAML through the JSON Schema engine while preserving YAML source positions in diagnostics.
type ItemCountDetails ¶ added in v0.4.0
type ItemCountDetails = engine.ItemCountDetails
ItemCountDetails describes a sequence length failure.
type JSONSchemaCachingResolver ¶ added in v0.5.0
type JSONSchemaCachingResolver = engine.JSONSchemaCachingResolver
JSONSchemaCachingResolver caches successful resolutions. Returned byte slices are defensive copies.
func NewJSONSchemaCachingResolver ¶ added in v0.5.0
func NewJSONSchemaCachingResolver(resolver JSONSchemaResolver) *JSONSchemaCachingResolver
NewJSONSchemaCachingResolver wraps resolver with a concurrency-safe cache.
type JSONSchemaCompileOptions ¶ added in v0.3.0
type JSONSchemaCompileOptions = engine.JSONSchemaCompileOptions
JSONSchemaCompileOptions controls JSON Schema compilation and validation.
type JSONSchemaContentEncoding ¶ added in v0.6.0
type JSONSchemaContentEncoding = engine.JSONSchemaContentEncoding
JSONSchemaContentEncoding registers a custom JSON Schema contentEncoding.
type JSONSchemaContentMediaType ¶ added in v0.6.0
type JSONSchemaContentMediaType = engine.JSONSchemaContentMediaType
JSONSchemaContentMediaType registers a custom JSON Schema contentMediaType. UnmarshalJSON is optional and should be set only when the media type contains a JSON-compatible data model that contentSchema may validate.
type JSONSchemaDraft ¶ added in v0.3.0
type JSONSchemaDraft = engine.JSONSchemaDraft
JSONSchemaDraft selects the default JSON Schema dialect used when a schema does not declare $schema. An explicit $schema in the document takes precedence.
type JSONSchemaFileResolver ¶ added in v0.5.0
type JSONSchemaFileResolver = engine.JSONSchemaFileResolver
JSONSchemaFileResolver resolves file:// resources confined to one directory. The root must exist when the resolver is created.
func NewJSONSchemaFileResolver ¶ added in v0.5.0
func NewJSONSchemaFileResolver(root string) (*JSONSchemaFileResolver, error)
NewJSONSchemaFileResolver creates a file resolver constrained to root.
type JSONSchemaFormat ¶ added in v0.3.0
type JSONSchemaFormat = engine.JSONSchemaFormat
JSONSchemaFormat registers a custom JSON Schema format without exposing the underlying JSON Schema engine API.
type JSONSchemaKeyword ¶ added in v0.3.0
type JSONSchemaKeyword = engine.JSONSchemaKeyword
JSONSchemaKeyword defines one custom JSON Schema keyword. Set exactly one of Validate, Compile, or CompileWithContext. Subschemas declares locations of nested schemas relative to the keyword value.
type JSONSchemaKeywordCompileContext ¶ added in v0.5.0
type JSONSchemaKeywordCompileContext = engine.JSONSchemaKeywordCompileContext
JSONSchemaKeywordCompileContext exposes high-level helpers needed by custom keywords which contain nested schemas.
type JSONSchemaKeywordCompileContextFunc ¶ added in v0.5.0
type JSONSchemaKeywordCompileContextFunc = engine.JSONSchemaKeywordCompileContextFunc
JSONSchemaKeywordCompileContextFunc is the advanced high-level compile form for keywords that contain nested schemas or schema references.
type JSONSchemaKeywordCompileFunc ¶ added in v0.3.0
type JSONSchemaKeywordCompileFunc = engine.JSONSchemaKeywordCompileFunc
JSONSchemaKeywordCompileFunc compiles a keyword value from the schema into a runtime validation function. It is useful for parsing/validating keyword configuration once instead of on every validation call.
type JSONSchemaKeywordFunc ¶ added in v0.3.0
type JSONSchemaKeywordFunc = engine.JSONSchemaKeywordFunc
JSONSchemaKeywordFunc is the simple custom-keyword form. The schema keyword value and current JSON instance value are supplied on each validation call.
type JSONSchemaKeywordValidateFunc ¶ added in v0.3.0
type JSONSchemaKeywordValidateFunc = engine.JSONSchemaKeywordValidateFunc
JSONSchemaKeywordValidateFunc validates one JSON instance value.
type JSONSchemaKeywordValidationContext ¶ added in v0.3.0
type JSONSchemaKeywordValidationContext = engine.JSONSchemaKeywordValidationContext
JSONSchemaKeywordValidationContext collects custom keyword diagnostics and evaluation annotations during one keyword validation call.
type JSONSchemaResolver ¶ added in v0.5.0
type JSONSchemaResolver = engine.JSONSchemaResolver
JSONSchemaResolver resolves an absolute JSON Schema retrieval URL. Implementations must be safe for concurrent use if a compile configuration is reused concurrently.
type JSONSchemaResolverChain ¶ added in v0.5.0
type JSONSchemaResolverChain = engine.JSONSchemaResolverChain
JSONSchemaResolverChain tries resolvers in order. Only ErrJSONSchemaResourceNotFound advances to the next resolver. Treat the slice as immutable while it is in use.
type JSONSchemaResolverFunc ¶ added in v0.5.0
type JSONSchemaResolverFunc = engine.JSONSchemaResolverFunc
JSONSchemaResolverFunc adapts a function to JSONSchemaResolver.
type JSONSchemaResourceMap ¶ added in v0.5.0
type JSONSchemaResourceMap = engine.JSONSchemaResourceMap
JSONSchemaResourceMap is an in-memory resolver keyed by absolute retrieval URL. Treat the map as immutable while it is in use; resolved byte slices are copied.
type JSONSchemaSubschema ¶ added in v0.5.0
type JSONSchemaSubschema = engine.JSONSchemaSubschema
JSONSchemaSubschema is a compiled nested schema used by custom keywords. Its implementation is intentionally opaque so callers do not depend on the underlying JSON Schema engine.
type JSONSchemaSubschemaPath ¶ added in v0.5.0
type JSONSchemaSubschemaPath = engine.JSONSchemaSubschemaPath
JSONSchemaSubschemaPath declares a nested schema location relative to a custom keyword value.
type JSONSchemaSubschemaPosition ¶ added in v0.5.0
type JSONSchemaSubschemaPosition = engine.JSONSchemaSubschemaPosition
JSONSchemaSubschemaPosition identifies one possible nested-schema location inside a custom keyword value. Use the constructor functions below.
func JSONSchemaSubschemaAllItems ¶ added in v0.5.0
func JSONSchemaSubschemaAllItems() JSONSchemaSubschemaPosition
JSONSchemaSubschemaAllItems selects all array items.
func JSONSchemaSubschemaAllProperties ¶ added in v0.5.0
func JSONSchemaSubschemaAllProperties() JSONSchemaSubschemaPosition
JSONSchemaSubschemaAllProperties selects all object property values.
func JSONSchemaSubschemaItem ¶ added in v0.5.0
func JSONSchemaSubschemaItem(index int) JSONSchemaSubschemaPosition
JSONSchemaSubschemaItem selects one array item.
func JSONSchemaSubschemaProperty ¶ added in v0.5.0
func JSONSchemaSubschemaProperty(name string) JSONSchemaSubschemaPosition
JSONSchemaSubschemaProperty selects one object property.
type JSONSchemaVocabulary ¶ added in v0.3.0
type JSONSchemaVocabulary = engine.JSONSchemaVocabulary
JSONSchemaVocabulary groups custom keywords under a vocabulary URL. Vocabularies registered through JSONSchemaCompileOptions are activated for that compilation.
type KeyValidator ¶
type KeyValidator = engine.KeyValidator
KeyValidator validates key names in mappings. The same concurrency contract as ValueValidator applies to stateful implementations.
type KeyValidatorCloner ¶ added in v0.4.0
type KeyValidatorCloner = engine.KeyValidatorCloner
KeyValidatorCloner is the key-validator counterpart of ValueValidatorCloner.
type KeyValidatorFactory ¶ added in v1.1.0
type KeyValidatorFactory = engine.KeyValidatorFactory
KeyValidatorFactory builds a key validator from structured tag arguments.
type Limits ¶ added in v1.1.0
Limits bounds high-level validation work. A zero field selects its default.
type MarshalOptions ¶ added in v1.1.0
type MarshalOptions = engine.MarshalOptions
MarshalOptions controls validated YAML encoding. Its zero value enables validation.
type NodeType ¶
NodeType represents the expected YAML node type.
func InferNodeType ¶ added in v0.6.0
func InferNodeType(node *yaml.Node, ctx *ValidationContext) NodeType
InferNodeType applies the same YAML type inference used by FieldSchema validation. A nil context uses default type-inference settings.
type NumericRangeDetails ¶ added in v0.4.0
type NumericRangeDetails = engine.NumericRangeDetails
NumericRangeDetails describes a numeric bound failure.
type PathToken ¶ added in v0.4.0
PathToken is one typed component of ValidationError.Path.
func ParsePathTokens ¶ added in v0.4.0
ParsePathTokens parses the library's display path syntax into typed tokens.
type PathTokenKind ¶ added in v0.4.0
type PathTokenKind = engine.PathTokenKind
PathTokenKind identifies one component of a structured validation path.
type Registry ¶ added in v1.1.0
Registry is an immutable collection of high-level schema extensions.
func NewRegistry ¶ added in v1.1.0
func NewRegistry(config RegistryConfig) (*Registry, error)
NewRegistry snapshots and validates the supplied registry configuration.
type RegistryConfig ¶ added in v1.1.0
type RegistryConfig = engine.RegistryConfig
RegistryConfig declares named validation extensions. The constructor takes a snapshot of its maps, slices, and cloneable validators.
type RequiredDetails ¶ added in v0.4.0
type RequiredDetails = engine.RequiredDetails
RequiredDetails describes a missing required field/property.
type SchemaError ¶ added in v1.1.0
type SchemaError = engine.SchemaError
SchemaError describes an invalid Go type, field, or validation declaration.
type TypeBinding ¶ added in v1.1.0
type TypeBinding = engine.TypeBinding
TypeBinding supplies a schema for a type whose YAML representation cannot be inferred safely. Encode and Decode may differ.
type TypeMismatchDetails ¶ added in v0.4.0
type TypeMismatchDetails = engine.TypeMismatchDetails
TypeMismatchDetails describes a type validation failure.
type UnknownKeyDetails ¶ added in v0.4.0
type UnknownKeyDetails = engine.UnknownKeyDetails
UnknownKeyDetails describes an unknown mapping key and optional suggestion.
type UnknownKeyPolicy ¶
type UnknownKeyPolicy = engine.UnknownKeyPolicy
UnknownKeyPolicy determines how unknown keys in maps are handled.
type UnmarshalOptions ¶ added in v1.1.0
type UnmarshalOptions = engine.UnmarshalOptions
UnmarshalOptions controls validated YAML decoding. Its zero value enables validation.
type ValidationContext ¶
type ValidationContext = engine.ValidationContext
ValidationContext holds configuration and state for a validation run.
func NewValidationContext ¶
func NewValidationContext() *ValidationContext
NewValidationContext creates a new ValidationContext with default settings.
type ValidationError ¶
type ValidationError = engine.ValidationError
ValidationError represents a single validation issue.
type ValidationErrors ¶ added in v1.1.0
type ValidationErrors = engine.ValidationErrors
ValidationErrors groups the final diagnostics from one high-level operation.
type ValidationResult ¶
type ValidationResult = engine.ValidationResult
ValidationResult contains the validation outcome and context for formatting.
type Validator ¶
Validator performs YAML validation against a schema.
func CompileFieldSchema ¶ added in v0.3.0
func CompileFieldSchema(schema *FieldSchema) (*Validator, error)
CompileFieldSchema validates a native FieldSchema before constructing a Validator. New code should prefer this over NewValidator when schemas are assembled dynamically or loaded from configuration.
func NewValidator ¶
func NewValidator(schema *FieldSchema) *Validator
NewValidator creates a Validator that references the supplied schema directly. Callers must not mutate that schema concurrently with validation. Prefer CompileFieldSchema when an immutable, concurrency-safe schema snapshot is desired.
type ValueValidator ¶
type ValueValidator = engine.ValueValidator
ValueValidator validates a node's value. Validator instances may be reused concurrently; custom implementations that keep mutable state must therefore provide their own synchronization.
type ValueValidatorCloner ¶ added in v0.4.0
type ValueValidatorCloner = engine.ValueValidatorCloner
ValueValidatorCloner can be implemented by stateful/configurable custom validators so CompileFieldSchema can snapshot their configuration.
type ValueValidatorFactory ¶ added in v1.1.0
type ValueValidatorFactory = engine.ValueValidatorFactory
ValueValidatorFactory builds a validator from structured tag arguments. Numeric arguments are json.Number values, preserving their exact spelling.
Directories
¶
| Path | Synopsis |
|---|---|
|
benchmarks
|
|
|
cmd
|
|
|
yamlvalidator
command
|
|
|
yamlvalidator-gen
command
Command yamlvalidator-gen generates typed YAML node codecs for selected Go types.
|
Command yamlvalidator-gen generates typed YAML node codecs for selected Go types. |
|
examples
|
|
|
advanced
command
|
|
|
basic
command
Example demonstrates how to use the yamlvalidator package to validate Kubernetes-like manifests with comprehensive schema definitions.
|
Example demonstrates how to use the yamlvalidator package to validate Kubernetes-like manifests with comprehensive schema definitions. |
|
codegen
command
|
|
|
highlevel
command
|
|
|
tagrules
Package tagrules demonstrates native cross-field validation declarations.
|
Package tagrules demonstrates native cross-field validation declarations. |
|
Package genruntime contains the runtime ABI used by yamlvalidator-gen output.
|
Package genruntime contains the runtime ABI used by yamlvalidator-gen output. |
|
spec
Package spec defines the generated schema blueprints shared by yamlvalidator-gen and the private native schema materializer.
|
Package spec defines the generated schema blueprints shared by yamlvalidator-gen and the private native schema materializer. |
|
internal
|
|
|
checks
Package checks contains calculation primitives shared by the native validators and the high-level tag adapters.
|
Package checks contains calculation primitives shared by the native validators and the high-level tag adapters. |
|
engine
Package yamlvalidator provides a flexible YAML validation library with support for type checking, custom validators, conditional logic, and detailed error reporting with source context.
|
Package yamlvalidator provides a flexible YAML validation library with support for type checking, custom validators, conditional logic, and detailed error reporting with source context. |
|
schemacompiler
Package schemacompiler is the generation-time entry to native tag lowering.
|
Package schemacompiler is the generation-time entry to native tag lowering. |
|
thirdparty/xnet/idna
Package idna implements IDNA2008 using the compatibility processing defined by UTS (Unicode Technical Standard) #46, which defines a standard to deal with the transition from IDNA2003.
|
Package idna implements IDNA2008 using the compatibility processing defined by UTS (Unicode Technical Standard) #46, which defines a standard to deal with the transition from IDNA2003. |
|
thirdparty/xtext/secure/bidirule
Package bidirule implements the Bidi Rule defined by RFC 5893.
|
Package bidirule implements the Bidi Rule defined by RFC 5893. |
|
thirdparty/xtext/transform
Package transform provides reader and writer wrappers that transform the bytes passing through as well as various transformations.
|
Package transform provides reader and writer wrappers that transform the bytes passing through as well as various transformations. |
|
thirdparty/xtext/unicode/bidi
Package bidi contains functionality for bidirectional text support.
|
Package bidi contains functionality for bidirectional text support. |
|
thirdparty/xtext/unicode/norm
Package norm contains types and functions for normalizing Unicode strings.
|
Package norm contains types and functions for normalizing Unicode strings. |
|
pkg
|
|
|
skills
|
|
|
go-yamlvalidator/examples/native
command
|