Tuesday, February 18, 2025
HomeGolangA kind-safe, expressive, and extensible validator library for Golang

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


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

Set up in your undertaking:

go get github.com/cohesivestack/valgo

Import in your code:

import v github.com/cohesivestack/valgo

Word: You should use some other aliases as an alternative of v or simply reference the package deal valgo instantly.

Validation session

The Validation session in Valgo is the principle construction for validating a number of values. It’s known as ‘Validation’ in code.

A validation session will comprise a number of Validators, the place every Validator could have the accountability to validate a worth with a number of guidelines.

There are a number of capabilities to create a Validation session, relying on the necessities:

  • New(),
  • Is(...),
  • In(...),
  • InRow(...),
  • Verify(...),
  • AddErrorMessage(...)

Is(...) is more likely to be probably the most steadily used perform in your validations. When Is(...) is named, the perform creates a validation and receives a validator on the similar time. Within the subsequent part, you’ll study extra concerning the Is(...) perform.

Is(...) perform

The Is(...) perform permits you to move a Validator with the worth and the foundations for validating it. On the similar time, create a Validation session, which helps you to add extra Validators so as to confirm extra values.

As proven within the following instance, we’re passing to the perform Is(...) the Validator for the full_name worth. The perform returns a Validation session that enables us so as to add extra Validators to validate extra values; within the instance case the values age and standing:

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

if !val.Legitimate() {
  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""
  ],
  "standing": [
    "Status is not valid"
  ]
}

Validation.Legitimate() perform

Validation session present this perform, which returns both true if all their validators are legitimate or false if any one in every of them is invalid.

Within the following instance, although the Validator for age is legitimate, the Validator for standing is invalid, making your complete Validator session invalid.

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

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

output:

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

Validation.IsValid(...) perform

This capabilities permits to examine if an particular worth in a Validation session is legitimate or not. That is very helpful for conditional logic.

The next instance prints an error message if the age worth is invalid.

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

if !val.IsValid("age") {
  fmt.Println("Warning: somebody underage is making an attempt to enroll")
}

output:

Warning: somebody underage is making an attempt to enroll

In(...) perform

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments