Documentation
¶
Overview ¶
Package validation provides proto field validation rule parsing and code generation.
It implements a system inspired by protoc-gen-validate (PGV) as a native Buffalo feature. Users annotate proto fields with [(buffalo.validate.rules)...] options, and Buffalo parses these annotations to generate Validate() methods for each target language.
Index ¶
- func DefaultValidateConfig() plugin.Config
- func NewValidatePlugin() plugin.Plugin
- type CodeGenerator
- type CppCodeGenerator
- type FieldRule
- type GeneratedFile
- type GoCodeGenerator
- type MessageRules
- type PythonCodeGenerator
- type RuleType
- type RustCodeGenerator
- type ValidatePlugin
- func (p *ValidatePlugin) Description() string
- func (p *ValidatePlugin) Execute(ctx context.Context, input *plugin.Input) (*plugin.Output, error)
- func (p *ValidatePlugin) Init(config plugin.Config) error
- func (p *ValidatePlugin) Name() string
- func (p *ValidatePlugin) Shutdown() error
- func (p *ValidatePlugin) Type() plugin.PluginType
- func (p *ValidatePlugin) ValidationRules() []string
- func (p *ValidatePlugin) Version() string
- type ValidationResult
- type Violation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultValidateConfig ¶
DefaultValidateConfig returns the default plugin configuration.
func NewValidatePlugin ¶
NewValidatePlugin creates the built-in buffalo-validate plugin.
Types ¶
type CodeGenerator ¶
type CodeGenerator interface {
// Language returns the target language identifier.
Language() string
// Generate produces validation code for the given messages.
Generate(messages []MessageRules) ([]GeneratedFile, error)
}
CodeGenerator generates validation source code for a target language.
func NewCodeGenerator ¶
func NewCodeGenerator(language string) (CodeGenerator, error)
NewCodeGenerator returns a CodeGenerator for the given language.
type CppCodeGenerator ¶
type CppCodeGenerator struct{}
CppCodeGenerator generates C++ validation functions as a header + source pair.
func (*CppCodeGenerator) Generate ¶
func (g *CppCodeGenerator) Generate(messages []MessageRules) ([]GeneratedFile, error)
func (*CppCodeGenerator) Language ¶
func (g *CppCodeGenerator) Language() string
type FieldRule ¶
type FieldRule struct {
Type RuleType // Kind of rule
Value interface{} // Rule parameter (threshold, pattern, list, etc.)
FieldName string // Proto field name this applies to
FieldType string // Proto field type (string, int32, double, etc.)
Message string // Custom error message override
}
FieldRule represents a single parsed validation rule attached to a field.
func ParseFieldAnnotation ¶
ParseFieldAnnotation parses all buffalo.validate annotations from a field's option string and returns the list of parsed rules.
annotation is the full [...] option portion of a proto field line. fieldName / fieldType are used to tag each returned FieldRule.
type GeneratedFile ¶
type GeneratedFile struct {
Path string // Relative output path
Content string // Generated source code
}
GeneratedFile represents a single generated source file.
type GoCodeGenerator ¶
type GoCodeGenerator struct{}
GoCodeGenerator generates Go Validate() methods.
func (*GoCodeGenerator) Generate ¶
func (g *GoCodeGenerator) Generate(messages []MessageRules) ([]GeneratedFile, error)
func (*GoCodeGenerator) Language ¶
func (g *GoCodeGenerator) Language() string
type MessageRules ¶
type MessageRules struct {
MessageName string // Proto message name
Package string // Proto package
FilePath string // Source .proto file path
Disabled bool // If true, skip validation for this message
Fields map[string][]FieldRule // field_name -> list of rules
}
MessageRules is a collection of validation rules for a single message.
func ExtractValidationRules ¶
func ExtractValidationRules(content, filePath string) ([]MessageRules, error)
ExtractValidationRules scans a full proto file text and returns MessageRules for every message that has at least one buffalo.validate annotation.
type PythonCodeGenerator ¶
type PythonCodeGenerator struct{}
PythonCodeGenerator generates Python validate_* functions.
func (*PythonCodeGenerator) Generate ¶
func (g *PythonCodeGenerator) Generate(messages []MessageRules) ([]GeneratedFile, error)
func (*PythonCodeGenerator) Language ¶
func (g *PythonCodeGenerator) Language() string
type RuleType ¶
type RuleType string
RuleType identifies a specific kind of validation rule.
const ( RuleRequired RuleType = "required" RuleGt RuleType = "gt" RuleGte RuleType = "gte" RuleLt RuleType = "lt" RuleLte RuleType = "lte" RuleIn RuleType = "in" RuleNotIn RuleType = "not_in" RuleConst RuleType = "const" RuleMinLen RuleType = "min_len" RuleMaxLen RuleType = "max_len" RulePattern RuleType = "pattern" RuleEmail RuleType = "email" RuleURI RuleType = "uri" RuleUUID RuleType = "uuid" RuleNotEmpty RuleType = "not_empty" RulePrefix RuleType = "prefix" RuleSuffix RuleType = "suffix" RuleContains RuleType = "contains" RuleIP RuleType = "ip" RuleIPv4 RuleType = "ipv4" RuleIPv6 RuleType = "ipv6" RuleHostname RuleType = "hostname" RuleMinItems RuleType = "min_items" RuleMaxItems RuleType = "max_items" RuleUnique RuleType = "unique" RuleMinPairs RuleType = "min_pairs" RuleMaxPairs RuleType = "max_pairs" RuleGtNow RuleType = "gt_now" RuleLtNow RuleType = "lt_now" )
type RustCodeGenerator ¶
type RustCodeGenerator struct{}
RustCodeGenerator generates Rust Validate trait implementations.
func (*RustCodeGenerator) Generate ¶
func (g *RustCodeGenerator) Generate(messages []MessageRules) ([]GeneratedFile, error)
func (*RustCodeGenerator) Language ¶
func (g *RustCodeGenerator) Language() string
type ValidatePlugin ¶
type ValidatePlugin struct {
// contains filtered or unexported fields
}
ValidatePlugin is a Buffalo plugin that parses proto field annotations ([(buffalo.validate.rules)...]) and generates Validate() methods for each target language.
func (*ValidatePlugin) Description ¶
func (p *ValidatePlugin) Description() string
func (*ValidatePlugin) Name ¶
func (p *ValidatePlugin) Name() string
func (*ValidatePlugin) Shutdown ¶
func (p *ValidatePlugin) Shutdown() error
func (*ValidatePlugin) Type ¶
func (p *ValidatePlugin) Type() plugin.PluginType
func (*ValidatePlugin) ValidationRules ¶
func (p *ValidatePlugin) ValidationRules() []string
ValidationRules returns the list of rule types this plugin checks.
func (*ValidatePlugin) Version ¶
func (p *ValidatePlugin) Version() string
type ValidationResult ¶
ValidationResult contains all violations for a validated message instance.
func (*ValidationResult) Error ¶
func (r *ValidationResult) Error() string
Error returns a combined error string or empty string if valid.
func (*ValidationResult) IsValid ¶
func (r *ValidationResult) IsValid() bool
IsValid returns true if no violations were found.