feat: validate user input for entity names
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Allowed special characters
|
||||
var allowedSpecialChars = map[rune]bool{
|
||||
'@': true,
|
||||
'^': true,
|
||||
'~': true,
|
||||
'-': true,
|
||||
'_': true,
|
||||
}
|
||||
|
||||
func ValidateTypeName(input string) (string, bool) {
|
||||
// Trim leading and trailing spaces
|
||||
trimmed := strings.TrimSpace(input)
|
||||
|
||||
// If the string is empty after trimming, return false
|
||||
if len(trimmed) == 0 {
|
||||
return "", false
|
||||
}
|
||||
|
||||
runes := []rune(trimmed)
|
||||
|
||||
if len(runes) > 30 {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Dash cannot be at the beginning or end
|
||||
if runes[0] == '-' || runes[len(runes)-1] == '-' {
|
||||
return "", false
|
||||
}
|
||||
|
||||
for _, r := range runes {
|
||||
// Check if the character is a whitespace, which is not allowed
|
||||
if unicode.IsSpace(r) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Letters (including any alphabet) and digits are allowed
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Combining marks (accents) are allowed
|
||||
if unicode.IsMark(r) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check for allowed special characters
|
||||
if allowedSpecialChars[r] {
|
||||
continue
|
||||
}
|
||||
|
||||
// If any other character is encountered, return false
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Return the trimmed string and true if all conditions are met
|
||||
return trimmed, true
|
||||
}
|
||||
Reference in New Issue
Block a user