Idiomatic way to validate structs

adriaan.wiers

I need to validate that a struct value is correct and this means I need to check every field individually, which is easy for a small number of small structs but I was wondering if there's a better way to do it. Here's how I'm doing it right now.

type Event struct {
    Id     int
    UserId int
    Start  time.Time
    End    time.Time
    Title  string
    Notes  string
}

func (e Event) IsValid() error {
    if e.Id <= 0 {
        return errors.New("Id must be greater than 0")
    }
    if e.UserId <= 0 {
        return errors.New("UserId must be greater than 0")
    }
    if e.End <= e.Start {
        return errors.New("End must be after Start")
    }
    if e.Start < time.Now() {
        return errors.New("Cannot create events in the past")
    }
    if e.Title == "" {
        return errors.New("Title cannot be empty")
    }
    return nil
}

Is this the idiomatic way to validate the values of fields in a struct? It looks cumbersome.

julienc

I don't see any other way to do this quickly. But I found a go package which can help you with this: https://github.com/go-validator/validator

The README file gives this example:

type NewUserRequest struct {
    Username string `validator:"min=3,max=40,regexp=^[a-zA-Z]$"`
    Name string     `validator:"nonzero"`
    Age int         `validator:"min=21"`
    Password string `validator:"min=8"`
}

nur := NewUserRequest{Username: "something", Age: 20}
if valid, errs := validator.Validate(nur); !valid {
    // values not valid, deal with errors here
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Idiomatic way to validate structs

From Dev

What is the idiomatic way in Go to create a complex hierarchy of structs?

From Java

Idiomatic way of logging in Kotlin

From Java

Idiomatic way to prevent slicing?

From Dev

Idiomatic way to remove child

From Dev

Idiomatic way of modifying NSFont

From Dev

Idiomatic way to initialize a Ruby class

From Dev

Idiomatic way of naming Getters in Go

From Dev

Idiomatic way to check for parameter initialization

From Dev

Idiomatic way to test Swift Optionals

From Dev

Idiomatic way to execute an array of blocks

From Dev

Idiomatic Way To Merge Trait Functionality

From Dev

Idiomatic way of merging arrays in Ruby?

From Dev

Idiomatic way of returning nil in Ruby

From Dev

Idiomatic way of If none return false?

From Dev

Idiomatic way to write firstRightOrLefts in Haskell?

From Dev

Idiomatic way to process lists and dicts

From Dev

Is there an idiomatic way to implement the component pattern?

From Dev

Idiomatic way to define constant functions

From Dev

Idiomatic way to possibly compose a function

From Dev

Idiomatic way of If none return false?

From Dev

Initializing instance variable: Idiomatic way

From Dev

Is there a way to access a structs fields as an array?

From Dev

Is there a way to access a structs fields as an array?

From Dev

What is idiomatic clojure to validate that a string has only alphanumerics and hyphen?

From Dev

What is the idiomatic way to convert a String to &str?

From Dev

Idiomatic way to select a map in vector by a key

From Dev

Idiomatic way to recover from stream onError

From Dev

What is an idiomatic way to enumerate the alphabet in go?

Related Related

HotTag

Archive