Skip to content

Commit

Permalink
Add docs from gofiber/fiber@2a2801f
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Feb 2, 2024
1 parent 0dfbe48 commit 88358e6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/core/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions docs/core/guide/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,59 @@ app.Get("/:test<int>?", 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<ulid>", 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.
Expand Down

0 comments on commit 88358e6

Please sign in to comment.