validator

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package validator

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToGetTranslator = errors.New("failed to get translator for `en`")
	ErrValidationFailed      = errors.New("validation failed")
	ErrInputNotTranslatable  = errors.New("input is not translatable")
)

Functions

This section is empty.

Types

type Error

type Error struct {
	Field   string `json:"field,omitempty"`
	Message string `json:"message,omitempty"`
}

Error is the serializer for a single validation error.

type InternalErrorSerializer

type InternalErrorSerializer struct {
	ErrorID string `json:"error_id"`
}

InternalErrorSerializer is the serializer for internal errors. Generally, this is returned when there is an internal error in the application.

Example:

c.Status(fiber.StatusInternalServerError).JSON(common.InternalErrorSerializer{
  ErrorID: h.logger.Error(err),
})

type ParsingErrorSerializer

type ParsingErrorSerializer struct {
	Error string `json:"error"`
}

ParsingErrorSerializer is the serializer for parsing errors. Generally, this is returned when there is an error parsing the request body or query parameters.

Example:

c.Status(fiber.StatusBadRequest).JSON(common.ParsingErrorSerializer{
  Error: "error parsing input data",
})

type V10

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

func NewValidatorV10

func NewValidatorV10() (*V10, error)

func (V10) Translate

func (v V10) Translate(err error) []Error

Translate implements validator.Validator interface.

func (V10) ValidateStruct

func (v V10) ValidateStruct(input any) error

ValidateStruct implements validator.Validator interface.

type ValidationErrorSerializer

type ValidationErrorSerializer struct {
	Errors []Error `json:"errors"`
}

ValidationErrorSerializer is the serializer for validation errors. Generally, this is returned when there is an error validating the input/output data.

Example:

c.Status(fiber.StatusBadRequest).JSON(common.ValidationErrorSerializer{
  Errors: h.validator.Translate(err),
})

type Validator

type Validator interface {
	// ValidateStruct validates a struct using the validate struct tag.
	// The input should be a pointer to a struct, and it should have exported fields.
	ValidateStruct(input any) error
	// Translate translates the error returned by ValidateStruct to a slice of errors.
	// that can be used to return to the user.
	Translate(err error) []Error
}

Jump to

Keyboard shortcuts

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