Categorygithub.com/take0fit/validationcontext
modulepackage
1.0.4
Repository: https://github.com/take0fit/validationcontext.git
Documentation: pkg.go.dev

# README

ValidationContext Library

ValidationContext is a Go library designed to provide a centralized and efficient approach to managing validations in your applications, especially within a Domain-Driven Design (DDD) context. The library is intended to be easy to use, allowing you to collect validation results from various value objects and handle them collectively at a single point in your application.

Key Features

  • Centralized Validation: Collect and manage validation errors from multiple value objects across your application.
  • Deferred Error Handling: Aggregate all validation errors and handle them collectively when necessary.
  • Comprehensive Validation Methods: A wide range of built-in validation methods, including file handling, string formatting, and value range checks.
  • Customizable: Easily extend the library with custom validation logic to fit specific requirements.
  • Enhanced Error Handling: Each validation error now includes a stack trace, and aggregate errors can be retrieved with both error messages and stack traces.

Use Case

In a DDD context, validations are often dispersed across multiple value objects. ValidationContext allows these validations to be aggregated and handled together, ensuring that all potential issues are addressed before proceeding with business logic.

Getting Started

Installation

To install the ValidationContext library, you can use go get:

go get github.com/yourusername/validationcontext

Example Usage: Validating Multiple Value Objects

Consider a scenario where you need to validate several value objects like HairdresserLicenseImage and UserAddress. ValidationContext helps you collect and handle validation results from these objects at once.

package main

import (
	"fmt"
	"os"

	"github.com/yourusername/validationcontext"
)

type LicenseImage struct {
	File *os.File
}

type UserAddress struct {
	City   string
	Street string
}

func NewLicenseImage(file *os.File, vc *validationcontext.ValidationContext) LicenseImage {
	vc.Required(file, "LicenseImage", " license image is required", false)
	vc.ValidateFileExtension(file, "LicenseImage", []string{".png", ".jpg"}, "Invalid file extension")
	vc.ValidateFileSize(file, "LicenseImage", 2*1024*1024, "File size must be 2MB or less")
	return LicenseImage{File: file}
}

func NewUserAddress(street, city string, vc *validationcontext.ValidationContext) UserAddress {
	vc.Required(street, "Street", "Street is required", false)
	vc.Required(city, "City", "City is required", false)
	return UserAddress{Street: street, City: city}
}

func main() {
	vc := validationcontext.NewValidationContext()

	// Validate LicenseImage
	file, _ := os.Open("test.jpg")
	defer file.Close()
	image := NewLicenseImage(file, vc)

	// Validate UserAddress
	address := NewUserAddress("Main St", "New York", vc)

	// Check if any validation errors occurred
	if vc.HasErrors() {
		// Aggregate and handle all errors at once
		err := vc.AggregateError()
		if err != nil {
			// Print the error messages and stack traces
			fmt.Println(err.Error())
			aggregateErr, ok := err.(*validationcontext.ValidationAggregateError)
			if ok {
				fmt.Println("Stack Traces:")
				for _, trace := range aggregateErr.GetStackTraces() {
					fmt.Println(trace)
				}
			}
		}
	} else {
		fmt.Println("Validation passed for:", image.File.Name(), "and", address)
	}
}

Explanation

  • Centralized Validation: The ValidationContext instance (vc) is passed around to each value object, collecting validation errors.
  • Deferred Error Handling: After all validations, vc.HasErrors() checks for any errors. If errors exist, they are aggregated and formatted for handling.
  • Enhanced Error Information: The updated version includes stack traces for each validation error, which can be accessed via the ValidationAggregateError type

Validation Methods

ValidationContext provides a variety of built-in validation methods. Below is a table summarizing the available methods:

Method Description Example Usage

MethodDescriptionExample Usage
RequiredEnsures a value is not empty or nilvc.Required(value, "FieldName", "Field is required", false)
ValidateMinLengthChecks if a string has at least a certain number of charactersvc.ValidateMinLength(value, "FieldName", 5, "Minimum length is 5")
ValidateMaxLengthChecks if a string does not exceed a certain number of charactersvc.ValidateMaxLength(value, "FieldName", 10, "Maximum length is 10")
ValidateEmailValidates if a string is in a proper email formatvc.ValidateEmail(email, "Email", "Invalid email format")
ValidateContainsSpecialEnsures a string contains at least one special charactervc.ValidateContainsSpecial(value, "FieldName", "Must contain a special character")
ValidateContainsNumberEnsures a string contains at least one numeric charactervc.ValidateContainsNumber(value, "FieldName", "Must contain a number")
ValidateContainsUppercaseEnsures a string contains at least one uppercase lettervc.ValidateContainsUppercase(value, "FieldName", "Must contain an uppercase letter")
ValidateContainsLowercaseEnsures a string contains at least one lowercase lettervc.ValidateContainsLowercase(value, "FieldName", "Must contain a lowercase letter")
ValidateURLChecks if a string is a valid URLvc.ValidateURL(value, "FieldName", "Invalid URL format")
ValidateFilePathEnsures the file path is validvc.ValidateFilePath(value, "FilePath", "Invalid file path")
ValidateFileExtensionChecks if a file has a valid extensionvc.ValidateFileExtension(file, "FieldName", []string{".jpg", ".png"}, "")
ValidateFileSizeEnsures the file size is within the specified limitvc.ValidateFileSize(file, "FieldName", 2*1024*1024, "File size must be 2MB or less")
ValidateUUIDChecks if a string is a valid UUIDvc.ValidateUUID(value, "FieldName", "Invalid UUID format")
ValidateMinValueEnsures a numeric value meets the minimum requirementvc.ValidateMinValue(value, "FieldName", 1, "Value must be at least 1")
ValidateMaxValueEnsures a numeric value does not exceed the maximum limitvc.ValidateMaxValue(value, "FieldName", 100, "Value must be 100 or less")
ValidateDateEnsures a string is a valid date in the format "2006-01-02"vc.ValidateDate(value, "FieldName", "Invalid date format")
ValidateYearMonthEnsures a string is a valid year and month in the format "2006-01"vc.ValidateYearMonth(value, "FieldName", "Invalid year-month format")
ValidateYearEnsures a string is a valid yearvc.ValidateYear(value, "FieldName", "Invalid year format")
ValidateMonthEnsures a string is a valid monthvc.ValidateMonth(value, "FieldName", "Invalid month format")
ValidateDateTimeEnsures a string is a valid date and time in the format "2006-01-02 15:04:05"vc.ValidateDateTime(value, "FieldName", "Invalid datetime format")
ValidateTimeEnsures a string is a valid time in the format "15:04"vc.ValidateTime(value, "FieldName", "Invalid time format")

Customizing Validation Logic

ValidationContext is designed to be easily extendable, allowing you to implement custom validation logic that fits your specific needs. This can include additional string checks, complex object validations, or even integrating with external validation libraries.

Conclusion

ValidationContext simplifies the process of managing validations across multiple value objects in your application. It allows you to collect all validation errors in a centralized context and handle them at your convenience, ensuring consistent and comprehensive error management.

This library is ideal for projects that require robust validation mechanisms, particularly in Domain-Driven Design (DDD) contexts where validation logic is scattered across multiple components.

License

This project is licensed under the MIT License. See the LICENSE file for details.

# Functions

NewValidationContext creates and returns a new ValidationContext instance.

# Structs

ValidationAggregateError is a custom error type that aggregates multiple validation errors, including their messages and stack traces.
No description provided by the author
No description provided by the author