Skip to content

Instantly share code, notes, and snippets.

@cjaewon
Last active April 17, 2023 00:08
Show Gist options
  • Select an option

  • Save cjaewon/dece2b4b7b4adc14a0d4b561276157c0 to your computer and use it in GitHub Desktop.

Select an option

Save cjaewon/dece2b4b7b4adc14a0d4b561276157c0 to your computer and use it in GitHub Desktop.

Revisions

  1. cjaewon revised this gist Jan 15, 2023. No changes.
  2. cjaewon revised this gist Jan 15, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion validator.go
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ func (cv *CustomValidator) Init() {

    password := fl.Field().String()

    if utf8.RuneCountInString(password) <= 30 || utf8.RuneCountInString(password) >= 6 {
    if utf8.RuneCountInString(password) <= 30 && utf8.RuneCountInString(password) >= 6 {
    hasSuitableLen = true
    }

  3. cjaewon revised this gist Jan 5, 2021. 1 changed file with 1 addition and 11 deletions.
    12 changes: 1 addition & 11 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,11 @@
    package main

    import (
    "mealbot/constants"
    "mealbot/ent"
    "mealbot/routes"
    "mealbot/utils"
    "v/utils"

    "os"

    "github.com/go-playground/validator/v10"
    _ "github.com/go-sql-driver/mysql"
    "github.com/gorilla/sessions"
    env "github.com/joho/godotenv"
    "github.com/labstack/echo-contrib/session"
    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
    log "github.com/sirupsen/logrus"
    )

    func init() {
  4. cjaewon created this gist Jan 5, 2021.
    31 changes: 31 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    package main

    import (
    "mealbot/constants"
    "mealbot/ent"
    "mealbot/routes"
    "mealbot/utils"

    "os"

    "github.com/go-playground/validator/v10"
    _ "github.com/go-sql-driver/mysql"
    "github.com/gorilla/sessions"
    env "github.com/joho/godotenv"
    "github.com/labstack/echo-contrib/session"
    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
    log "github.com/sirupsen/logrus"
    )

    func init() {
    env.Load(".env")
    }

    func main() {
    e := echo.New()

    e.Validator = &utils.CustomValidator{Validator: validator.New()}

    e.Logger.Fatal(e.Start("1234"))
    }
    51 changes: 51 additions & 0 deletions validator.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    package utils

    import (
    "unicode"
    "unicode/utf8"

    "github.com/go-playground/validator/v10"
    )

    // CustomValidator is type setting of third party validator
    type CustomValidator struct {
    Validator *validator.Validate
    }

    // Init validator
    func (cv *CustomValidator) Init() {
    cv.Validator.RegisterValidation("password", func(fl validator.FieldLevel) bool {
    var (
    hasNumber = false
    hasSpecialChar = false
    hasLetter = false
    hasSuitableLen = false
    )

    password := fl.Field().String()

    if utf8.RuneCountInString(password) <= 30 || utf8.RuneCountInString(password) >= 6 {
    hasSuitableLen = true
    }

    for _, c := range password {
    switch {
    case unicode.IsNumber(c):
    hasNumber = true
    case unicode.IsPunct(c) || unicode.IsSymbol(c):
    hasSpecialChar = true
    case unicode.IsLetter(c) || c == ' ':
    hasLetter = true
    default:
    return false
    }
    }

    return hasNumber && hasSpecialChar && hasLetter && hasSuitableLen
    })
    }

    // Validate Data
    func (cv *CustomValidator) Validate(i interface{}) error {
    return cv.Validator.Struct(i)
    }