Skip to content
Merged
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
221 changes: 221 additions & 0 deletions validation/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package validation

import (
"context"
"fmt"
"testing"

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

// --- End-to-end validation benchmarks ---

func BenchmarkValidation_Simple(b *testing.B) {
v := NewValidation()
ctx := context.Background()
data := map[string]any{
"name": "John",
"email": "john@example.com",
"age": "25",
}
rules := map[string]any{
"name": "required|string|max:255",
"email": "required|email",
"age": "required|integer|min:1|max:150",
}

b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = v.Make(ctx, data, rules)
}
}

func BenchmarkValidation_ManyFields(b *testing.B) {
v := NewValidation()
ctx := context.Background()
data := make(map[string]any, 50)
rules := make(map[string]any, 50)
for i := range 50 {
key := fmt.Sprintf("field_%d", i)
data[key] = fmt.Sprintf("value_%d", i)
rules[key] = "required|string|min:1|max:255"
}

b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = v.Make(ctx, data, rules)
}
}

func BenchmarkValidation_ComplexRules(b *testing.B) {
v := NewValidation()
ctx := context.Background()
data := map[string]any{
"email": "test@example.com",
"website": "https://example.com",
"code": "ABC-123",
"start_date": "2024-01-01",
"end_date": "2024-12-31",
"password": "secret123",
"password_confirmation": "secret123",
}
rules := map[string]any{
"email": "required|email",
"website": "required|url",
"code": []string{"required", "regex:^[A-Z]{3}-[0-9]{3}$"},
"start_date": "required|date|before:end_date",
"end_date": "required|date|after:start_date",
"password": "required|string|min:8|confirmed",
}

b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = v.Make(ctx, data, rules)
}
}

func BenchmarkValidation_Wildcards(b *testing.B) {
v := NewValidation()
ctx := context.Background()
items := make([]any, 20)
for i := range 20 {
items[i] = map[string]any{
"name": fmt.Sprintf("Item %d", i),
"price": fmt.Sprintf("%d.99", i+1),
"qty": fmt.Sprintf("%d", i+1),
}
}
data := map[string]any{"items": items}
rules := map[string]any{
"items": "required|array|min:1",
"items.*.name": "required|string|max:100",
"items.*.price": "required|numeric|min:0",
"items.*.qty": "required|integer|min:1",
}

b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = v.Make(ctx, data, rules)
}
}

func BenchmarkValidation_AllInvalid(b *testing.B) {
v := NewValidation()
ctx := context.Background()
data := map[string]any{
"name": "",
"email": "not-an-email",
"age": "not-a-number",
"url": "not-a-url",
"date": "not-a-date",
}
rules := map[string]any{
"name": "required|string|min:1",
"email": "required|email",
"age": "required|integer|min:1|max:150",
"url": "required|url",
"date": "required|date",
}

b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = v.Make(ctx, data, rules)
}
}

func BenchmarkValidation_WithFilters(b *testing.B) {
v := NewValidation()
ctx := context.Background()
rules := map[string]any{
"name": "required|string|max:255",
"email": "required|email",
"bio": "required|string",
}
opts := []contractsvalidation.Option{
Filters(map[string]any{
"name": "trim",
"email": "trim|lower",
"bio": "trim|strip_tags",
}),
}

b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
// Data is mutated by filters, so recreate it each iteration
dataCopy := map[string]any{
"name": " John Doe ",
"email": " JOHN@EXAMPLE.COM ",
"bio": " <b>Hello</b> World ",
}
_, _ = v.Make(ctx, dataCopy, rules, opts...)
}
}

// --- Component benchmarks ---

func BenchmarkRuleParser_Simple(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
ParseRules("required|string|max:255")
}
}

func BenchmarkRuleParser_Complex(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
ParseRules("required|string|min:1|max:255|in:a,b,c,d,e")
}
}

func BenchmarkDataBag_Get_Flat(b *testing.B) {
bag, _ := NewDataBag(map[string]any{"name": "John", "age": 25})
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = bag.Get("name")
}
}

func BenchmarkDataBag_Get_Nested(b *testing.B) {
bag, _ := NewDataBag(map[string]any{
"user": map[string]any{
"profile": map[string]any{
"name": "John",
},
},
})
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = bag.Get("user.profile.name")
}
}

func BenchmarkExpandWildcardFields(b *testing.B) {
rules := map[string][]ParsedRule{
"items.*.name": {{Name: "required"}, {Name: "string"}},
"items.*.price": {{Name: "required"}, {Name: "numeric"}},
"items.*.qty": {{Name: "required"}, {Name: "integer"}},
}
keys := make([]string, 0, 100)
for i := range 20 {
keys = append(keys, fmt.Sprintf("items.%d", i))
keys = append(keys, fmt.Sprintf("items.%d.name", i))
keys = append(keys, fmt.Sprintf("items.%d.price", i))
keys = append(keys, fmt.Sprintf("items.%d.qty", i))
}

b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_ = expandWildcardFields(rules, keys, true)
}
}
36 changes: 36 additions & 0 deletions validation/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,21 @@ func (e *Engine) formatErrorMessage(field string, rule ParsedRule, attrType stri
}
replacements[":values"] = strings.Join(names, ", ")
}
case "exclude_if", "exclude_unless":
if len(rule.Parameters) > 0 {
replacements[":other"] = getDisplayableAttribute(rule.Parameters[0], e.attributes)
}
if len(rule.Parameters) > 1 {
replacements[":value"] = strings.Join(rule.Parameters[1:], ", ")
}
case "same", "different", "in_array", "confirmed", "prohibits":
if len(rule.Parameters) > 0 {
replacements[":other"] = getDisplayableAttribute(rule.Parameters[0], e.attributes)
}
case "eq", "ne":
if len(rule.Parameters) > 0 {
replacements[":value"] = rule.Parameters[0]
}
case "digits":
if len(rule.Parameters) > 0 {
replacements[":digits"] = rule.Parameters[0]
Expand All @@ -379,6 +390,31 @@ func (e *Engine) formatErrorMessage(field string, rule ParsedRule, attrType stri
if len(rule.Parameters) > 0 {
replacements[":format"] = rule.Parameters[0]
}
// Deprecated: will be removed in the next version.
case "len":
if len(rule.Parameters) > 0 {
replacements[":size"] = rule.Parameters[0]
}
case "min_len":
if len(rule.Parameters) > 0 {
replacements[":min"] = rule.Parameters[0]
}
case "max_len":
if len(rule.Parameters) > 0 {
replacements[":max"] = rule.Parameters[0]
}
case "eq_field", "ne_field":
if len(rule.Parameters) > 0 {
replacements[":other"] = getDisplayableAttribute(rule.Parameters[0], e.attributes)
}
case "gt_field", "gte_field", "lt_field", "lte_field":
if len(rule.Parameters) > 0 {
replacements[":value"] = getDisplayableAttribute(rule.Parameters[0], e.attributes)
}
case "gt_date", "lt_date", "gte_date", "lte_date":
if len(rule.Parameters) > 0 {
replacements[":date"] = rule.Parameters[0]
}
}

return formatMessage(msg, replacements)
Expand Down
Loading
Loading