mirror of https://github.com/gofiber/fiber.git
📚 Doc: Add caution note about custom constraints overriding built-in constraints in routing guide
parent
f16644cd9f
commit
87203a5d0d
|
@ -250,7 +250,9 @@ app.Get("/:test<int>?", func(c fiber.Ctx) error {
|
||||||
|
|
||||||
Custom constraints can be added to Fiber using the `app.RegisterCustomConstraint` method. Your constraints have to be compatible with the `CustomConstraint` interface.
|
Custom constraints can be added to Fiber using the `app.RegisterCustomConstraint` method. Your constraints have to be compatible with the `CustomConstraint` interface.
|
||||||
|
|
||||||
|
:::caution
|
||||||
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.
|
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.
|
||||||
|
:::
|
||||||
|
|
||||||
It is a good idea to add external constraints to your project once you want to add more specific rules to your routes.
|
It is a good idea to add external constraints to your project once you want to add more specific rules to your routes.
|
||||||
For example, you can add a constraint to check if a parameter is a valid ULID.
|
For example, you can add a constraint to check if a parameter is a valid ULID.
|
||||||
|
|
|
@ -55,14 +55,14 @@ func New(config ...Config) fiber.Handler {
|
||||||
// - context.Context: Retrieves request ID from context values
|
// - context.Context: Retrieves request ID from context values
|
||||||
func FromContext(c any) string {
|
func FromContext(c any) string {
|
||||||
switch ctx := c.(type) {
|
switch ctx := c.(type) {
|
||||||
case context.Context:
|
|
||||||
if rid, ok := ctx.Value(requestIDKey).(string); ok {
|
|
||||||
return rid
|
|
||||||
}
|
|
||||||
case fiber.Ctx:
|
case fiber.Ctx:
|
||||||
if rid, ok := ctx.Locals(requestIDKey).(string); ok {
|
if rid, ok := ctx.Locals(requestIDKey).(string); ok {
|
||||||
return rid
|
return rid
|
||||||
}
|
}
|
||||||
|
case context.Context:
|
||||||
|
if rid, ok := ctx.Value(requestIDKey).(string); ok {
|
||||||
|
return rid
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
log.Errorf("Unsupported context type: %T. Expected fiber.Ctx or context.Context", c)
|
log.Errorf("Unsupported context type: %T. Expected fiber.Ctx or context.Context", c)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue