~bigbes/core-go

073f26337febc0cb4cb402e51ee125fd49b33331 — Drew DeVault 4 years ago f44afb1
valid: add generic optional interface
1 files changed, 13 insertions(+), 0 deletions(-)

M valid/valid.go
M valid/valid.go => valid/valid.go +13 -0
@@ 36,6 36,19 @@ func (valid *Validation) Ok() bool {
	return len(graphql.GetErrors(valid.ctx)) == 0
}

// Fetches an item from the validation context, which must have an input
// registered. If the field is not present, the callback is not run. Otherwise,
// the function is called with the value for the user to conduct further
// validation with.
func (valid *Validation) Optional(name string, fn func(i interface{})) {
	if valid.input == nil {
		panic("Attempted to validate fields without input")
	}
	if o, ok := valid.input[name]; ok {
		fn(o)
	}
}

// Fetches a string from the validation context, which must have an input
// registered. If the field is not present, the callback is not run. If
// present, but not a string, an error is recorded. Otherwise, the function is