From cb5aee1b6993971dea2c90a62203bebd3f768468 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 20 Aug 2021 11:07:17 +0200 Subject: [PATCH] valid: accept format strings --- valid/valid.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/valid/valid.go b/valid/valid.go index 2337a374c7e0ac8090f45f6344ab7e6bcf0fbbad..66383d6a53cfed3cf26399475e1890662d59729e 100644 --- a/valid/valid.go +++ b/valid/valid.go @@ -47,7 +47,7 @@ func (valid *Validation) OptionalString(name string, fn func(s string)) { if o, ok := valid.input[name]; ok { s, ok := o.(string) valid. - Expect(ok, fmt.Sprintf("Expected %s to be a string", name)). + Expect(ok, "Expected %s to be a string", name). WithField(name) if ok { fn(s) @@ -55,15 +55,12 @@ func (valid *Validation) OptionalString(name string, fn func(s string)) { } } -// Asserts that a condition is true, recording a GraphQL error with the given -// message if not. -func (valid *Validation) Expect(cond bool, msg string) *ValidationError { - if cond { - return &ValidationError{valid: valid} - } +// Creates a validation error unconditionally. +func (valid *Validation) Error(msg string, + items ...interface{}) *ValidationError { err := &gqlerror.Error{ Path: graphql.GetPath(valid.ctx), - Message: msg, + Message: fmt.Sprintf(msg, items), } graphql.AddError(valid.ctx, err) return &ValidationError{ @@ -72,6 +69,16 @@ func (valid *Validation) Expect(cond bool, msg string) *ValidationError { } } +// Asserts that a condition is true, recording a GraphQL error with the given +// message if not. +func (valid *Validation) Expect(cond bool, + msg string, items ...interface{}) *ValidationError { + if cond { + return &ValidationError{valid: valid} + } + return valid.Error(msg, items) +} + // Associates a field name with an error. func (err *ValidationError) WithField(field string) *ValidationError { if err.err == nil { @@ -87,9 +94,10 @@ func (err *ValidationError) WithField(field string) *ValidationError { // Composes another assertion onto the same validation context which initially // created an error. Short-circuiting is used, such that if the earlier // condition failed, the new condition is not considered. -func (err *ValidationError) And(cond bool, msg string) *ValidationError { +func (err *ValidationError) And(cond bool, + msg string, items ...interface{}) *ValidationError { if err.err != nil { return err } - return err.valid.Expect(cond, msg) + return err.valid.Expect(cond, msg, items...) }