All Projects → twharmon → Govalid

twharmon / Govalid

Licence: mit
Struct validation using tags

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Govalid

Angular Validation
[INACTIVE] Client Side Validation for AngularJS 1. (You should use version > 2 💥)
Stars: ✭ 714 (+3145.45%)
Mutual labels:  validation
Flow Runtime
A runtime type system for JavaScript with full Flow compatibility.
Stars: ✭ 813 (+3595.45%)
Mutual labels:  validation
Andhow
Strongly typed, validated, easy to use Java configuration
Stars: ✭ 17 (-22.73%)
Mutual labels:  validation
Skinny Framework
🚝 "Scala on Rails" - A full-stack web app framework for rapid development in Scala
Stars: ✭ 719 (+3168.18%)
Mutual labels:  validation
Winterfell
Generate complex, validated and extendable JSON-based forms in React.
Stars: ✭ 787 (+3477.27%)
Mutual labels:  validation
Musthave
A Node.js helper module for checking object elements against a list of required elements.
Stars: ✭ 5 (-77.27%)
Mutual labels:  validation
Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+3127.27%)
Mutual labels:  validation
Validating
Automatically validating Eloquent models for Laravel
Stars: ✭ 906 (+4018.18%)
Mutual labels:  validation
React Final Form
🏁 High performance subscription-based form state management for React
Stars: ✭ 6,781 (+30722.73%)
Mutual labels:  validation
Laravel Jsonapi
Basic setup framework for creating a Laravel JSON-API server
Stars: ✭ 16 (-27.27%)
Mutual labels:  validation
Hyperform
Capture form validation back from the browser
Stars: ✭ 729 (+3213.64%)
Mutual labels:  validation
Form Backend Validation
An easy way to validate forms using back end logic
Stars: ✭ 784 (+3463.64%)
Mutual labels:  validation
Strictyaml
Type-safe YAML parser and validator.
Stars: ✭ 836 (+3700%)
Mutual labels:  validation
Spected
Validation library
Stars: ✭ 717 (+3159.09%)
Mutual labels:  validation
Identity Number
Validator for Swedish personal identity numbers (personnummer). For use "standalone" or with Laravel.
Stars: ✭ 17 (-22.73%)
Mutual labels:  validation
Go Proto Validators
Generate message validators from .proto annotations.
Stars: ✭ 713 (+3140.91%)
Mutual labels:  validation
Simple Java Mail
Simple API, Complex Emails (JavaMail smtp wrapper)
Stars: ✭ 821 (+3631.82%)
Mutual labels:  validation
Furion
Make .NET development easier, more versatile, and more popular.
Stars: ✭ 902 (+4000%)
Mutual labels:  validation
Swiftyform
iOS framework for creating forms
Stars: ✭ 907 (+4022.73%)
Mutual labels:  validation
Tram Policy
Policy Object Pattern
Stars: ✭ 16 (-27.27%)
Mutual labels:  validation

Govalid

Use Govalid to validate structs.

Documentation

For full documentation see pkg.go.dev.

Example

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/twharmon/govalid"
)

type Post struct {
	// ID has no constraints
	ID int

	// Title is required, must be at least 3 characters long, cannot be
	// more than 20 characters long, and must match ^[a-zA-Z ]+$
	Title string `govalid:"req|min:3|max:20|regex:^[a-zA-Z ]+$"`

	// Body is not required, cannot be more than 10000 charachers,
	// and must be "fun" (a custom rule defined below).
	Body string `govalid:"max:10000|fun"`

	// Category is not required, but if not zero value ("") it must be
	// either "announcement" or "bookreview".
	Category string `govalid:"in:announcement,bookreview"`
}

var v = govalid.New()

func main() {
	v.Register(Post{}) // Register all structs at load time

	// Add Custom validation to the struct `Post`
	v.AddCustom(Post{}, func(val interface{}) string {
		post := val.(*Post)
		if post.Category != "" && !strings.Contains(post.Body, post.Category) {
			return fmt.Sprintf("Body must contain %s", post.Category)
		}
		return ""
	})

	// Add custom string "fun" that can be used on any string field
	// in any struct.
	v.AddCustomStringRule("fun", func(field string, value string) string {
		if float64(strings.Count(value, "!")) / float64(utf8.RuneCountInString(value)) > 0.001 {
			return ""
		}
		return fmt.Sprintf("%s must contain more exclamation marks", field)
	})

	p := Post{
		ID:       5,
		Title:    "Hi",
		Body:     "Hello world!",
		Category: "announcement",
	}

	vio, err := v.Violations(&p)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(vio)
}

Benchmarks

BenchmarkValidatorStringReqInvalid	         192 ns/op	      48 B/op	       3 allocs/op
BenchmarkValidatorStringReqValid	        98.5 ns/op	      16 B/op	       1 allocs/op
BenchmarkValidatorsVariety	                1114 ns/op	     281 B/op	      13 allocs/op

Contribute

Make a pull request.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].