1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-05 03:07:38 +00:00

add a new 'uuid' path parameter type e.g. /{id:uuid}

This commit is contained in:
Gerasimos (Makis) Maropoulos
2021-02-12 17:12:23 +02:00
parent 590e999688
commit 5fe233278a
4 changed files with 58 additions and 0 deletions

View File

@@ -5,6 +5,8 @@ import (
"strings"
"github.com/kataras/iris/v12/macro/interpreter/ast"
"github.com/google/uuid"
)
var (
@@ -402,6 +404,16 @@ var (
// Should be living in the latest path segment of a route path.
Path = NewMacro("path", "", false, true, nil)
// UUID string type for validating a uuidv4 (and v1) path parameter
// Read more at: https://tools.ietf.org/html/rfc4122
UUID = NewMacro("uuid", "uuidv4", false, false, func(paramValue string) (interface{}, bool) {
_, err := uuid.Parse(paramValue) // this is x10+ times faster than regexp.
if err != nil {
return nil, false
}
return paramValue, true
})
// Defaults contains the defaults macro and parameters types for the router.
//
// Read https://github.com/kataras/iris/tree/master/_examples/routing/macros for more details.
@@ -421,6 +433,7 @@ var (
Alphabetical,
File,
Path,
UUID,
}
)