Socket
Socket
Sign inDemoInstall

github.com/crunchyroll/govalid

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/crunchyroll/govalid

govalid generates validation code for maps of strings to strings to marshal the data into well-typed structures. Command-line invocation is as follows. If no input file is specified as a positional argument, govalid reads from standard in. If no output file is specified and reading from standard in, output will be written to standard out. Otherwise, output will be written to the a path generated by replacing the input path extension with ".go". The generated code abides by gofmt. It leverages strconv.Parse, net/mail.ParseAddress, and net/url.Parse. govalid is meant to be used with go generate. In your project, you write .v files, which are valid Go files that define structure types into which you want to marshal input data. Here is an example of a .v file. Given this input file, govalid will generate the following Go file. Look at all that code you didn't have to write. govalid is meant to help you avoid having to write such boilerplate data validation code. Note that any fields not mentioned in the input structure will be ignored. Supported Types You are responsible for importing net/mail or net/url in your .v file yourself if using a mail address or URL. The generated validation code for ints and uints uses a base of 0, so input strings may be in any base represented by the strconv.ParseInt or strconv.ParseUint functions. For example, a hexadecimal value would be parsed by passing in "0xbeef" or "48879". You may tag your struct fields to activate extra validation logic. Use the tag key "valid". Three tags are supported. If "def" is unaccompanied by a value, the field is optional, and the default is the zero value given by the field's type. If provided, it is injected directly into the generated code. max and min do not apply to bools. On numeric types, max and min can be used to bound the value. Like the default value, the bounds you specify in the tag will be injected directly into the generated code. For strings, URLs and email addresses, the bounds apply to the length of the input data string. If your structure name indicates it is to be (un)exported, then the validation function will also be (un)exported. For example, in the above sample, the validation function was unexported. But if we had written out instead, then our validation function would have had the following signature.


Version published

Readme

Source

govalid generates validation code for maps of strings to strings to marshal the data into well-typed structures.

Documentation

  • GoDoc Documentation

Testing

Run go test to test. You may want to run go test -short to avoid fuzz testing of random input programs based off of the test/struct.ebnf grammar.

Future Work

  • Real functional testing.
  • Nicer error reporting. Ideally, the validation library would give you an error object from which you could easily generate a human-readable string indicating all of the bad fields passed in and why they were bad.
  • Add more types to validate:
    • Enums?
  • Include comments from original source.
  • Handle weirder pre-existing imports source files, such as strconv imported as some other name.

Bugs

  • Users cannot add more types to validate without modifying this library.

FAQs

Last updated on 27 Aug 2015

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