Skip to content

Commit 67f16b9

Browse files
committed
add docs
1 parent 5d7fc11 commit 67f16b9

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

sanitize.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Sanitizer struct {
1111
backend *mold.Transformer
1212
}
1313

14+
// NewSanitizer creates a new instance of Sanitizer with a background context and a default transformer backend.
1415
func NewSanitizer() Sanitizer {
1516
be := modifiers.New()
1617
return Sanitizer{
@@ -19,6 +20,7 @@ func NewSanitizer() Sanitizer {
1920
}
2021
}
2122

23+
// Struct sanitizes the given struct based on the rules defined in its fields' tags.
2224
func (s Sanitizer) Struct(val any) error {
2325
err := s.backend.Struct(s.ctx, val)
2426
if err != nil {

validator.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package validation is basically a wrapper around https://github.com/go-playground/validator and https://github.com/go-playground/mold.
2+
//
3+
// Besides the functionality of the go-playground packages it supports a more flexible error translation by using struct tags for error messages.
4+
//
5+
// The error message is set into the `errors` tag of the struct field, the key is the validator as provided in the `validate`
6+
// struct tag. Multiple validators/errors are supported.
17
package validation
28

39
import (
@@ -20,6 +26,7 @@ type Validator struct {
2026

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

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

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

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

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

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

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

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

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

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

validators.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

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

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

0 commit comments

Comments
 (0)