Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/benjivesterby/validator

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/benjivesterby/validator

  • v1.0.8
  • Source
  • Go
  • Socket score

Version published
Created
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

Package last updated on 08 Aug 2021

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc