Skip to content

devnw/validator

Validator

Build & Test Go Report Card codecov Go Reference License: MIT PRs Welcome

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

To install:

go get -u go.devnw.com/validator

To use validator

  1. import go.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 "go.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

About

Flexible, fast validation library for Go. Handles both boolean validation as well as error returns (assertion) for expanded validation information.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

  •  
  •  

Packages

No packages published

Languages