Skip to content
Open
Changes from 2 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
20 changes: 20 additions & 0 deletions pkg/generators/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type commentTags struct {
ExclusiveMinimum *bool `json:"exclusiveMinimum,omitempty"`
MaxLength *int64 `json:"maxLength,omitempty"`
MinLength *int64 `json:"minLength,omitempty"`
MaxBytes *int64 `json:"maxBytes,omitempty"`
MinBytes *int64 `json:"minBytes,omitempty"`
Pattern *string `json:"pattern,omitempty"`
MaxItems *int64 `json:"maxItems,omitempty"`
MinItems *int64 `json:"minItems,omitempty"`
Expand Down Expand Up @@ -187,7 +189,13 @@ func (c *commentTags) ValidationSchema() (*spec.Schema, error) {
}
transformedAdditionalProperties = &spec.SchemaOrBool{Schema: additionalProperties, Allows: true}
}
if c.MaxBytes != nil && c.MaxLength == nil {
Comment thread
BasavarajBankolli marked this conversation as resolved.
Outdated
c.MaxLength = c.MaxBytes
Comment thread
BasavarajBankolli marked this conversation as resolved.
Outdated
}

if c.MinBytes != nil && c.MinLength == nil {
c.MinLength = c.MinBytes
Comment thread
BasavarajBankolli marked this conversation as resolved.
Outdated
}
res := spec.Schema{
SchemaProps: spec.SchemaProps{
Nullable: isNullable,
Expand Down Expand Up @@ -249,6 +257,12 @@ func (c commentTags) Validate() error {
if c.MaxLength != nil && *c.MaxLength < 0 {
err = errors.Join(err, fmt.Errorf("maxLength cannot be negative"))
}
if c.MinBytes != nil && *c.MinBytes < 0 {
err = errors.Join(err, fmt.Errorf("minBytes cannot be negative"))
}
if c.MinLength != nil && c.MinBytes != nil {
Comment thread
BasavarajBankolli marked this conversation as resolved.
err = errors.Join(err, fmt.Errorf("minLength and minBytes cannot both be specified"))
}
if c.MinItems != nil && *c.MinItems < 0 {
err = errors.Join(err, fmt.Errorf("minItems cannot be negative"))
}
Expand All @@ -270,6 +284,9 @@ func (c commentTags) Validate() error {
if c.MinLength != nil && c.MaxLength != nil && *c.MinLength > *c.MaxLength {
err = errors.Join(err, fmt.Errorf("minLength %d is greater than maxLength %d", *c.MinLength, *c.MaxLength))
}
if c.MinBytes != nil && c.MaxBytes != nil && *c.MinBytes > *c.MaxBytes {
err = errors.Join(err, fmt.Errorf("minBytes %d is greater than maxBytes %d", *c.MinBytes, *c.MaxBytes))
}
if c.MinItems != nil && c.MaxItems != nil && *c.MinItems > *c.MaxItems {
err = errors.Join(err, fmt.Errorf("minItems %d is greater than maxItems %d", *c.MinItems, *c.MaxItems))
}
Expand Down Expand Up @@ -352,6 +369,9 @@ func (c commentTags) ValidateType(t *types.Type) error {
if c.MaxLength != nil && !isString {
err = errors.Join(err, fmt.Errorf("maxLength can only be used on string types"))
}
if c.MaxBytes != nil && !isString {
err = errors.Join(err, fmt.Errorf("maxBytes can only be used on string types"))
}
if c.Pattern != nil && !isString {
err = errors.Join(err, fmt.Errorf("pattern can only be used on string types"))
}
Expand Down