Documentation
¶
Overview ¶
Package validator provides a privacy-focused data format validation library for Go.
It supports validation of JSON, YAML, XML, and TOML formats with automatic format detection.
Features:
- Zero dependencies for core validation logic
- No logging or data retention
- Format auto-detection
- Simple, clean API
- Thread-safe validators
Basic usage:
import "github.com/akhilesharora/serdeval/pkg/validator"
// Validate with explicit format
v, _ := validator.NewValidator(validator.FormatJSON)
result := v.ValidateString(`{"test": true}`)
if result.Valid {
fmt.Println("Valid JSON!")
}
// Validate with auto-detection
result := validator.ValidateAuto([]byte(data))
fmt.Printf("Format: %s, Valid: %v\n", result.Format, result.Valid)
Advanced usage:
// Create reusable validators
jsonValidator, _ := validator.NewValidator(validator.FormatJSON)
yamlValidator, _ := validator.NewValidator(validator.FormatYAML)
// Validate multiple files
for _, file := range files {
data, _ := os.ReadFile(file)
format := validator.DetectFormatFromFilename(file)
v, _ := validator.NewValidator(format)
result := v.Validate(data)
if !result.Valid {
log.Printf("%s: %s", file, result.Error)
}
}
Privacy guarantee:
- No network connections
- No temporary files
- No logging of validated data
- All validation happens in-memory
Package validator provides data format validation for JSON, YAML, XML, TOML, CSV, GraphQL, INI, HCL, Protobuf text format, Markdown, JSON Lines, Jupyter Notebooks, Requirements.txt, and Dockerfile
Index ¶
- type CSVValidator
- type DockerfileValidator
- type Format
- type GraphQLValidator
- type HCLValidator
- type INIValidator
- type JSONLValidator
- type JSONValidator
- type JupyterValidator
- type MarkdownValidator
- type ProtobufValidator
- type RequirementsValidator
- type Result
- type TOMLValidator
- type Validator
- type XMLValidator
- type YAMLValidator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CSVValidator ¶ added in v0.2.0
type CSVValidator struct {
// contains filtered or unexported fields
}
CSVValidator validates CSV data
func (CSVValidator) Format ¶ added in v0.2.0
func (v CSVValidator) Format() Format
Format returns the validator's format
func (*CSVValidator) Validate ¶ added in v0.2.0
func (v *CSVValidator) Validate(data []byte) Result
Validate validates CSV data
func (*CSVValidator) ValidateString ¶ added in v0.2.0
func (v *CSVValidator) ValidateString(data string) Result
ValidateString validates CSV string
type DockerfileValidator ¶ added in v0.2.0
type DockerfileValidator struct {
// contains filtered or unexported fields
}
DockerfileValidator validates Dockerfile data
func (DockerfileValidator) Format ¶ added in v0.2.0
func (v DockerfileValidator) Format() Format
Format returns the validator's format
func (*DockerfileValidator) Validate ¶ added in v0.2.0
func (v *DockerfileValidator) Validate(data []byte) Result
Validate validates Dockerfile data
func (*DockerfileValidator) ValidateString ¶ added in v0.2.0
func (v *DockerfileValidator) ValidateString(data string) Result
ValidateString validates Dockerfile string
type Format ¶
type Format string
Format represents a data format type
const ( // FormatJSON represents JSON format FormatJSON Format = "json" // FormatYAML represents YAML format FormatYAML Format = "yaml" // FormatXML represents XML format FormatXML Format = "xml" // FormatTOML represents TOML format FormatTOML Format = "toml" // FormatCSV represents CSV format FormatCSV Format = "csv" // FormatGraphQL represents GraphQL query/schema format FormatGraphQL Format = "graphql" // FormatINI represents INI configuration format FormatINI Format = "ini" // FormatHCL represents HCL (HashiCorp Configuration Language) format FormatHCL Format = "hcl" // FormatProtobuf represents Protobuf text format FormatProtobuf Format = "protobuf" // FormatMarkdown represents Markdown format FormatMarkdown Format = "markdown" // FormatJSONL represents JSON Lines format (newline-delimited JSON) FormatJSONL Format = "jsonl" // FormatJupyter represents Jupyter Notebook format FormatJupyter Format = "jupyter" // FormatRequirements represents Requirements.txt format FormatRequirements Format = "requirements" // FormatDockerfile represents Dockerfile format FormatDockerfile Format = "dockerfile" // FormatAuto represents automatic format detection FormatAuto Format = "auto" // FormatUnknown represents unknown format FormatUnknown Format = "unknown" )
func DetectFormat ¶
DetectFormat attempts to detect the data format by analyzing the content. Uses simple heuristics to identify various data formats.
Detection rules:
- JSON: Starts with '{' or '['
- XML: Starts with '<?xml' or '<'
- YAML: Contains '---' or has key:value pattern
- TOML: Contains '[section]' pattern with key=value pairs
- CSV: Has comma-separated values with consistent columns
- GraphQL: Contains query/mutation/type/schema keywords
- INI: Has [section] headers or key=value pairs
- Dockerfile: Starts with FROM instruction
- Markdown: Contains markdown syntax like #, *, -, ```
- Requirements.txt: Contains package names with version specifiers
Returns FormatUnknown if the format cannot be determined.
func DetectFormatFromFilename ¶
DetectFormatFromFilename attempts to detect format from filename extension.
Supported extensions:
- .json → FormatJSON
- .yaml, .yml → FormatYAML
- .xml → FormatXML
- .toml → FormatTOML
Example:
format := DetectFormatFromFilename("config.json")
// format == FormatJSON
Returns FormatUnknown if the extension is not recognized.
func DetectFormatParallel ¶ added in v0.2.1
DetectFormatParallel attempts to detect the data format using parallel detection. Uses GOMAXPROCS goroutines to check different format families concurrently. This is mainly useful for educational purposes as the overhead usually exceeds benefits.
type GraphQLValidator ¶ added in v0.2.0
type GraphQLValidator struct {
// contains filtered or unexported fields
}
GraphQLValidator validates GraphQL queries and schemas
func (GraphQLValidator) Format ¶ added in v0.2.0
func (v GraphQLValidator) Format() Format
Format returns the validator's format
func (*GraphQLValidator) Validate ¶ added in v0.2.0
func (v *GraphQLValidator) Validate(data []byte) Result
Validate validates GraphQL queries and schemas
func (*GraphQLValidator) ValidateString ¶ added in v0.2.0
func (v *GraphQLValidator) ValidateString(data string) Result
ValidateString validates GraphQL string
type HCLValidator ¶ added in v0.2.0
type HCLValidator struct {
// contains filtered or unexported fields
}
HCLValidator validates HCL configuration data
func (HCLValidator) Format ¶ added in v0.2.0
func (v HCLValidator) Format() Format
Format returns the validator's format
func (*HCLValidator) Validate ¶ added in v0.2.0
func (v *HCLValidator) Validate(data []byte) Result
Validate validates HCL configuration data
func (*HCLValidator) ValidateString ¶ added in v0.2.0
func (v *HCLValidator) ValidateString(data string) Result
ValidateString validates HCL string
type INIValidator ¶ added in v0.2.0
type INIValidator struct {
// contains filtered or unexported fields
}
INIValidator validates INI configuration data
func (INIValidator) Format ¶ added in v0.2.0
func (v INIValidator) Format() Format
Format returns the validator's format
func (*INIValidator) Validate ¶ added in v0.2.0
func (v *INIValidator) Validate(data []byte) Result
Validate validates INI configuration data
func (*INIValidator) ValidateString ¶ added in v0.2.0
func (v *INIValidator) ValidateString(data string) Result
ValidateString validates INI string
type JSONLValidator ¶ added in v0.2.0
type JSONLValidator struct {
// contains filtered or unexported fields
}
JSONLValidator validates JSON Lines data
func (JSONLValidator) Format ¶ added in v0.2.0
func (v JSONLValidator) Format() Format
Format returns the validator's format
func (*JSONLValidator) Validate ¶ added in v0.2.0
func (v *JSONLValidator) Validate(data []byte) Result
Validate validates JSON Lines data (each line must be valid JSON)
func (*JSONLValidator) ValidateString ¶ added in v0.2.0
func (v *JSONLValidator) ValidateString(data string) Result
ValidateString validates JSON Lines string
type JSONValidator ¶
type JSONValidator struct {
// contains filtered or unexported fields
}
JSONValidator validates JSON data
func (JSONValidator) Format ¶
func (v JSONValidator) Format() Format
Format returns the validator's format
func (*JSONValidator) Validate ¶
func (v *JSONValidator) Validate(data []byte) Result
Validate validates JSON data
func (*JSONValidator) ValidateString ¶
func (v *JSONValidator) ValidateString(data string) Result
ValidateString validates JSON string
type JupyterValidator ¶ added in v0.2.0
type JupyterValidator struct {
// contains filtered or unexported fields
}
JupyterValidator validates Jupyter Notebook data
func (JupyterValidator) Format ¶ added in v0.2.0
func (v JupyterValidator) Format() Format
Format returns the validator's format
func (*JupyterValidator) Validate ¶ added in v0.2.0
func (v *JupyterValidator) Validate(data []byte) Result
Validate validates Jupyter Notebook data (must be valid JSON with notebook structure)
func (*JupyterValidator) ValidateString ¶ added in v0.2.0
func (v *JupyterValidator) ValidateString(data string) Result
ValidateString validates Jupyter Notebook string
type MarkdownValidator ¶ added in v0.2.0
type MarkdownValidator struct {
// contains filtered or unexported fields
}
MarkdownValidator validates Markdown data
func (MarkdownValidator) Format ¶ added in v0.2.0
func (v MarkdownValidator) Format() Format
Format returns the validator's format
func (*MarkdownValidator) Validate ¶ added in v0.2.0
func (v *MarkdownValidator) Validate(data []byte) Result
Validate validates Markdown data
func (*MarkdownValidator) ValidateString ¶ added in v0.2.0
func (v *MarkdownValidator) ValidateString(data string) Result
ValidateString validates Markdown string
type ProtobufValidator ¶ added in v0.2.0
type ProtobufValidator struct {
// contains filtered or unexported fields
}
ProtobufValidator validates Protobuf text format data
func (ProtobufValidator) Format ¶ added in v0.2.0
func (v ProtobufValidator) Format() Format
Format returns the validator's format
func (*ProtobufValidator) Validate ¶ added in v0.2.0
func (v *ProtobufValidator) Validate(data []byte) Result
Validate validates Protobuf text format data
func (*ProtobufValidator) ValidateString ¶ added in v0.2.0
func (v *ProtobufValidator) ValidateString(data string) Result
ValidateString validates Protobuf text format string
type RequirementsValidator ¶ added in v0.2.0
type RequirementsValidator struct {
// contains filtered or unexported fields
}
RequirementsValidator validates Requirements.txt data
func (RequirementsValidator) Format ¶ added in v0.2.0
func (v RequirementsValidator) Format() Format
Format returns the validator's format
func (*RequirementsValidator) Validate ¶ added in v0.2.0
func (v *RequirementsValidator) Validate(data []byte) Result
Validate validates Requirements.txt data
func (*RequirementsValidator) ValidateString ¶ added in v0.2.0
func (v *RequirementsValidator) ValidateString(data string) Result
ValidateString validates Requirements.txt string
type Result ¶
type Result struct {
// Valid indicates whether the data is valid for the detected/specified format
Valid bool `json:"valid"`
// Format indicates the data format that was validated
Format Format `json:"format"`
// Error contains the validation error message if Valid is false
Error string `json:"error,omitempty"`
// FileName is an optional field to track which file was validated
FileName string `json:"filename,omitempty"`
}
Result contains the validation result for a data format validation operation.
func ValidateAuto ¶
ValidateAuto validates data with automatic format detection. It first attempts to detect the format, then validates using the appropriate validator.
Example:
data := []byte(`{"name": "test"}`)
result := ValidateAuto(data)
fmt.Printf("Format: %s, Valid: %v\n", result.Format, result.Valid)
// Output: Format: json, Valid: true
Returns a Result with Format=FormatUnknown if the format cannot be detected.
type TOMLValidator ¶
type TOMLValidator struct {
// contains filtered or unexported fields
}
TOMLValidator validates TOML data
func (TOMLValidator) Format ¶
func (v TOMLValidator) Format() Format
Format returns the validator's format
func (*TOMLValidator) Validate ¶
func (v *TOMLValidator) Validate(data []byte) Result
Validate validates TOML data
func (*TOMLValidator) ValidateString ¶
func (v *TOMLValidator) ValidateString(data string) Result
ValidateString validates TOML string
type Validator ¶
type Validator interface {
// Validate checks if the provided byte slice is valid for this validator's format.
// Returns a Result containing validation status and any error messages.
Validate(data []byte) Result
// ValidateString is a convenience method that validates a string.
// Internally converts the string to []byte and calls Validate.
ValidateString(data string) Result
// Format returns the data format this validator is configured for.
Format() Format
}
Validator is the main validator interface for validating data formats. Each validator is specific to a single format (JSON, YAML, XML, or TOML).
func NewValidator ¶
NewValidator creates a new validator for the specified format.
Example:
validator, err := NewValidator(FormatJSON)
if err != nil {
log.Fatal(err)
}
result := validator.ValidateString(`{"key": "value"}`)
if result.Valid {
fmt.Println("Valid JSON!")
}
Supported formats: FormatJSON, FormatYAML, FormatXML, FormatTOML, FormatCSV, FormatGraphQL, FormatINI, FormatHCL, FormatProtobuf, FormatMarkdown, FormatJSONL, FormatJupyter, FormatRequirements, FormatDockerfile Returns an error if an unsupported format is specified.
type XMLValidator ¶
type XMLValidator struct {
// contains filtered or unexported fields
}
XMLValidator validates XML data
func (XMLValidator) Format ¶
func (v XMLValidator) Format() Format
Format returns the validator's format
func (*XMLValidator) Validate ¶
func (v *XMLValidator) Validate(data []byte) Result
Validate validates XML data
func (*XMLValidator) ValidateString ¶
func (v *XMLValidator) ValidateString(data string) Result
ValidateString validates XML string
type YAMLValidator ¶
type YAMLValidator struct {
// contains filtered or unexported fields
}
YAMLValidator validates YAML data
func (YAMLValidator) Format ¶
func (v YAMLValidator) Format() Format
Format returns the validator's format
func (*YAMLValidator) Validate ¶
func (v *YAMLValidator) Validate(data []byte) Result
Validate validates YAML data
func (*YAMLValidator) ValidateString ¶
func (v *YAMLValidator) ValidateString(data string) Result
ValidateString validates YAML string