Closed
Description
Is your feature request related to a problem? Please describe.
Currently there is no way to enforce a pattern on a string field similar to the pattern regex field in the Swagger 2.0 spec.
Describe the solution you'd like
String fields could use a new .Pattern()
to supply a regex and enforce a pattern on string fields. I think fields of other types would just ignore this helper?
This could look something like the following (I didn't show it below, but could also add Pattern to the Parameter/JsonSchema structs so that field also gets included in the swagger):
Let me know what you think. If you accept PRs, I could also try to put something together after the holiday week.
// Field allows specification of swagger or json schema types using the builder pattern.
type Field struct {
kind string
obj map[string]Field
max *float64
min *float64
// TODO: New pattern field
pattern *regexp.Regexp
required *bool
example interface{}
description string
enum enum
_default interface{}
arr *Field
allow enum
strip *bool
unknown *bool
}
...
var (
errRequired = fmt.Errorf("value is required")
errWrongType = fmt.Errorf("wrong type passed")
errMaximum = fmt.Errorf("maximum exceeded")
errMinimum = fmt.Errorf("minimum exceeded")
errEnumNotFound = fmt.Errorf("value not in enum")
errUnknown = fmt.Errorf("unknown value")
// TODO: New Error
errPattern = fmt.Errorf("value does not match pattern")
)
...
case string:
if f.kind != KindString {
return errWrongType
}
if f.required != nil && *f.required && v == "" && !f.allow.has("") {
return errRequired
}
if f.max != nil && len(v) > int(*f.max) {
return errMaximum
}
if f.min != nil && len(v) < int(*f.min) {
return errMinimu
5767
m
}
// TODO: This would be a new check
if !f.pattern.MatchString(v) {
return errPattern
}
...
// Pattern specifies a regex pattern value for this field
func (f Field) Pattern(pattern string) Field {
f.pattern = regexp.MustCompile(pattern)
return f
}