Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mbretter committed Oct 26, 2024
1 parent 5d7fc11 commit 67f16b9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Sanitizer struct {
backend *mold.Transformer
}

// NewSanitizer creates a new instance of Sanitizer with a background context and a default transformer backend.
func NewSanitizer() Sanitizer {
be := modifiers.New()
return Sanitizer{
Expand All @@ -19,6 +20,7 @@ func NewSanitizer() Sanitizer {
}
}

// Struct sanitizes the given struct based on the rules defined in its fields' tags.
func (s Sanitizer) Struct(val any) error {
err := s.backend.Struct(s.ctx, val)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package validation is basically a wrapper around https://github.com/go-playground/validator and https://github.com/go-playground/mold.
//
// Besides the functionality of the go-playground packages it supports a more flexible error translation by using struct tags for error messages.
//
// The error message is set into the `errors` tag of the struct field, the key is the validator as provided in the `validate`
// struct tag. Multiple validators/errors are supported.
package validation

import (
Expand All @@ -20,6 +26,7 @@ type Validator struct {

var regexIndex = regexp.MustCompile(`(\[(\d+)])$`)

// NewValidator initializes and returns a new Validator instance with default configurations and custom validations.
func NewValidator() Validator {
be := validator.New(validator.WithRequiredStructEnabled())
_ = be.RegisterValidation("dateString", validateDateString)
Expand All @@ -40,10 +47,12 @@ func NewValidator() Validator {
}
}

// RegisterCustomTypeFunc registers a custom validation function for specific types with the validator backend.
func (v Validator) RegisterCustomTypeFunc(fn validator.CustomTypeFunc, types ...interface{}) {
v.backend.RegisterCustomTypeFunc(fn, types...)
}

// Var validates a single variable using the specified validation tag and returns a list of errors or nil if valid.
func (v Validator) Var(val any, tag string) ([]string, error) {
err := v.backend.Var(val, tag)

Expand All @@ -65,6 +74,7 @@ func (v Validator) Var(val any, tag string) ([]string, error) {
return errs, nil
}

// Struct validates a given struct instance according to the validator rules set up and returns any field validation errors.
func (v Validator) Struct(s any, tl Translate) (FieldErrors, error) {
fe := make(FieldErrors)

Expand Down Expand Up @@ -119,6 +129,7 @@ func (v Validator) Struct(s any, tl Translate) (FieldErrors, error) {
return fe, nil
}

// getStructField retrieves the specified field by following a path of names within a struct type using reflection.
func getStructField(s reflect.Type, path []string) *reflect.StructField {
var field reflect.StructField

Expand All @@ -139,6 +150,7 @@ func getStructField(s reflect.Type, path []string) *reflect.StructField {
return &field
}

// parseErrorsTag parses the "errors" tag of a struct field into a map of error keys and their corresponding messages.
func parseErrorsTag(tag string) tagErrors {
errMap := make(tagErrors)

Expand Down
1 change: 1 addition & 0 deletions validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

var dateRegex = regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}$`)

// validateDateString validates whether a given string conforms to the yyyy-mm-dd date format.
func validateDateString(fl validator.FieldLevel) bool {
val := fl.Field().String()
if len(val) == 0 {
Expand Down

0 comments on commit 67f16b9

Please sign in to comment.