Skip to content

Commit

Permalink
fix: print only important errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jan 10, 2025
1 parent a78a3aa commit 8308a9b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Script struct {

Skip interface{} `json:"skip,omitempty" jsonschema:"oneof_type=boolean;array" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Only interface{} `json:"only,omitempty" jsonschema:"oneof_type=boolean;array" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"`
Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
Tags []string `json:"tags,omitempty" jsonschema:"oneof_type=string;array" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"`
Priority int `json:"priority,omitempty" mapstructure:"priority" toml:"priority,omitempty" yaml:",omitempty"`

Expand Down
45 changes: 27 additions & 18 deletions internal/lefthook/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,56 @@ func Validate(opts *Options) error {
details := result.ToList()
logValidationErrors(0, *details)

return errors.New("Validation failed: main config")
return errors.New("Validation failed for main config")
}

result = schema.Validate(secondary.Raw())
if !result.IsValid() {
details := result.ToList()
logValidationErrors(0, *details)

return errors.New("Validation failed: secondary config")
return errors.New("Validation failed for secondary config")
}

log.Info("All good")
return nil
}

func logValidationErrors(indent int, details jsonschema.List) {
if len(details.Details) == 0 && details.Valid {
if details.Valid {
return
}

if len(details.InstanceLocation) > 0 {
var errors []string
if len(details.Errors) > 0 {
for key, value := range details.Errors {
errors = append(errors, "["+key+"] "+value)
}
}

key := strings.Repeat(" ", indent) + strings.ReplaceAll(details.InstanceLocation, "/", "") + ":"

if len(errors) == 0 {
key = log.Gray(key)
} else {
key = log.Yellow(key)
}
logDetail(indent, details)

log.Info(key, log.Red(strings.Join(errors, ",")))
indent += 2
}

for _, d := range details.Details {
logValidationErrors(indent, d)
}
}

func logDetail(indent int, details jsonschema.List) {
var errors []string
if len(details.Errors) > 0 {
for _, err := range details.Errors {
errors = append(errors, err)
}
}

option := strings.Repeat(" ", indent) + strings.TrimLeft(details.InstanceLocation, "/") + ":"

if len(errors) == 0 {
option = log.Gray(option)
} else {
option = log.Yellow(option)
}

if len(details.Details) > 0 {
log.Info(option)
} else {
log.Info(option, log.Red(strings.Join(errors, ",")))
}
}
13 changes: 10 additions & 3 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,17 @@
]
},
"tags": {
"oneOf": [
{
"type": "string"
},
{
"type": "array"
}
],
"items": {
"type": "string"
},
"type": "array"
}
},
"env": {
"additionalProperties": {
Expand Down Expand Up @@ -385,7 +392,7 @@
"type": "object"
}
},
"$comment": "Last updated on 2025.01.09.",
"$comment": "Last updated on 2025.01.10.",
"properties": {
"min_version": {
"type": "string",
Expand Down

0 comments on commit 8308a9b

Please sign in to comment.