10000 Support pattern for String fields · Issue #9 · jakecoffman/crud · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Support pattern for String fields #9
Closed
@matthewtylerwood

Description

@matthewtylerwood

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0