Skip to content

Commit

Permalink
👔 up: update some code style and error message format logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 12, 2024
1 parent 050296c commit 637f9df
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
- '**.go'
- '**.yml'

permissions:
contents: read

jobs:

test:
Expand Down Expand Up @@ -45,7 +48,7 @@ jobs:
parallel: true

lint:
name: linter
name: code linter
runs-on: ubuntu-latest
steps:
- name: Check out code
Expand All @@ -55,7 +58,7 @@ jobs:
uses: WillAbides/setup-go-faster@v1.13.0
timeout-minutes: 3
with:
go-version: 1.19
go-version: *

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
1 change: 0 additions & 1 deletion data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,6 @@ func (d *StructData) loadMessagesFromTag(trans *Translator, field, vRule, vMsg s
if vName == "" {
// eg `validate:"required|date"`
vNames = []string{vRule}

if strings.ContainsRune(vRule, '|') {
vNames = strings.Split(vRule, "|")
}
Expand Down
20 changes: 17 additions & 3 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ var (
// Example:
//
// {
// "field": {validator: message, validator1: message1}
// "field": {
// "required": "error message",
// "min_len": "error message1"
// }
// }
type Errors map[string]MS

Expand All @@ -44,7 +47,7 @@ func (es Errors) Empty() bool {
return len(es) == 0
}

// Add a error for the field
// Add an error for the field
func (es Errors) Add(field, validator, message string) {
if _, ok := es[field]; ok {
es[field][validator] = message
Expand Down Expand Up @@ -84,7 +87,6 @@ func (es Errors) Random() string {
// All get all errors data
func (es Errors) All() map[string]map[string]string {
mm := make(map[string]map[string]string, len(es))

for field, fe := range es {
mm[field] = fe
}
Expand All @@ -104,8 +106,20 @@ func (es Errors) Error() string {

// String errors to string
func (es Errors) String() string {
ln := len(es)
if ln == 0 {
return ""
}

buf := new(bytes.Buffer)
for field, fe := range es {
// only one error, return simple format: "field: message"
if ln == 1 && len(fe) == 1 {
for _, msg := range fe {
return fmt.Sprintf("%s: %s", field, msg)
}
}

buf.WriteString(fmt.Sprintf("%s:\n%s\n", field, fe.String()))
}

Expand Down
3 changes: 2 additions & 1 deletion messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ func TestErrorsBasic(t *testing.T) {

assert.True(t, es.Empty())
assert.Equal(t, "", es.One())
assert.Equal(t, "", es.String())
assert.Nil(t, es.ErrOrNil())

es.Add("field", "required", "error msg0")
assert.Len(t, es, 1)
assert.Equal(t, "error msg0", es.One())
assert.Equal(t, "error msg0", es.FieldOne("field"))
assert.Equal(t, "field:\n required: error msg0", es.String())
assert.Equal(t, "field: error msg0", es.String())

es.Add("field2", "min", "error msg2")
assert.Contains(t, fmt.Sprintf("%v", es.All()), "field:map[required:error msg0]")
Expand Down
3 changes: 3 additions & 0 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func init() {
const (
RuleRequired = "required"
RuleRegexp = "regexp"
// RuleSafe means skip validate this field
RuleSafe = "safe"
RuleSafe1 = "-"
)

// validator func reflect.Value map
Expand Down
2 changes: 1 addition & 1 deletion rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (v *Validation) StringRule(field, rule string, filterRule ...string) *Valid
case "default":
v.SetDefValue(field, list[1])
// eg 'regex:\d{4,6}' dont need split args. args is "\d{4,6}"
case "regexp":
case RuleRegexp:
v.AddRule(field, validator, list[1])
// some special validator. need merge args to one.
case "enum", "notIn":
Expand Down
2 changes: 1 addition & 1 deletion validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ type value struct {
// - name: the validator name. eg: "required", "min"
func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok bool) {
// "-" OR "safe" mark field value always is safe.
if name == "-" || name == "safe" {
if name == RuleSafe1 || name == RuleSafe {
return true
}

Expand Down

0 comments on commit 637f9df

Please sign in to comment.