mirror of
https://github.com/nikdoof/pocket-id.git
synced 2025-12-14 15:22:18 +00:00
feat: add validation to custom claim input
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
type CustomClaimDto struct {
|
type CustomClaimDto struct {
|
||||||
Key string `json:"key" binding:"required,max=20"`
|
Key string `json:"key"`
|
||||||
Value string `json:"value" binding:"required,max=10000"`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomClaimCreateDto = CustomClaimDto
|
type CustomClaimCreateDto struct {
|
||||||
|
Key string `json:"key" binding:"required,claimKey"`
|
||||||
|
Value string `json:"value" binding:"required"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,8 +29,15 @@ var validateUsername validator.Func = func(fl validator.FieldLevel) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var validateUserGroupName validator.Func = func(fl validator.FieldLevel) bool {
|
var validateUserGroupName validator.Func = func(fl validator.FieldLevel) bool {
|
||||||
// [a-z0-9_] : The group name can only contain lowercase letters, numbers, and underscores
|
// The string can only contain lowercase letters, numbers, and underscores
|
||||||
regex := "^[a-z0-9_]+$"
|
regex := "^[a-z0-9_]*$"
|
||||||
|
matched, _ := regexp.MatchString(regex, fl.Field().String())
|
||||||
|
return matched
|
||||||
|
}
|
||||||
|
|
||||||
|
var validateClaimKey validator.Func = func(fl validator.FieldLevel) bool {
|
||||||
|
// The string can only contain letters and numbers
|
||||||
|
regex := "^[A-Za-z0-9]*$"
|
||||||
matched, _ := regexp.MatchString(regex, fl.Field().String())
|
matched, _ := regexp.MatchString(regex, fl.Field().String())
|
||||||
return matched
|
return matched
|
||||||
}
|
}
|
||||||
@@ -52,4 +59,10 @@ func init() {
|
|||||||
log.Fatalf("Failed to register custom validation: %v", err)
|
log.Fatalf("Failed to register custom validation: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||||
|
if err := v.RegisterValidation("claimKey", validateClaimKey); err != nil {
|
||||||
|
log.Fatalf("Failed to register custom validation: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/stonith404/pocket-id/backend/internal/common"
|
"github.com/stonith404/pocket-id/backend/internal/common"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -54,7 +53,6 @@ func (m *ErrorHandlerMiddleware) Add() gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(err)
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Something went wrong"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Something went wrong"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
let filteredSuggestions: string[] = $state(suggestions.slice(0, suggestionLimit));
|
let filteredSuggestions: string[] = $state(suggestions.slice(0, suggestionLimit));
|
||||||
let selectedIndex = $state(-1);
|
let selectedIndex = $state(-1);
|
||||||
|
let keyError: string | undefined = $state();
|
||||||
|
|
||||||
let isInputFocused = $state(false);
|
let isInputFocused = $state(false);
|
||||||
|
|
||||||
@@ -25,6 +26,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleOnInput() {
|
function handleOnInput() {
|
||||||
|
if (value.length > 0 && !/^[A-Za-z0-9]*$/.test(value)) {
|
||||||
|
keyError = 'Only alphanumeric characters are allowed';
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
keyError = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
filteredSuggestions = suggestions
|
filteredSuggestions = suggestions
|
||||||
.filter((s) => s.includes(value.toLowerCase()))
|
.filter((s) => s.includes(value.toLowerCase()))
|
||||||
.slice(0, suggestionLimit);
|
.slice(0, suggestionLimit);
|
||||||
@@ -75,6 +83,9 @@
|
|||||||
onfocus={() => (isInputFocused = true)}
|
onfocus={() => (isInputFocused = true)}
|
||||||
onblur={() => (isInputFocused = false)}
|
onblur={() => (isInputFocused = false)}
|
||||||
/>
|
/>
|
||||||
|
{#if keyError}
|
||||||
|
<p class="mt-1 text-sm text-red-500">{keyError}</p>
|
||||||
|
{/if}
|
||||||
<Popover.Root
|
<Popover.Root
|
||||||
open={isOpen}
|
open={isOpen}
|
||||||
disableFocusTrap
|
disableFocusTrap
|
||||||
|
|||||||
Reference in New Issue
Block a user