Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions validation/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"strings"

contractstranslation "github.com/goravel/framework/contracts/translation"
contractsvalidation "github.com/goravel/framework/contracts/validation"
)

Expand All @@ -18,6 +19,7 @@ type Engine struct {
customRules map[string]contractsvalidation.Rule
messages map[string]string
attributes map[string]string
translator contractstranslation.Translator
errors *Errors
excludes map[string]bool
distinctValues map[string]map[string]bool // For tracking distinct values
Expand All @@ -29,6 +31,7 @@ type engineOptions struct {
customRules map[string]contractsvalidation.Rule
messages map[string]string
attributes map[string]string
translator contractstranslation.Translator
}

// NewEngine creates a new validation engine.
Expand All @@ -40,6 +43,7 @@ func NewEngine(ctx context.Context, data *DataBag, rules map[string][]ParsedRule
customRules: opts.customRules,
messages: opts.messages,
attributes: opts.attributes,
translator: opts.translator,
errors: NewErrors(),
excludes: make(map[string]bool),
distinctValues: make(map[string]map[string]bool),
Expand Down Expand Up @@ -184,7 +188,7 @@ func (e *Engine) validateField(field string, fieldRules []ParsedRule, allRules m

if !passed {
attrType := getAttributeType(field, value, allRules)
msg := e.formatErrorMessage(field, rule, attrType)
msg := e.formatErrorMessage(field, rule, attrType, value)
e.errors.Add(field, rule.Name, msg)
}

Expand Down Expand Up @@ -291,8 +295,8 @@ func (e *Engine) trackDistinct(field string, value any) bool {
}

// formatErrorMessage creates the error message for a rule failure.
func (e *Engine) formatErrorMessage(field string, rule ParsedRule, attrType string) string {
msg := getMessage(field, rule.Name, e.messages, attrType)
func (e *Engine) formatErrorMessage(field string, rule ParsedRule, attrType string, value any) string {
msg := getMessage(field, rule.Name, e.messages, attrType, e.translator)
if _, hasFieldRuleMessage := e.messages[field+"."+rule.Name]; !hasFieldRuleMessage {
if _, hasRuleMessage := e.messages[rule.Name]; !hasRuleMessage {
if customRule, ok := e.customRules[rule.Name]; ok {
Expand All @@ -305,6 +309,14 @@ func (e *Engine) formatErrorMessage(field string, rule ParsedRule, attrType stri
":attribute": getDisplayableAttribute(field, e.attributes),
}

// Add placeholders for custom rules: :value and :option0, :option1, etc.
if _, ok := e.customRules[rule.Name]; ok {
replacements[":value"] = fmt.Sprintf("%v", value)
for i, param := range rule.Parameters {
replacements[fmt.Sprintf(":option%d", i)] = param
}
}

// Add parameter-specific replacements
switch rule.Name {
case "min", "min_digits":
Expand Down
57 changes: 50 additions & 7 deletions validation/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func TestEngine_FormatErrorMessage(t *testing.T) {
attributes: map[string]string{"name": "Full Name"},
})

msg := engine.formatErrorMessage("name", ParsedRule{Name: "my_rule"}, "string")
msg := engine.formatErrorMessage("name", ParsedRule{Name: "my_rule"}, "string", nil)
assert.Equal(t, "The Full Name is bad.", msg)
})

Expand All @@ -460,7 +460,7 @@ func TestEngine_FormatErrorMessage(t *testing.T) {
},
})

msg := engine.formatErrorMessage("f", ParsedRule{Name: "custom_exists"}, "string")
msg := engine.formatErrorMessage("f", ParsedRule{Name: "custom_exists"}, "string", nil)
assert.Equal(t, "custom_exists failed for f", msg)
})

Expand All @@ -475,7 +475,7 @@ func TestEngine_FormatErrorMessage(t *testing.T) {
},
})

msg := engine.formatErrorMessage("f", ParsedRule{Name: "custom_exists"}, "string")
msg := engine.formatErrorMessage("f", ParsedRule{Name: "custom_exists"}, "string", nil)
assert.Equal(t, "custom_exists failed", msg)
})

Expand All @@ -487,7 +487,7 @@ func TestEngine_FormatErrorMessage(t *testing.T) {
},
})

msg := engine.formatErrorMessage("name", ParsedRule{Name: "required"}, "string")
msg := engine.formatErrorMessage("name", ParsedRule{Name: "required"}, "string", nil)
assert.Equal(t, "Please enter your name.", msg)
})

