Help us shape the future of Go! Take the 2026 Go Developer Survey

validator

package module
v0.0.0-...-34b65cc Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2019 License: MIT Imports: 1 Imported by: 0

README

Validator

Build Status Go Report Card GoDoc

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

The standard check executes a "nil" check against the value being passed into the validator. It then uses reflection to determine if the object is a pointer or not and whether the pointer is nil.

If it's a pointer and the value is nil then it returns invalid, if it's not a pointer or the value is not nil then it checks to see if the type of the object implements the Validator interface. If the object does not implement the interface then it returns valid because the object is non-nil. If it does implement the validator interface then it executes the validate method on the object after typecasting it. At this point the IsValid method will return the value returned from the Validate method.

To install:

DEP

dep ensure -add github.com/benjivesterby/validator

Go Get

go get github.com/benjivesterby/validator

To use:

import "github.com/benjivesterby/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.IsValid(testStruct{true})

See unit tests for more examples

https://github.com/benjivesterby/validator/blob/master/validate_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValid

func IsValid(obj interface{}) (valid bool)

IsValid 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

This section is empty.

Jump to

Keyboard shortcuts

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