Skip to content

Commit c5c7f86

Browse files
JIeJaittgabyReneWerner87
authored
🔥 Feature: Enhance CheckConstraint method for improved error handling (#3356)
* 🔥 Feature: Enhance CheckConstraint method for improved error handling * Revert "🔥 Feature: Enhance CheckConstraint method for improved error handling" This reverts commit 68e8777. * Reapply "🔥 Feature: Enhance CheckConstraint method for improved error handling" This reverts commit 9e6c8e6. * 🚨 Test: Add comprehensive tests for CheckConstraint method with various constraint scenarios * 🩹 Fix: lint error * 🩹 Fix: Update CheckConstraint method to return true for noConstraint and improve error handling * ♻️ Refactor: Remove unused CheckConstraint test cases and reorganize benchmark test cases for clarity * ♻️ Refactor: Remove outdated test cases from path_testcases_test.go and clean up CheckConstraint method in path.go * 📚 Doc: Update custom constraints section to clarify overriding behavior * 🔥 Feature: Enhance CheckConstraint method for improved error handling * Revert "🔥 Feature: Enhance CheckConstraint method for improved error handling" This reverts commit 68e8777. * Reapply "🔥 Feature: Enhance CheckConstraint method for improved error handling" This reverts commit 9e6c8e6. * 🚨 Test: Add comprehensive tests for CheckConstraint method with various constraint scenarios * 🩹 Fix: lint error * 🩹 Fix: Update CheckConstraint method to return true for noConstraint and improve error handling * ♻️ Refactor: Remove unused CheckConstraint test cases and reorganize benchmark test cases for clarity * ♻️ Refactor: Remove outdated test cases from path_testcases_test.go and clean up CheckConstraint method in path.go * 📚 Doc: Update custom constraints section to clarify overriding behavior * 📚 Doc: Add caution note about custom constraints overriding built-in constraints in routing guide --------- Co-authored-by: Juan Calderon-Perez <[email protected]> Co-authored-by: RW <[email protected]>
1 parent bb12633 commit c5c7f86

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

docs/guide/routing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ app.Get("/:test<int>?", func(c fiber.Ctx) error {
250250

251251
Custom constraints can be added to Fiber using the `app.RegisterCustomConstraint` method. Your constraints have to be compatible with the `CustomConstraint` interface.
252252

253+
:::caution
254+
Attention, custom constraints can now override built-in constraints. If a custom constraint has the same name as a built-in constraint, the custom constraint will be used instead. This allows for more flexibility in defining route parameter constraints.
255+
:::
256+
253257
It is a good idea to add external constraints to your project once you want to add more specific rules to your routes.
254258
For example, you can add a constraint to check if a parameter is a valid ULID.
255259

path.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,25 @@ func getParamConstraintType(constraintPart string) TypeConstraint {
672672
}
673673
}
674674

675-
//nolint:errcheck // TODO: Properly check _all_ errors in here, log them & immediately return
675+
// CheckConstraint validates if a param matches the given constraint
676+
// Returns true if the param passes the constraint check, false otherwise
677+
//
678+
//nolint:errcheck // TODO: Properly check _all_ errors in here, log them or immediately return
676679
func (c *Constraint) CheckConstraint(param string) bool {
677-
var err error
678-
var num int
680+
// First check if there's a custom constraint with the same name
681+
// This allows custom constraints to override built-in constraints
682+
for _, cc := range c.customConstraints {
683+
if cc.Name() == c.Name {
684+
return cc.Execute(param, c.Data...)
685+
}
686+
}
687+
688+
var (
689+
err error
690+
num int
691+
)
679692

680-
// check data exists
693+
// Validate constraint has required data
681694
needOneData := []TypeConstraint{minLenConstraint, maxLenConstraint, lenConstraint, minConstraint, maxConstraint, datetimeConstraint, regexConstraint}
682695
needTwoData := []TypeConstraint{betweenLenConstraint, rangeConstraint}
683696

@@ -696,11 +709,7 @@ func (c *Constraint) CheckConstraint(param string) bool {
696709
// check constraints
697710
switch c.ID {
698711
case noConstraint:
699-
for _, cc := range c.customConstraints {
700-
if cc.Name() == c.Name {
701-
return cc.Execute(param, c.Data...)
702-
}
703-
}
712+
return true
704713
case intConstraint:
705714
_, err = strconv.Atoi(param)
706715
case boolConstraint:
@@ -744,27 +753,33 @@ func (c *Constraint) CheckConstraint(param string) bool {
744753
data, _ := strconv.Atoi(c.Data[0])
745754
num, err = strconv.Atoi(param)
746755

747-
if num < data {
756+
if err != nil || num < data {
748757
return false
749758
}
750759
case maxConstraint:
751760
data, _ := strconv.Atoi(c.Data[0])
752761
num, err = strconv.Atoi(param)
753762

754-
if num > data {
763+
if err != nil || num > data {
755764
return false
756765
}
757766
case rangeConstraint:
758767
data, _ := strconv.Atoi(c.Data[0])
759768
data2, _ := strconv.Atoi(c.Data[1])
760769
num, err = strconv.Atoi(param)
761770

762-
if num < data || num > data2 {
771+
if err != nil || num < data || num > data2 {
763772
return false
764773
}
765774
case datetimeConstraint:
766775
_, err = time.Parse(c.Data[0], param)
776+
if err != nil {
777+
return false
778+
}
767779
case regexConstraint:
780+
if c.RegexCompiler == nil {
781+
return false
782+
}
768783
if match := c.RegexCompiler.MatchString(param); !match {
769784
return false
770785
}

path_testcases_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,14 @@ func init() {
713713
{url: "/api/v1/", params: []string{""}, match: true},
714714
},
715715
},
716+
// Add test case for RegexCompiler == nil
717+
{
718+
pattern: "/api/v1/:param<regex(\\d+)>",
719+
testCases: []routeTestCase{
720+
{url: "/api/v1/123", params: []string{"123"}, match: true},
721+
{url: "/api/v1/abc", params: nil, match: false},
722+
},
723+
},
716724
}...,
717725
)
718726
}

0 commit comments

Comments
 (0)