validator

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

The validator package contains the validators use by the validation package to perform validation.

The Validator interface defined in this package require that a function ReadOptionsFromTagItems([]string) error be implemented.

The proper syntax for struct field tag validator instructions is as follows:

`validate:"validatorname,required,validatorspecificvalues"`

So for instance using the int validator might have a tag lie this:

`validate:"int,min=3,max=15"`

This could be read by the validation package to produce an array of validation arguments from a struct field tag. The format is a comma sperated list of validation parameters where:

  • The first parameter is always the validator name as registered in the validators map in the validation package.
  • There is a special case when validating an embedded struct, you need the tag data, but for the validator name you need to add "struct" like below:
  • `validate:"struct"`
  • You can still have the required parameter in the tag data also, and if the underlying field is a pointer then the normal required rules for a pointer apply.
  • The second parameter is the validator parameters, if you are using the required parameter for any validator, it bus the the second parameter in the tag data to be registered properly.
  • After than, any additional validator parameters that you may need

The validator parameters are the read by the above mentioned ReadOptionsFromTagItems function implemented by the validator matched by the validator name is the tag data.

For examples of how a validator is implemented take a look at the various validators implemented in this package.

Index

Constants

View Source
const (
	InvalidTypeErrorTemplate = "type: the value of %s is of type %T which is not valid"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator

type Validator interface {
	// Validate takes a value, fieldName if from struct, and the fieldKind and returns true if valid and false if not.
	// If the value is not valid then an error should also be returned with info on why its invalid.
	Validate(value interface{}, fieldName string, fieldKind reflect.Kind) (bool, error)
	/*
		ReadOptionsFromTagItems takes in an array of tag arguments and reads them into the validator.
		This function is intended to be attached to a pointer of the validator,
		and the pointer has its options set from this method.
		An error should be returned if there is an issue populating the Validator options.

		For more information on the validation tag syntax please see the package documentation for the validtor package.

		An example of the implementation signature would be:

			func (v *myCustomValidator) ReadOptionsFromTagItems error
	*/
	ReadOptionsFromTagItems([]string) error
}

This interface represents a data validator, they are intended to be simple and easy to implement for custom types.

type ValidatorFactory

type ValidatorFactory func() Validator

ValidatorFactory is a type alias for a function that take no parameters and reutrns a Validator.

Jump to

Keyboard shortcuts

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