Socket
Socket
Sign inDemoInstall

github.com/benjivesterby/validator

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/benjivesterby/validator

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.


Version published

Readme

Source

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

FAQs

Last updated on 08 Aug 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc