Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 793 Bytes

README.md

File metadata and controls

52 lines (42 loc) · 793 Bytes

Guard

Guard is a fluent argument validation library.

Usage

Without Guard

type Person struct {
 	Name string
	Age  int
}

func NewPerson(name string, age int) (*Person, error) {

	if name == nil {
		return nil, errors.New("Name cannot be null.")
	}

	if name == "" {
		return nil, errors.New("Name cannot be empty.")
	}

	if len(name) < 3 {
		return nil, errors.New("Name cannot be shorter than 3 characters")
	}

	return &Person{
		Name: name,
		Age:  age,
	}, nil
}

With Guard

type Person struct {
	Name string
	Age  int
}

func NewPerson(name string, age int) (*Person, error) {

	v := Guard(name, "Name").NotNull().NotEmpty().MinLength(3)
	
	if v.Error != nil {
		return nil, v.Error
	}

        return &Person{
		Name: name,
		Age:  age,
        }, nil	
}