validator

package
v1.0.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

func DetectFormatFromFilename

func DetectFormatFromFilename(filename string) Format

DetectFormatFromFilename attempts to detect format from filename extension

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    bool   `json:"valid"`
	Format   Format `json:"format"`
	Error    string `json:"error,omitempty"`
	FileName string `json:"filename,omitempty"`
}

Result contains the validation result

func ValidateAuto

func ValidateAuto(data []byte) Result

ValidateAuto validates data with automatic format detection

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(data []byte) Result
	ValidateString(data string) Result
	Format() Format
}

Validator is the main validator interface

func NewValidator

func NewValidator(format Format) (Validator, error)

NewValidator creates a new validator for the specified format

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