From 88358e6a3a0ccbb441a22ad36ff8ea79c6b4aeaa Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 2 Feb 2024 08:57:40 +0000 Subject: [PATCH] Add docs from https://github.com/gofiber/fiber/commit/2a2801f --- docs/core/api/app.md | 10 +++++++ docs/core/guide/routing.md | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/docs/core/api/app.md b/docs/core/api/app.md index 29022a3104d..2e58a426d1a 100644 --- a/docs/core/api/app.md +++ b/docs/core/api/app.md @@ -617,6 +617,16 @@ ln = tls.NewListener(ln, &tls.Config{Certificates: []tls.Certificate{cer}}) app.Listener(ln) ``` +## RegisterCustomConstraint + +RegisterCustomConstraint allows to register custom constraint. + +```go title="Signature" +func (app *App) RegisterCustomConstraint(constraint CustomConstraint) +``` + +See [Custom Constraint](../guide/routing.md#custom-constraint) section for more information. + ## Test Testing your application is done with the **Test** method. Use this method for creating `_test.go` files or when you need to debug your routing logic. The default timeout is `1s` if you want to disable a timeout altogether, pass `-1` as a second argument. diff --git a/docs/core/guide/routing.md b/docs/core/guide/routing.md index 31d187aeb58..70d746a317f 100644 --- a/docs/core/guide/routing.md +++ b/docs/core/guide/routing.md @@ -240,6 +240,59 @@ app.Get("/:test?", func(c fiber.Ctx) error { // Cannot GET /7.0 ``` +**Custom Constraint Example** + +Custom constraints can be added to Fiber using the `app.RegisterCustomConstraint` method. Your constraints have to be compatible with the `CustomConstraint` interface. + +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. + +```go +// CustomConstraint is an interface for custom constraints +type CustomConstraint interface { + // Name returns the name of the constraint. + // This name is used in the constraint matching. + Name() string + + // Execute executes the constraint. + // It returns true if the constraint is matched and right. + // param is the parameter value to check. + // args are the constraint arguments. + Execute(param string, args ...string) bool +} +``` + +You can check the example below: + +```go +type UlidConstraint struct { + fiber.CustomConstraint +} + +func (*UlidConstraint) Name() string { + return "ulid" +} + +func (*UlidConstraint) Execute(param string, args ...string) bool { + _, err := ulid.Parse(param) + return err == nil +} + +func main() { + app := fiber.New() + app.RegisterCustomConstraint(&UlidConstraint{}) + + app.Get("/login/:id", func(c fiber.Ctx) error { + return c.SendString("...") + }) + + app.Listen(":3000") + + // /login/01HK7H9ZE5BFMK348CPYP14S0Z -> 200 + // /login/12345 -> 404 +} +``` + ## Middleware Functions that are designed to make changes to the request or response are called **middleware functions**. The [Next](../api/ctx.md#next) is a **Fiber** router function, when called, executes the **next** function that **matches** the current route.