Skip to content

Commit

Permalink
Merge pull request beego#4017 from guhan121/fix#4000
Browse files Browse the repository at this point in the history
Prohibit multiple calls SetDefaultMessage.
  • Loading branch information
flycash authored Jun 26, 2020
2 parents 469f2c2 + e725192 commit 8039cc8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions validation/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package validation

import (
"fmt"
"github.com/astaxie/beego/logs"
"reflect"
"regexp"
"strings"
"sync"
"time"
"unicode/utf8"
)
Expand Down Expand Up @@ -57,6 +59,8 @@ var MessageTmpls = map[string]string{
"ZipCode": "Must be valid zipcode",
}

var once sync.Once

// SetDefaultMessage set default messages
// if not set, the default messages are
// "Required": "Can not be empty",
Expand Down Expand Up @@ -84,9 +88,12 @@ func SetDefaultMessage(msg map[string]string) {
return
}

for name := range msg {
MessageTmpls[name] = msg[name]
}
once.Do(func() {
for name := range msg {
MessageTmpls[name] = msg[name]
}
})
logs.Warn(`you must SetDefaultMessage at once`)
}

// Validator interface
Expand Down

0 comments on commit 8039cc8

Please sign in to comment.