1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +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

@@ -419,6 +419,24 @@ func TestPathEvaluatorRaw(t *testing.T) {
}
}
func TestUUIDEvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
input string
}{
{true, "978ad967-5fad-4c82-af99-580097ace662"}, // v4
{true, "c7067f9c-6d43-11eb-9439-0242ac130002"}, // v1
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{false, "32321"}, // 2
{false, "main.css"}, // 3
{false, "/assets/main.css"}, // 4
}
for i, tt := range tests {
testEvaluatorRaw(t, UUID, tt.input, reflect.String, tt.pass, i)
}
}
func TestConvertBuilderFunc(t *testing.T) {
fn := func(min uint64, slice []string) func(string) bool {
return func(paramValue string) bool {

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,
}
)