Skip to content

Commit e8ea0d6

Browse files
committed
feat: add support for parameter schema
1 parent e5bb9cb commit e8ea0d6

6 files changed

Lines changed: 361 additions & 4 deletions

File tree

examples/constraints/api.yaml

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,53 @@ openapi: 3.0.0
22
info:
33
title: Constraints Demo API
44
version: 1.0.0
5-
description: Demo API to showcase constraint constants generation
5+
description: Demo API to showcase constraint constants generation for both schemas and inline parameters
66

77
paths:
88
/users:
9+
get:
10+
operationId: listUsers
11+
summary: List users with pagination and filtering
12+
description: Demonstrates inline parameter constraints generating constants
13+
parameters:
14+
- in: query
15+
name: limit
16+
description: Maximum number of items to return
17+
schema:
18+
type: integer
19+
minimum: 1
20+
maximum: 100
21+
default: 20
22+
- in: query
23+
name: offset
24+
description: Number of items to skip
25+
schema:
26+
type: integer
27+
minimum: 0
28+
- in: query
29+
name: search
30+
description: Search query string
31+
schema:
32+
type: string
33+
minLength: 3
34+
maxLength: 50
35+
- in: query
36+
name: minScore
37+
description: Minimum score filter
38+
schema:
39+
type: number
40+
format: float
41+
minimum: 0.0
42+
maximum: 100.0
43+
responses:
44+
"200":
45+
description: List of users
46+
content:
47+
application/json:
48+
schema:
49+
type: array
50+
items:
51+
$ref: "#/components/schemas/User"
952
post:
1053
operationId: createUser
1154
requestBody:
@@ -22,6 +65,27 @@ paths:
2265
schema:
2366
$ref: "#/components/schemas/User"
2467

68+
/users/{userId}:
69+
get:
70+
operationId: getUser
71+
summary: Get a specific user by ID
72+
parameters:
73+
- in: path
74+
name: userId
75+
required: true
76+
description: User ID (UUID format)
77+
schema:
78+
type: string
79+
minLength: 36
80+
maxLength: 36
81+
responses:
82+
"200":
83+
description: User details
84+
content:
85+
application/json:
86+
schema:
87+
$ref: "#/components/schemas/User"
88+
2589
components:
2690
schemas:
2791
# These are defined as standalone types so constraints become constants

examples/constraints/example.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,79 @@ import (
77
"github.com/labstack/echo/v4"
88
)
99

10-
// Example implementation showing how to use the generated constraint constants
10+
// Example implementation showing how to use the generated constraint constants for both schema types and inline parameters
1111
type Server struct{}
1212

1313
func NewServer() *Server {
1414
return &Server{}
1515
}
1616

