Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aymaneallaoui committed Sep 13, 2024
1 parent 1d25406 commit f2c78d6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 17 deletions.
5 changes: 2 additions & 3 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"fmt"

"github.com/aymaneallaoui/zod-Go/zod"
"github.com/aymaneallaoui/zod-Go/zod/validators"
)

func main() {
// Example 1: Custom Error Messages for Strings

stringSchema := validators.String().Min(3).Max(5).Required().
WithMessage("minLength", "This string is too short!").
WithMessage("maxLength", "This string is too long!")
Expand All @@ -17,7 +18,6 @@ func main() {
fmt.Println("Validation failed:", err.(*zod.ValidationError).ErrorJSON())
}

// Example 2: Nested Object Validation with Custom Messages
userSchema := validators.Object(map[string]zod.Schema{
"name": validators.String().Min(3).Required().
WithMessage("required", "Name is a required field!").
Expand All @@ -34,7 +34,6 @@ func main() {
}).Required(),
})

// Invalid user data
userData := map[string]interface{}{
"name": "Jo",
"age": 17,
Expand Down
11 changes: 3 additions & 8 deletions zod/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@ import (
"fmt"
)

// ValidationError holds detailed information about validation errors
type ValidationError struct {
Field string `json:"field"`
Value interface{} `json:"value"`
Message string `json:"message"`
Details []ValidationError `json:"details,omitempty"` // Nested errors go here
Details []ValidationError `json:"details,omitempty"`
}

// Error formats the error as a human-readable message
func (e *ValidationError) Error() string {
return fmt.Sprintf("Validation error on field '%s': %s (Value: %v)", e.Field, e.Message, e.Value)
}

// ErrorJSON formats the error as a JSON string
func (e *ValidationError) ErrorJSON() string {
jsonData, _ := json.MarshalIndent(e, "", " ") // Pretty print JSON for readability
jsonData, _ := json.MarshalIndent(e, "", " ")
return string(jsonData)
}

// NewValidationError creates a new validation error
func NewValidationError(field string, value interface{}, message string) *ValidationError {
return &ValidationError{
Field: field,
Expand All @@ -33,12 +29,11 @@ func NewValidationError(field string, value interface{}, message string) *Valida
}
}

// NewNestedValidationError creates a new validation error with nested details
func NewNestedValidationError(field string, value interface{}, message string, details []ValidationError) *ValidationError {
return &ValidationError{
Field: field,
Value: value,
Message: message,
Details: details, // Add nested errors here
Details: details,
}
}
1 change: 0 additions & 1 deletion zod/validators/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func (s *StringSchema) Required() *StringSchema {
return s
}

// WithMessage allows custom error messages for different validation types
func (s *StringSchema) WithMessage(validationType, message string) *StringSchema {
s.customError[validationType] = message
return s
Expand Down
7 changes: 2 additions & 5 deletions zod/validators/stucts.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,16 @@ func (o *ObjectSchema) Validate(data interface{}) error {
}
}

// If the value is an object, validate recursively and collect errors
if subObj, isMap := v.(map[string]interface{}); isMap {
if subErr := s.Validate(subObj); subErr != nil {
if nestedValidationErr, ok := subErr.(*zod.ValidationError); ok {
// Collect nested errors and pass them into NewNestedValidationError

errChan <- zod.NewNestedValidationError(k, v, "Validation failed", nestedValidationErr.Details)
}
return
}
}

// Handle regular validation errors
if err := s.Validate(v); err != nil {
errChan <- zod.NewValidationError(k, v, err.Error())
}
Expand All @@ -93,7 +91,6 @@ func (o *ObjectSchema) Validate(data interface{}) error {
close(errChan)
}()

// Collect errors from all fields
var combinedErrors []zod.ValidationError
for err := range errChan {
if validationErr, ok := err.(*zod.ValidationError); ok {
Expand All @@ -102,7 +99,7 @@ func (o *ObjectSchema) Validate(data interface{}) error {
}

if len(combinedErrors) > 0 {
// Return structured error with nested details

return zod.NewNestedValidationError("object", data, "Validation failed", combinedErrors)
}

Expand Down

0 comments on commit f2c78d6

Please sign in to comment.