Skip to content

xload: Struct level validation #42

@ajatprabha

Description

@ajatprabha

Overview

With xload, I want to see what option(s) makes sense for doing validation post loading the values into struct and/or nested structs.

Example

type A struct {
	B *xloadtype.Listener `env:"B"`
	C *xloadtype.Listener `env:"C"`
}

func (a *A) Validate() error {
	if a.B == nil && a.C == nil {
		return errors.New("both A and B cannot be nil, one must be set")
	}
}

The required keyword from xload isn't enough to handle such use-case, I think having an optional Validator interface implementation can be used to check if the object passes custom validation checks while returning up the call stack(after all nested values are loaded), and fail if the validation errors out.

type Validator interface {
	Validate() error
}

Proposal

Add some mechanism to Validate structs at any level, and propagate errors all the way up, if any.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions