validator

package
v0.0.0-...-37076da Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Example
package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"log"

	"github.com/jeanmolossi/vigilant-waddle/src/pkg/validator"
)

// InputToValidate is a struct to validate example.
//
// Validation tags are based on the validator package.
// Read more at:
// https://pkg.go.dev/github.com/go-playground/validator/v10?utm_source=godoc#pkg-overview
type InputToValidate struct {
	Field  string `validate:"required"`
	Nested struct {
		Field string `validate:"required"`
	} `validate:"required"`
}

// GetErrorMap implements validator.ModuleErrorMap interface.
func (i *InputToValidate) GetErrorMap() map[string]map[string]error {
	return map[string]map[string]error{
		"field": {
			"required": errors.New("field is required"),
		},
		"nested": {
			"required": errors.New("nested is required"),
		},
		"nested.field": {
			"required": errors.New("nested field is required"),
		},
	}
}

func main() {
	i := &InputToValidate{}
	v := validator.NewCustomValidator()
	err := v.Validate(i)

	result := errToInterface(err)

	fmt.Println(result)
}

// errToInterface converts error to interface.
//
// This is a helper function to convert error to interface.
// This is useful to print error in a pretty way.
func errToInterface(err error) interface{} {
	var result interface{}
	bytes, _ := json.Marshal(err)
	if err := json.Unmarshal(bytes, &result); err != nil {
		log.Fatal(err)
	}

	return result
}
Output:

	map[error:Bad Request errors:[map[field:field message:field is required] map[field:nested.field message:nested field is required]]]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFieldError

func GetFieldError(err validator.FieldError, moduleErrMap ModuleErrorMap) error

GetFieldError gets a validation field error and ModuleErrorMap. That function select the correct field error from the map of errors and return that error.

Types

type CustomValidator

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

CustomValidator is a struct to manage the validator.

func NewCustomValidator

func NewCustomValidator() *CustomValidator

NewCustomValidation is a factory method to create a CustomValidator.

func (*CustomValidator) Validate

func (cv *CustomValidator) Validate(i interface{}) error

Validate is a method to validate a struct with custom validator. Param `i` should be an struct tagged with validate tag.

type FieldError

type FieldError struct {
	Field string `json:"field" example:"field_name"`
	Err   string `json:"message" example:"field_name is required"`
}

FieldError is a struct to manage the field errors.

func (*FieldError) Error

func (f *FieldError) Error() string

Error implements error interface.

type ModuleErrorMap

type ModuleErrorMap interface {
	// GetErrorMap is the method who returns a map with errors.
	GetErrorMap() map[string]map[string]error
}

ModuleErrorMap is an interface with input structs of data should implements to get validated with custom validator.

type ValidationErr

type ValidationErr struct {
	Err    string  `json:"error"`
	Errors []error `json:"errors,omitempty"`
}

ValidationErr is a struct to manage the validation errors. Err is the main error message. Errors is a slice of errors. That slice will contain the errors of every field validation.

func (*ValidationErr) AddError

func (v *ValidationErr) AddError(err error)

AddError is a method to add an error to the slice of errors.

func (*ValidationErr) Error

func (v *ValidationErr) Error() string

Error implements error interface.

Jump to

Keyboard shortcuts

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