17+
// ListUsers demonstrates using constraint constants from inline parameters
18+
func (s *Server) ListUsers(ctx echo.Context, params ListUsersParams) error {
19+
// Validate limit parameter using generated constants
20+
if params.Limit != nil {
21+
if *params.Limit < ListUsersLimitMinimum || *params.Limit > ListUsersLimitMaximum {
22+
return echo.NewHTTPError(http.StatusBadRequest,
23+
fmt.Sprintf("Limit must be between %d and %d", ListUsersLimitMinimum, ListUsersLimitMaximum))
24+
}
25+
} else {
26+
// Use the generated default constant
27+
defaultLimit := ListUsersLimitDefault
28+
params.Limit = &defaultLimit
29+
}
30+
31+
// Validate offset parameter
32+
if params.Offset != nil {
33+
if *params.Offset < ListUsersOffsetMinimum {
34+
return echo.NewHTTPError(http.StatusBadRequest,
35+
fmt.Sprintf("Offset must be at least %d", ListUsersOffsetMinimum))
36+
}
37+
}
38+
39+
// Validate search parameter length
40+
if params.Search != nil {
41+
searchLen := uint64(len(*params.Search))
42+
if searchLen < ListUsersSearchMinLength {
43+
return echo.NewHTTPError(http.StatusBadRequest,
44+
fmt.Sprintf("Search must be at least %d characters", ListUsersSearchMinLength))
45+
}
46+
if searchLen > ListUsersSearchMaxLength {
47+
return echo.NewHTTPError(http.StatusBadRequest,
48+
fmt.Sprintf("Search must not exceed %d characters", ListUsersSearchMaxLength))
49+
}
50+
}
51+
52+
// Validate minScore parameter
53+
if params.MinScore != nil {
54+
if *params.MinScore < ListUsersMinScoreMinimum || *params.MinScore > ListUsersMinScoreMaximum {
55+
return echo.NewHTTPError(http.StatusBadRequest,
56+
fmt.Sprintf("MinScore must be between %.1f and %.1f", ListUsersMinScoreMinimum, ListUsersMinScoreMaximum))
57+
}
58+
}
59+
60+
fmt.Printf("Listing users with limit=%d, offset=%d\n", *params.Limit, *params.Offset)
61+
62+
// Return mock data (implementation not shown)
63+
users := []User{}
64+
return ctx.JSON(http.StatusOK, users)
65+
}
66+
67+
// GetUser demonstrates using constraint constants from path parameters
68+
func (s *Server) GetUser(ctx echo.Context, userId string) error {
69+
// Validate userId length using generated constants
70+
userIdLen := uint64(len(userId))
71+
if userIdLen < GetUserUserIdMinLength || userIdLen > GetUserUserIdMaxLength {
72+
return echo.NewHTTPError(http.StatusBadRequest,
73+
fmt.Sprintf("UserId must be exactly %d characters", GetUserUserIdMinLength))
74+
}
75+
76+
fmt.Printf("Getting user: %s\n", userId)
77+
78+
// Return mock data (implementation not shown)
79+
user := User{Username: UsernameDefault, Age: AgeDefault}
80+
return ctx.JSON(http.StatusOK, user)
81+
}
82+
1783
// CreateUser demonstrates using the generated constraint constants for validation
1884
func (s *Server) CreateUser(ctx echo.Context) error {
1985
var user User
@@ -93,13 +159,20 @@ func Example() {
93159
server := NewServer()
94160
RegisterHandlers(e, server)
95161

96-
// The constraints are available as typed constants:
162+
fmt.Println("=== Schema Constraint Constants ===")
97163
fmt.Printf("Age range: %d-%d (default: %d)\n", AgeMinimum, AgeMaximum, AgeDefault)
98164
fmt.Printf("Username length: %d-%d (default: %s)\n", UsernameMinLength, UsernameMaxLength, UsernameDefault)
99165
fmt.Printf("Port range: %d-%d (default: %d)\n", PortMinimum, PortMaximum, PortDefault)
100166
fmt.Printf("Score range: %.1f-%.1f (default: %.1f)\n", UserScoreMinimum, UserScoreMaximum, UserScoreDefault)
101167
fmt.Printf("Tags count: %d-%d\n", UserTagsMinItems, UserTagsMaxItems)
102168
fmt.Printf("Is active default: %v\n", IsActiveDefault)
103169

170+
fmt.Println("\n=== Inline Parameter Constraint Constants ===")
171+
fmt.Printf("ListUsers limit: %d-%d (default: %d)\n", ListUsersLimitMinimum, ListUsersLimitMaximum, ListUsersLimitDefault)
172+
fmt.Printf("ListUsers offset: minimum %d\n", ListUsersOffsetMinimum)
173+
fmt.Printf("ListUsers search length: %d-%d\n", ListUsersSearchMinLength, ListUsersSearchMaxLength)
174+
fmt.Printf("ListUsers minScore: %.1f-%.1f\n", ListUsersMinScoreMinimum, ListUsersMinScoreMaximum)
175+
fmt.Printf("GetUser userId length: %d-%d\n", GetUserUserIdMinLength, GetUserUserIdMaxLength)
176+
104177
e.Logger.Fatal(e.Start(":8080"))
105178
}

examples/constraints/gen.go

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/externalref/petstore/externalref.gen.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)