Validator LIVR - Lightweight validator supporting Language Independent Validation Rules Specification (LIVR).
One of the biggest advantages is language independent. There are implementations on other languages so you can use your validation rules in applications on different programming languages. For example, if you write web application with back-end on Go, you can use your validation rules in front-end (JS) and back-end (Go). By reusing your rules, you solve one the most tricky problem of inconsitent validation of the same data.
- Download and install.
go get github.com/k33nice/go-livr
- Example.
package main
import (
"encoding/json"
"fmt"
"github.com/k33nice/go-livr"
)
func main() {
var jsonRules = `{
"name": "required",
"email": ["required", "email"],
"gender": {"one_of": ["male", "female"]},
"phone": {"max_length": 11},
"password": ["required", {"min_length": 10}],
"password2": {"equal_to_field": "password"}
}`
var rules map[string]interface{}
err := json.Unmarshal([]byte(jsonRules), &rules)
if err != nil {
panic(err)
}
var jsonData = []byte(`{
"name": "Jekyll",
"email": "dangerous.game@dregs.us",
"gender": "male",
"phone": "12025550193",
"password": "take_me_as_i_am",
"password2": "take_me_as_i_am"
}`)
var data map[string]interface{}
err = json.Unmarshal(jsonData, &data)
if err != nil {
panic(err)
}
validator := livr.New(&livr.Options{LivrRules: rules})
validatedData, err := validator.Validate(data)
if err != nil {
panic(validator.Errors())
}
fmt.Println(validatedData)
}
If you want, you can get rid of json unmarshal.
type d livr.Dictionary
var rules = d{
"name": d{"nested_object": "required"},
}
validator := livr.New(&livr.Options{LivrRules: rules})
You can use modifiers separately or can combine them with validation:
var jsonRules = `{
"email": ["required", "trim", "email", "to_lc"]
}`
Feel free to register your own rules.
v := livr.New(&livr.Options{LivrRules: rules})
a := livr.Alias{
Name: "strong_password",
Rules: livr.Dictionary{"min_length": 6},
Error: "WEAK_PASSWORD",
}
v.RegisterAliasedRule(a)
- Clone and update subomodule with test cases
git submodule update --init --recursive
- Run tests.
go test ./test -v
See LIVR Specification for detailed documentation and list of supported rules.
- Rules are declarative and language independent
- Any number of rules for each field
- Return together errors for all fields
- Excludes all fields that do not have validation rules described
- Has possibility to validate complex hierarchical structures
- Easy to describe and understand rules
- Returns understandable error codes(not error messages)
- Easy to add own rules
- Rules are be able to change results output ("trim", "nested_object", for example)
- Multipurpose (user input validation, configs validation, contracts programming etc)
Distributed under MIT License, please see license file within the code for more details.