Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions dsl/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,17 @@ func MinLength(val int) {
}
}

// AllowEmptyValue sets the AllowEmptyValue flag for a query or header parameter.
// If not set, default behavior in paramFor is in != "path".
func AllowEmptyValue(val bool) {
if a, ok := eval.Current().(*expr.AttributeExpr); ok {
if a.Validation == nil {
a.Validation = &expr.ValidationExpr{}
}
a.Validation.AllowEmptyValue = &val
}
}

// MaxLength adds a "maxItems" validation to the attribute.
// See http://json-schema.org/latest/json-schema-validation.html#anchor42.
//
Expand Down
3 changes: 3 additions & 0 deletions expr/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type (
// described at
// http://json-schema.org/latest/json-schema-validation.html#anchor61.
Required []string
// AllowEmptyValue indicates whether the attribute allows empty
// values. This is primarily used for query string parameters.
AllowEmptyValue *bool
}

// ValidationFormat is the type used to enumerate the possible string
Expand Down
7 changes: 6 additions & 1 deletion http/codegen/openapi/v3/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@ func paramsFromHeadersAndCookies(endpoint *expr.HTTPEndpointExpr, rand *expr.Exa

// paramFor converts the given attribute into a OpenAPI spec parameter.
func paramFor(att *expr.AttributeExpr, name, in string, required bool, rand *expr.ExampleGenerator) *Parameter {
allowEmpty := in != "path"
if att.Validation != nil && att.Validation.AllowEmptyValue != nil {
allowEmpty = *att.Validation.AllowEmptyValue
}

param := &Parameter{
Name: name,
In: in,
Description: att.Description,
AllowEmptyValue: in != "path",
AllowEmptyValue: allowEmpty,
Required: required,
Schema: newSchemafier(rand).schemafy(att),
Extensions: openapi.ExtensionsFromExpr(att.Meta),
Expand Down