validator

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 6 Imported by: 0

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/datavalidator/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, and TOML

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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"
	// FormatAuto represents automatic format detection
	FormatAuto Format = "auto"
	// FormatUnknown represents unknown format
	FormatUnknown Format = "unknown"
)

func DetectFormat

func DetectFormat(data []byte) Format

DetectFormat attempts to detect the data format by analyzing the content. Uses simple heuristics to identify JSON, XML, YAML, or TOML 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

Returns FormatUnknown if the format cannot be determined.

func DetectFormatFromFilename

func DetectFormatFromFilename(filename string) Format

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.

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 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

func ValidateAuto(data []byte) Result

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

func NewValidator(format Format) (Validator, error)

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 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

Jump to

Keyboard shortcuts

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