go-valid

module
v0.0.0-...-c47e7d8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2022 License: MIT

README

go-valid

A struct validator

package main

import (
  "github.com/fiuskylab/go-valid/validator"
)

type Foo struct {
  Field1 string `validator:""`
  Field2 int
}

// Rule keys MUST be lowercase
var fooRules map[string][]string = {
  "field1": [
    "required", 
    "min:2", 
    "max:10", 
    "type:string"
  ],
  "field2": [
    "required", 
    "min:2", 
    "max:10", 
    "regex:[a-zA-Z]{1,10}", 
    "type:int"
  ],
}

func main() {
  foo := Foo{
    Field1: "something",
    Field2: 20,
  }
  res, err := validator.Check(&foo, fooRules)
  if err != nil {
    // handles error
  }

  if !res.IsValid() {
    // not valid
  }
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL