PHP>> vld>> 返回
项目作者: rodcorsi

项目描述 :
Fast struct field validation
高级语言: Go
项目地址: git://github.com/rodcorsi/vld.git
创建时间: 2017-09-13T21:45:00Z
项目社区:https://github.com/rodcorsi/vld

开源协议:MIT License

下载


vld

Build Status
Coverage Status
Go Report Card

Package written in Go for validation of string, numbers and structs without use of tags

Vld may not be considered stable just yet!

Installation

You’ll need Go installed

  1. go get github.com.br/rodcorsi/vld

Usage

  1. oldID := "123"
  2. p := Product{
  3. ID: "12",
  4. OldID: &oldID,
  5. Descr: "ff",
  6. Qty: 10,
  7. }
  8. validate := vld.New()
  9. // use && to stop verification if has one error
  10. // you can use || to check all errors
  11. _ = validate.Ok("id", vld.String(p.ID).Required().Length(1, 20).Error()) &&
  12. validate.Ok("old_id", vld.StrPtr(p.OldID).Length(2, 20).Error()) &&
  13. validate.Ok("descr", vld.String(string(p.Descr)).Length(2, 20).Error()) &&
  14. validate.Ok("qty", vld.NumI64(p.Qty).Range(2, 20).Error())
  15. if err := validate.Error(); err != nil {
  16. fmt.Println(err)
  17. }

Benchmark

Benchmark comparing other validator

  1. $ go test -run=NONE -bench=. -benchmem ./example/
  2. goos: linux
  3. goarch: amd64
  4. pkg: github.com/rodcorsi/vld/example
  5. BenchmarkGovalidator-4 100000 17955 ns/op 2424 B/op 39 allocs/op
  6. BenchmarkValidator-4 1000000 1045 ns/op 64 B/op 2 allocs/op
  7. BenchmarkOzzo-4 200000 7863 ns/op 2104 B/op 50 allocs/op
  8. BenchmarkFourSigma-4 500000 2103 ns/op 688 B/op 25 allocs/op
  9. BenchmarkVld-4 100000000 16.7 ns/op 0 B/op 0 allocs/op