example_validation

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 3 Imported by: 0

README

Validation Package Examples

This directory contains examples demonstrating how to use the validation package, which provides utilities for validating data in Go applications. The package offers a structured way to perform validations, collect validation errors, and report them in a consistent format.

Examples

1. Basic Validation Example

basic_validation_example.go

Demonstrates basic validation functions for strings.

Key concepts:

  • Creating a validation result
  • Using Required to validate that a value is not empty
  • Using MinLength and MaxLength to validate string length
  • Using Pattern to validate that a string matches a regular expression
  • Checking if validation passed with IsValid()
2. Date Validation Example

date_validation_example.go

Shows how to validate dates and date ranges.

Key concepts:

  • Using PastDate to validate that a date is in the past
  • Using ValidDateRange to validate that a start date is before an end date
  • Handling validation errors for dates
3. Collection Validation Example

collection_validation_example.go

Demonstrates validation of collections (slices).

Key concepts:

  • Using AllTrue to check if all items in a collection satisfy a condition
  • Using ValidateAll to validate each item in a collection individually
  • Handling validation errors for collections
  • Validating different types of collections (strings, numbers)
4. Custom Validation Example

custom_validation_example.go

Shows how to create custom validation for a struct.

Key concepts:

  • Creating a validation function for a custom struct
  • Combining multiple validators for complex validation rules
  • Using ValidateID for ID validation
  • Adding custom validation logic
  • Returning validation errors as a standard error

Running the Examples

To run any of the examples, use the go run command:

go run examples/validation/basic_validation_example.go

Additional Resources

For more information about the validation package, see the validation package documentation.

Documentation

Overview

Example of basic validation using the validation package

Example of collection validation using the validation package

Example of custom validation using the validation package

Example of date validation using the validation package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateUser

func ValidateUser(user User) error

ValidateUser validates a user

Types

type User

type User struct {
	ID       string
	Username string
	Email    string
	Age      int
	Role     string
}

User represents a user in the system

Jump to

Keyboard shortcuts

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