Site icon Golang Libraries, Apps, Golang Jobs and Go Tutorials

A type-safe, expressive, and extensible validator library for Golang

Valgo is a type-safe, expressive, and extensible validator library for Golang. Supports localization and is built with generics.

Getting started

Install in your project:

go get github.com/cohesivestack/valgo

Import in your code:

import v github.com/cohesivestack/valgo

Note: You can use any other aliases instead of v or just reference the package valgo directly.

Using Valgo

Validation session

The Validation session in Valgo is the main structure for validating one or more values. It is called ‘Validation’ in code.

A validation session will contain one or more Validators, where each Validator will have the responsibility to validate a value with one or more rules.

There are multiple functions to create a Validation session, depending on the requirements:

Is(...) is likely to be the most frequently used function in your validations. When Is(...) is called, the function creates a validation and receives a validator at the same time. In the next section, you will learn more about the Is(...) function.

Is(...) function

The Is(...) function allows you to pass a Validator with the value and the rules for validating it. At the same time, create a Validation session, which lets you add more Validators in order to verify more values.

As shown in the following example, we are passing to the function Is(...) the Validator for the full_name value. The function returns a Validation session that allows us to add more Validators to validate more values; in the example case the values age and status:

val := v.
  Is(v.String("Bob", "full_name").Not().Blank().OfLengthBetween(4, 20)).
  Is(v.Number(17, "age").GreaterThan(18)).
  Is(v.String("singl", "status").InSlice([]string{"married", "single"}))

if !val.Valid() {
  out, _ := json.MarshalIndent(val.Error(), "", "  ")
  fmt.Println(string(out))
}

output:

{
  "age": [
    "Age must be greater than \"18\""
  ],
  "full_name": [
    "Full name must have a length between \"4\" and \"20\""
  ],
  "status": [
    "Status is not valid"
  ]
}

Validation.Valid() function

Validation session provide this function, which returns either true if all their validators are valid or false if any one of them is invalid.

In the following example, even though the Validator for age is valid, the Validator for status is invalid, making the entire Validator session invalid.

val := v.Is(v.Number(21, "age").GreaterThan(18)).
  Is(v.String("singl", "status").InSlice([]string{"married", "single"}))

if !val.Valid() {
  out, _ := json.MarshalIndent(val.Error(), "", "  ")
  fmt.Println(string(out))
}

output:

{
  "status": [
    "Status is not valid"
  ]
}

Validation.IsValid(...) function

This functions allows to check if an specific value in a Validation session is valid or not. This is very useful for conditional logic.

The following example prints an error message if the age value is invalid.

val := v.Is(v.Number(16, "age").GreaterThan(18)).
  Is(v.String("single", "status").InSlice([]string{"married", "single"}))

if !val.IsValid("age") {
  fmt.Println("Warning: someone underage is trying to sign up")
}

output:

Warning: someone underage is trying to sign up

In(...) function


There is no ads to display, Please add some
Exit mobile version