Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pattern for String fields #9

Closed
matthewtylerwood opened this issue Nov 19, 2021 · 1 comment · Fixed by #11
Closed

Support pattern for String fields #9

matthewtylerwood opened this issue Nov 19, 2021 · 1 comment · Fixed by #11
Labels
enhancement New feature or request

Comments

@matthewtylerwood
Copy link
Contributor

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 errMinimum
		}
		// 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
}
@matthewtylerwood matthewtylerwood added the enhancement New feature or request label Nov 19, 2021
@jakecoffman
Copy link
Owner

PRs are absolutely welcome!

Any missing swagger features/validations would be great. Just remember to write some tests because refactoring is prone to breaking things.

matthewtylerwood added a commit to matthewtylerwood/crud that referenced this issue Dec 3, 2021
jakecoffman pushed a commit that referenced this issue Dec 3, 2021
Co-authored-by: Matthew Wood <mwood@firstorion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants