adding ROOT_PATH + tests

This commit is contained in:
Julien Neuhart
2019-12-06 17:23:17 +01:00
parent e255f9bfc5
commit f0f48f4ddf
11 changed files with 244 additions and 62 deletions

View File

@@ -2,6 +2,7 @@ package xassert
import (
"fmt"
"strings"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
)
@@ -54,7 +55,67 @@ func StringOneOf(values []string) RuleString {
}
}
type ruleStringStartWith struct {
*baseRuleString
startWith string
}
func (r ruleStringStartWith) validate() error {
const op string = "xassert.ruleStringStartWith.validate"
if strings.HasPrefix(r.value, r.startWith) {
return nil
}
return xerror.Invalid(
op,
fmt.Sprintf("'%s' should start with '%s', got '%s'", r.key, r.startWith, r.value),
nil,
)
}
/*
StringStartWith returns a RuleString for
validating that a string starts with
given string.
*/
func StringStartWith(startWith string) RuleString {
return ruleStringStartWith{
&baseRuleString{},
startWith,
}
}
type ruleStringEndWith struct {
*baseRuleString
endWith string
}
func (r ruleStringEndWith) validate() error {
const op string = "xassert.ruleStringEndWith.validate"
if strings.HasSuffix(r.value, r.endWith) {
return nil
}
return xerror.Invalid(
op,
fmt.Sprintf("'%s' should end with '%s', got '%s'", r.key, r.endWith, r.value),
nil,
)
}
/*
StringEndWith returns a RuleString for
validating that a string ends with
given string.
*/
func StringEndWith(endWith string) RuleString {
return ruleStringEndWith{
&baseRuleString{},
endWith,
}
}
// Compile-time checks to ensure type implements desired interfaces.
var (
_ = RuleString(new(ruleStringOneOf))
_ = RuleString(new(ruleStringStartWith))
_ = RuleString(new(ruleStringEndWith))
)