Expand All @@ -498,7 +498,7 @@ func TestEngine_FormatErrorMessage(t *testing.T) {
msg := engine.formatErrorMessage("name", ParsedRule{
Name: "min",
Parameters: []string{"3"},
}, "string")
}, "string", nil)
assert.Equal(t, "The name field must be at least 3 characters.", msg)
})

Expand All @@ -509,7 +509,7 @@ func TestEngine_FormatErrorMessage(t *testing.T) {
msg := engine.formatErrorMessage("age", ParsedRule{
Name: "between",
Parameters: []string{"1", "100"},
}, "numeric")
}, "numeric", nil)
assert.Equal(t, "The age field must be between 1 and 100.", msg)
})

Expand All @@ -519,9 +519,52 @@ func TestEngine_FormatErrorMessage(t *testing.T) {
attributes: map[string]string{"first_name": "First Name"},
})

msg := engine.formatErrorMessage("first_name", ParsedRule{Name: "required"}, "string")
msg := engine.formatErrorMessage("first_name", ParsedRule{Name: "required"}, "string", nil)
assert.Equal(t, "The First Name field is required.", msg)
})

t.Run("custom rule with value placeholder", func(t *testing.T) {
bag, _ := NewDataBag(map[string]any{})
engine := NewEngine(context.Background(), bag, nil, engineOptions{
customRules: map[string]contractsvalidation.Rule{
"my_rule": newAlwaysFailRule("my_rule", "The :attribute value :value is invalid."),
},
})

msg := engine.formatErrorMessage("name", ParsedRule{Name: "my_rule"}, "string", "hello")
assert.Equal(t, "The name value hello is invalid.", msg)
})

t.Run("custom rule with option placeholders", func(t *testing.T) {
bag, _ := NewDataBag(map[string]any{})
engine := NewEngine(context.Background(), bag, nil, engineOptions{
customRules: map[string]contractsvalidation.Rule{
"my_rule": newAlwaysFailRule("my_rule", "The :attribute must be between :option0 and :option1."),
},
})

msg := engine.formatErrorMessage("age", ParsedRule{
Name: "my_rule",
Parameters: []string{"18", "65"},
}, "string", 10)
assert.Equal(t, "The age must be between 18 and 65.", msg)
})

t.Run("custom rule with combined placeholders", func(t *testing.T) {
bag, _ := NewDataBag(map[string]any{})
engine := NewEngine(context.Background(), bag, nil, engineOptions{
customRules: map[string]contractsvalidation.Rule{
"my_rule": newAlwaysFailRule("my_rule", "The :attribute got :value, expected :option0."),
},
attributes: map[string]string{"score": "Score"},
})

msg := engine.formatErrorMessage("score", ParsedRule{
Name: "my_rule",
Parameters: []string{"100"},
}, "string", 42)
assert.Equal(t, "The Score got 42, expected 100.", msg)
})
}

