Skip to content

proto-studio/protovalidate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tests License GoDoc codecov Go Report Card Discord Chat

ProtoValidate is a fluent data validation library for Go.

Project goals:

  1. Human readable easy to understand and write validation rules.
  2. Easily extensible / composable to support any data type and rules.
  3. Detailed, actionable, and customizable errors.

Features:

  • Type checking at compile time.
  • Declarative rule syntax.
  • Works on deeply nested objects and slices/arrays.
  • Easy to extend with custom validation rules.
  • Easy to support additional data types.
  • Structured error responses make it easy to format, display, and correct errors.
  • Support for Internationalization (i18n) for error messages.
  • Able to take unstructured input (such as Json) and output structured data.

Common use cases:

  • API input validation.
  • Command line flag validation.
  • Unit testing.
  • File validating.

Supported data types out of the box:

  • interface
  • string
  • int / int8 / int16 / int32 / int64
  • uint / uint8 / uint16 / uint32 / uint64
  • float32 / float64
  • struct / map / []
  • time.Time
  • Email addresses
  • Domains

Easily customize to support your own data types.

Versioning

This package follows conventional Go versioning. Any version up to version 1.0.0 is considered "unstable" and the API may change.

When possible, any backwards incompatible changes in the unstable releases will follow the procedure:

  1. Mark old methods as deprecated.
  2. Update deprecated methods to call newer methods.
  3. Document the changes in the release notes.

Getting Started

Quick Start

go get proto.zip/studio/validate

Simple usage:

package main

import (
        "fmt"
        "os"

        "proto.zip/studio/validate"
        "proto.zip/studio/validate/pkg/rules"
)

var ruleSet rules.RuleSet[string] = rules.String().
        WithMinLen(3).
        WithMaxLen(7)

// Try changing the string to see different results
func main() {
        str := "a"

        if _, err := ruleSet.Validate(str); err != nil {
                fmt.Println(err)
                os.Exit(1)
        }
}

See the examples folder for more samples.

Best Practices

Here are some best practices to help you get the most out of ProtoValidate:

  • Break custom rules out into their own testable functions.
  • If you need the whole object for your rule you can add it to the object RuleSet instead of the key.
  • Don't redefine your rules sets every use. Rule sets are immutable so you can use/reuse the same instance in multiple threads.
  • Impossible rules will often panic at runtime. Defining your rule sets at the top level or in module init will let you catch them at launch instead of later.

Sponsors

Support

ProtoValidate is built for mission critical code. We want you to get all the support you need.

For community support join the ProtoStudio Discord Community. If you require commercial support please contact our premium support partner Curioso Industries.