blu

package module
v0.1.2-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2023 License: BSD-2-Clause Imports: 5 Imported by: 0

README

blu

codecov Go Report Card

blu is struct value validation based on provided tags.

Note: As of the current version, "blu" is intended for personal use.

Usage

Install blu

go get github.com/n4x2/blu

Then import it:

import "github.com/n4x2/blu"

Define struct with validation tags:

type MyStruct struct {
    FieldA string `validate:"required"`
    // ...
}

Create a new validator instance:

v := blu.NewValidator()

Validate the struct using the created validator:

err := v.Validate(MyStruct)
if err != nil {
    // Handle error
}

Custom Rule

You can add custom validation rules by implementing the Rule interface and registering them with the validator. This allows you to extend the validation capabilities as per your specific requirements.

type CustomRule struct {}

func (r *CustomRule) Name() string {
    return "custom"
}

func (r *CustomRule) Validate(field string, value string, param string) error {
    // Implement your custom validation logic here.
    // Return an error if the validation fails.
    return nil
}

Register the custom rule:

customRule := &CustomRule{}
err := v.RegisterRule(customRule)
if err != nil {
    // Handle error (e.g., duplicated rule name).
}

Use the "custom" rule in your struct's validation tags:

type MyStruct struct {
    FieldA string `validate:"custom"`
    // ...
}

Supported Rules

  • alpha_dash: Validates that the field contains only alpha-numeric characters, underscores, and dashes.
  • alpha_num: Validates that the field contains only alpha-numeric characters.
  • alpha_space: Validates that the field contains only alpha-numeric characters and spaces.
  • alpha: Validates that the field contains only alphabetic characters.
  • enum: Validates that the field's value is within a predefined set of allowed values.
  • max: Validates that the numeric field's value is less than or equal to a specified maximum value.
  • min: Validates that the numeric field's value is greater than or equal to a specified minimum value.
  • number: Validates that the field contains a valid number.
  • numeric: Validates that the field contains only numeric characters.
  • required: Validates that the field is present and not empty.

Documentation

Overview

blu is struct value validation based on tags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorDuplicatedRule

type ErrorDuplicatedRule struct {
	RuleName string
}

ErrorDuplicatedRule an error type for attempting register new rule.

func (*ErrorDuplicatedRule) Error

func (e *ErrorDuplicatedRule) Error() string

Error returns an error message indicating that a rule with the same name already exists.

type ErrorInvalidInput

type ErrorInvalidInput struct {
	Type reflect.Type
}

InvalidaInputError an error type indicating an issue with input validation.

func (*ErrorInvalidInput) Error

func (e *ErrorInvalidInput) Error() string

Error return for invalid input (non-struct).

type ErrorUnexportedField

type ErrorUnexportedField struct {
	Field string
}

ErrorUnexportedField an error type for attempting to validate an unexported field.

func (*ErrorUnexportedField) Error

func (e *ErrorUnexportedField) Error() string

Error returns an error message indicating unexported field (lowercase).

type Field

type Field struct {
	Name  string
	Value string
	Tags  []Tag
}

Field represents struct field with its name, value, and associated tags.

type Rule

type Rule interface {
	// Name return the name of the validation rule.
	Name() string
	// Validate performs validation on the field value with the provided parameters.
	// It returns an error if the validation fails; otherwise, it returns nil.
	Validate(field, value, param string) error
}

Rule represents a validation rule for validating field values.

type Tag

type Tag struct {
	Name  string
	Param string
}

Tag represents a tag with name and parameter.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator contains the validation rules.

func NewValidator

func NewValidator() *Validator

NewValidator returns a new Validator instance.

func (*Validator) RegisterRule

func (v *Validator) RegisterRule(rule Rule) error

RegisterRule registers a new rule into the validator. It returns an error if the rule name is already registered.

func (*Validator) Validate

func (v *Validator) Validate(s interface{}) error

Validate validates the field value of the given struct based on tags. It returns an error if the input is not a struct. It returns an error if struct not exportable. If there are validation errors for the struct fields, it return a joined error that contains all the validation errors. It return nil if validation success.

func (*Validator) ValidateField

func (v *Validator) ValidateField(f Field) error

ValidateField validates the field value based on the provided tags. It returns a joined error that contains all validation errors of the field.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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