validator

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: MIT Imports: 2 Imported by: 0

README

Validator

CI Go Report Card codecov Go Reference PRs Welcome

Validator can be executed against any struct / interface to determine if it is valid.

To install:

go get -u devnw.com/validator

To use validator

  1. import devnw.com/validator
  2. execute boolean validation: validator.Valid(obj1, obj2, ..., objN)
  3. execute validation assertion: validator.Assert(obj1, obj2, ..., objN)

Valid() returns a boolean indicating validity

Assert() returns a nil error if the inputs are valid, and returns an error for invalid arguments, and the error specifies the index of the erroneous value

To implement your own validator use the interface method Validate() bool as shown below:

import "devnw.com/validator"

type testStruct struct {
    valid bool
}

// Implement the validate method on your struct
func(this testStruct) Validate() bool {
    return this.valid
}

// This accepts an interface. If the interface passed to it is valid it returns true,
// otherwise it returns false.
validator.Valid(testStruct{true})

This library will check for nil first on any nillable type, then it uses a type switch to check for validity on known types.

  • For slices it will indicate which element of the slice that is passed in is invalid when using Assert

  • Valid will also check individual slice indexes but will not indicate which is invalid since it only returns a bool

Documentation

Overview

Package validator sets up several methods for simple validation as well as setting up an interface which when implemented allows for custom validation logic by the implementor. `Valid()` returns a boolean indicating if the value(s) passed into it are valid based on the different types. `Assert()` returns an error indicating `nil` if the values are valid. Otherwise `Assert()` returns an error indicating the index of the value(s) passed into it as to which was determined to be invalid.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(objs ...interface{}) error

Assert determines the validity of each value passed in to determine if the inputs are valid. A non-nil error return indicates an invalid assertion and a nil error returns indicates validity

func IsValid

func IsValid(objs ...interface{}) bool

IsValid is DEPRECATED, replaced by Valid. This method will be available until version 1.1.0

func Valid

func Valid(objs ...interface{}) bool

Valid reads in an object, and checks to see if the object implements the validator interface if the object does then it executes the objects validate method and returns that

Types

type ValidationError

type ValidationError struct {

	// Message contains the custom message
	// for this validation error
	Message string

	// Index is the argument index which was
	// invalid during validation
	Index int

	// Type is the type of the invalid argument
	// at the index specified
	Type reflect.Type

	// ValidatorFailure indicates if the failure
	// was at the level which executes the Validate
	// method of a struct implementing the validator
	// interface which indicates it was in the users
	// validation logic where the failure occurred
	ValidatorFailure bool
}

ValidationError is a custom error type for the validator library which provides additional information when using the Assert methods for validating input values. This error contains more information pertinent to what the errors were when attempting to validation the arguments.

func (ValidationError) Error

func (e ValidationError) Error() string

Error implements the error interface for validation errors to be returned instead of standard lib errors

func (ValidationError) String

func (e ValidationError) String() string

String implements the stringer interface so that the error can be properly printed by the fmt printers, etc...

Jump to

Keyboard shortcuts

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