func TestEngine_ExecuteRule(t *testing.T) {
Expand Down
46 changes: 46 additions & 0 deletions validation/lang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package validation

import (
"embed"
"encoding/json"
"fmt"
"io/fs"
)

//go:embed lang
var LangFS embed.FS

func init() {
data, err := fs.ReadFile(LangFS, "lang/en/validation.json")
if err != nil {
panic(fmt.Sprintf("validation: failed to read embedded lang/en/validation.json: %v", err))
Comment thread
h2zi marked this conversation as resolved.
}

var raw map[string]any
if err := json.Unmarshal(data, &raw); err != nil {
panic(fmt.Sprintf("validation: failed to parse embedded lang/en/validation.json: %v", err))
}

defaultMessages = flattenMessages("", raw)
}

// flattenMessages flattens a nested JSON map into dot-separated keys.
// e.g. {"min": {"string": "...", "numeric": "..."}} → {"min.string": "...", "min.numeric": "..."}
func flattenMessages(prefix string, m map[string]any) map[string]string {
result := make(map[string]string)
for k, v := range m {
key := k
if prefix != "" {
key = prefix + "." + k
}
switch val := v.(type) {
case string:
result[key] = val
case map[string]any:
for fk, fv := range flattenMessages(key, val) {
result[fk] = fv
}
}
}
return result
}
176 changes: 176 additions & 0 deletions validation/lang/en/validation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"accepted": "The :attribute field must be accepted.",
"accepted_if": "The :attribute field must be accepted when :other is :value.",
"active_url": "The :attribute field must be a valid URL.",
"after": "The :attribute field must be a date after :date.",
"after_or_equal": "The :attribute field must be a date after or equal to :date.",
"alpha": "The :attribute field must only contain letters.",
"alpha_dash": "The :attribute field must only contain letters, numbers, dashes, and underscores.",
"alpha_num": "The :attribute field must only contain letters and numbers.",
"array": "The :attribute field must be an array.",
"ascii": "The :attribute field must only contain single-byte alphanumeric characters and symbols.",
"before": "The :attribute field must be a date before :date.",
"before_or_equal": "The :attribute field must be a date before or equal to :date.",
"between": {
"array": "The :attribute field must have between :min and :max items.",
"file": "The :attribute field must be between :min and :max kilobytes.",
"numeric": "The :attribute field must be between :min and :max.",
"string": "The :attribute field must be between :min and :max characters."
},
"bool": "The :attribute field must be true or false.",
"boolean": "The :attribute field must be true or false.",
"confirmed": "The :attribute field confirmation does not match.",
"contains": "The :attribute field is missing a required value.",
"date": "The :attribute field must be a valid date.",
"date_equals": "The :attribute field must be a date equal to :date.",
"date_format": "The :attribute field must match the format :format.",
"decimal": "The :attribute field must have :decimal decimal places.",
"declined": "The :attribute field must be declined.",
"declined_if": "The :attribute field must be declined when :other is :value.",
"different": "The :attribute field and :other must be different.",
"digits": "The :attribute field must be :digits digits.",
"digits_between": "The :attribute field must be between :min and :max digits.",
"dimensions": "The :attribute field has invalid image dimensions.",
"distinct": "The :attribute field has a duplicate value.",
"doesnt_contain": "The :attribute field must not contain any of the following: :values.",
"doesnt_end_with": "The :attribute field must not end with one of the following: :values.",
"doesnt_start_with": "The :attribute field must not start with one of the following: :values.",
"email": "The :attribute field must be a valid email address.",
"encoding": "The :attribute field must be encoded as :values.",
"ends_with": "The :attribute field must end with one of the following: :values.",
"eq": "The :attribute field must be equal to :value.",
"exclude": "The :attribute field is excluded.",
"exclude_if": "The :attribute field is excluded when :other is :value.",
"exclude_unless": "The :attribute field is excluded unless :other is :value.",
"exclude_with": "The :attribute field is excluded when :values is present.",
"exclude_without": "The :attribute field is excluded when :values is not present.",
"exists": "The :attribute does not exist.",
"extensions": "The :attribute field must have one of the following extensions: :values.",
"file": "The :attribute field must be a file.",
"filled": "The :attribute field must have a value.",
"float": "The :attribute field must be a float.",
"gt": {
"array": "The :attribute field must have more than :value items.",
"file": "The :attribute field must be greater than :value kilobytes.",
"numeric": "The :attribute field must be greater than :value.",
"string": "The :attribute field must be greater than :value characters."
},
"gte": {
"array": "The :attribute field must have :value items or more.",
"file": "The :attribute field must be greater than or equal to :value kilobytes.",
"numeric": "The :attribute field must be greater than or equal to :value.",
"string": "The :attribute field must be greater than or equal to :value characters."
},
"hex_color": "The :attribute field must be a valid hexadecimal color.",
"image": "The :attribute field must be an image.",
"in": "The selected :attribute is invalid.",
"in_array": "The :attribute field must exist in :other.",
"in_array_keys": "The :attribute field must contain at least one of the specified keys.",
"int": "The :attribute field must be an integer.",
"integer": "The :attribute field must be an integer.",
"ip": "The :attribute field must be a valid IP address.",
"ipv4": "The :attribute field must be a valid IPv4 address.",
"ipv6": "The :attribute field must be a valid IPv6 address.",
"json": "The :attribute field must be a valid JSON string.",
"list": "The :attribute field must be a list.",
"lowercase": "The :attribute field must be lowercase.",
"lt": {
"array": "The :attribute field must have less than :value items.",
"file": "The :attribute field must be less than :value kilobytes.",
"numeric": "The :attribute field must be less than :value.",
"string": "The :attribute field must be less than :value characters."
},
"lte": {
"array": "The :attribute field must not have more than :value items.",
"file": "The :attribute field must be less than or equal to :value kilobytes.",
"numeric": "The :attribute field must be less than or equal to :value.",
"string": "The :attribute field must be less than or equal to :value characters."
},
"mac": "The :attribute field must be a valid MAC address.",
"mac_address": "The :attribute field must be a valid MAC address.",
"map": "The :attribute field must be a map.",
"max": {
"array": "The :attribute field must not have more than :max items.",
"file": "The :attribute field must not be greater than :max kilobytes.",
"numeric": "The :attribute field must not be greater than :max.",
"string": "The :attribute field must not be greater than :max characters."
},
"max_digits": "The :attribute field must not have more than :max digits.",
"mimes": "The :attribute field must be a file of type: :values.",
"mimetypes": "The :attribute field must be a file of type: :values.",
"min": {
"array": "The :attribute field must have at least :min items.",
"file": "The :attribute field must be at least :min kilobytes.",
"numeric": "The :attribute field must be at least :min.",
"string": "The :attribute field must be at least :min characters."
},
"min_digits": "The :attribute field must have at least :min digits.",
"missing": "The :attribute field must be missing.",
"missing_if": "The :attribute field must be missing when :other is :value.",
"missing_unless": "The :attribute field must be missing unless :other is :value.",
"missing_with": "The :attribute field must be missing when :values is present.",
"missing_with_all": "The :attribute field must be missing when :values are present.",
"multiple_of": "The :attribute field must be a multiple of :value.",
"ne": "The :attribute field must not be equal to :value.",
"not_in": "The selected :attribute is invalid.",
"not_regex": "The :attribute field format is invalid.",
"numeric": "The :attribute field must be a number.",
"present": "The :attribute field must be present.",
"present_if": "The :attribute field must be present when :other is :value.",
"present_unless": "The :attribute field must be present unless :other is :value.",
"present_with": "The :attribute field must be present when :values is present.",
"present_with_all": "The :attribute field must be present when :values are present.",
"prohibited": "The :attribute field is prohibited.",
"prohibited_if": "The :attribute field is prohibited when :other is :value.",
"prohibited_if_accepted": "The :attribute field is prohibited when :other is accepted.",
"prohibited_if_declined": "The :attribute field is prohibited when :other is declined.",
"prohibited_unless": "The :attribute field is prohibited unless :other is in :values.",
"prohibits": "The :attribute field prohibits :other from being present.",
"regex": "The :attribute field format is invalid.",
"required": "The :attribute field is required.",
"required_array_keys": "The :attribute field must contain entries for: :values.",
"required_if": "The :attribute field is required when :other is :value.",
"required_if_accepted": "The :attribute field is required when :other is accepted.",
"required_if_declined": "The :attribute field is required when :other is declined.",
"required_unless": "The :attribute field is required unless :other is in :values.",
"required_with": "The :attribute field is required when :values is present.",
"required_with_all": "The :attribute field is required when :values are present.",
"required_without": "The :attribute field is required when :values is not present.",
"required_without_all": "The :attribute field is required when none of :values are present.",
"same": "The :attribute field must match :other.",
"size": {
"array": "The :attribute field must contain :size items.",
"file": "The :attribute field must be :size kilobytes.",
"numeric": "The :attribute field must be :size.",
"string": "The :attribute field must be :size characters."
},
"slice": "The :attribute field must be a slice.",
"starts_with": "The :attribute field must start with one of the following: :values.",
"string": "The :attribute field must be a string.",
"timezone": "The :attribute field must be a valid timezone.",
"uint": "The :attribute field must be a positive integer.",
"ulid": "The :attribute field must be a valid ULID.",
"unique": "The :attribute has already been taken.",
"uppercase": "The :attribute field must be uppercase.",
"url": "The :attribute field must be a valid URL.",
"uuid": "The :attribute field must be a valid UUID.",
"uuid3": "The :attribute field must be a valid UUID v3.",
"uuid4": "The :attribute field must be a valid UUID v4.",
"uuid5": "The :attribute field must be a valid UUID v5.",

"len": "The :attribute field must be :size characters.",
"min_len": "The :attribute field must be at least :min characters.",
"max_len": "The :attribute field must not be greater than :max characters.",
"eq_field": "The :attribute field must match :other.",
"ne_field": "The :attribute field and :other must be different.",
Comment thread
h2zi marked this conversation as resolved.
"gt_field": "The :attribute field must be greater than :value.",
"gte_field": "The :attribute field must be greater than or equal to :value.",
"lt_field": "The :attribute field must be less than :value.",
"lte_field": "The :attribute field must be less than or equal to :value.",
"gt_date": "The :attribute field must be a date after :date.",
"lt_date": "The :attribute field must be a date before :date.",
"gte_date": "The :attribute field must be a date after or equal to :date.",
"lte_date": "The :attribute field must be a date before or equal to :date.",
"number": "The :attribute field must be a number.",
"full_url": "The :attribute field must be a valid URL."
}
Loading
Loading