Add solution for gin challenge-3-validation-errors by yogtanko#1746
Conversation
WalkthroughAdds a Gin-based HTTP service providing in-memory product and category models, field and cross-field validation, sanitization, endpoints for single/bulk creation and validation checks, a validation-rules endpoint, and server startup on :8080. ChangesProduct & Category API with Validation
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 89300fce-03da-4b40-b9a5-3871abb87189
📒 Files selected for processing (1)
packages/gin/challenge-3-validation-errors/submissions/yogtanko/solution.go
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
packages/gin/challenge-3-validation-errors/submissions/yogtanko/solution.go (2)
157-164:⚠️ Potential issue | 🟠 Major | ⚡ Quick winSKU uniqueness is still not enforced at write time.
validateProductchecks SKU format only, and both create paths append without an atomic uniqueness guard. Concurrent requests can persist duplicate SKUs.Suggested fix
func createProduct(c *gin.Context) { @@ sanitizeProduct(&product) - mu.Lock() - defer mu.Unlock() + mu.Lock() + for _, existing := range products { + if strings.EqualFold(existing.SKU, product.SKU) { + mu.Unlock() + c.JSON(http.StatusBadRequest, APIResponse{ + Success: false, + Message: "sku already exists", + }) + return + } + } product.ID = nextProductID nextProductID++ products = append(products, product) + mu.Unlock() c.JSON(201, APIResponse{ @@ for i, product := range inputProducts { @@ } else { sanitizeProduct(&product) mu.Lock() + duplicate := false + for _, existing := range products { + if strings.EqualFold(existing.SKU, product.SKU) { + duplicate = true + break + } + } + if duplicate { + mu.Unlock() + results = append(results, BulkResult{ + Index: i, + Success: false, + Errors: []ValidationError{{ + Field: "sku", + Message: "sku already exists", + }}, + }) + continue + } product.ID = nextProductID nextProductID++ products = append(products, product) mu.Unlock()Also applies to: 251-256, 299-303
118-120:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winCategory validation is case-sensitive and inconsistent with category creation rules.
isValidCategoryuses exact match, while category uniqueness usesstrings.EqualFold. This can reject otherwise valid category names by casing alone.Suggested fix
- for _, cur := range categories { - if cur.Name == categoryName { + for _, cur := range categories { + if strings.EqualFold(cur.Name, categoryName) { return true } }
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 39ff678e-148d-4b61-a375-63086fa912b0
📒 Files selected for processing (1)
packages/gin/challenge-3-validation-errors/submissions/yogtanko/solution.go
|
🎉 Auto-merged! This PR was automatically merged after 2 days with all checks passing. Thank you for your contribution, @yogtanko! 📊 Scoreboards and badges will be updated shortly. |
gin challenge-3-validation-errors Solution
Submitted by: @yogtanko
Package: gin
Challenge: challenge-3-validation-errors
Description
This PR contains my solution for gin challenge-3-validation-errors.
Changes
packages/gin/challenge-3-validation-errors/submissions/yogtanko/solution.goTesting
Thank you for reviewing my submission! 🚀