Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Nil pointer dereference in Arabic translations #1391

Merged
merged 2 commits into from
Mar 5, 2025

Conversation

BlackSud0
Copy link
Contributor

Fixes or Enhances

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

Description

This PR fixes [Bug #1385]: Nil pointer dereference in Arabic translations and adds comprehensive Arabic (ar) translations for the go-playground/validator package. The implementation provides Arabic error messages for all common validation tags, making the validator more valuable to Arabic-speaking developers.

Features

  • Fixes nil pointer dereference bug in Arabic translations
  • Complete Arabic translations for all standard validation tags
  • Comprehensive test suite to verify translation accuracy
  • Follows the same pattern as existing language translations
  • Properly handles Arabic text direction and grammar

Implementation Details

  • The implementation follows the established pattern in the validator package:
  • Registers translations for each validation tag
  • Provides appropriate Arabic phrasing for validation errors
  • Handles parameter substitution in error messages
  • Includes tests for all validation scenarios

Testing

  • A comprehensive test suite is included that verifies:
  • Basic validations (required, email, etc.)
  • Length validations (min, max, len)
  • Comparison validations (lt, lte, gt, gte, eq, ne)
  • Field comparison validations (eqfield, nefield, gtefield)
  • Content validations (alpha, alphanum, contains, etc.)
  • Special validations (oneof, datetime)
  • All tests pass and confirm that the translations are correctly applied.

Usage Example

validate := validator.New()
arabic := ar.New()
uni := ut.New(arabic, arabic)
trans, _ := uni.GetTranslator("ar")

// Register Arabic translations
ar.RegisterDefaultTranslations(validate, trans)

// Now validation errors will be in Arabic
err := validate.Struct(myStruct)
if err != nil {
    errs := err.(validator.ValidationErrors)
    for _, e := range errs {
        // This will be in Arabic
        fmt.Println(e.Translate(trans))
    }
}

@nodivbyzero
@go-playground/validator-maintainers

@BlackSud0 BlackSud0 requested a review from a team as a code owner February 28, 2025 02:55
@coveralls
Copy link

coveralls commented Feb 28, 2025

Coverage Status

coverage: 74.712% (-0.4%) from 75.084%
when pulling f37f31f on BlackSud0:master
into 45dd6e3 on go-playground:master.

@nodivbyzero
Copy link
Contributor

Overall, it looks good to me.
Would it be possible to add more tests?
The current coverage is quite low at 39.2%.

$ go test ./translations/ar -coverprofile ar-coverage.out
ok      github.com/go-playground/validator/v10/translations/ar  0.249s  coverage: 39.2% of statements

@BlackSud0
Copy link
Contributor Author

My latest commit enhances the Arabic (ar) translations by adding support for several validation tags that were previously missing and improving test coverage. The current test coverage was approximately 56.9%.

Changes

Added translations for the following validation tags:

  • fqdn - Fully Qualified Domain Name validation
  • cron - Cron expression validation
  • md5 - MD5 hash validation
  • sha256 - SHA256 hash validation
  • semver - Semantic versioning validation

Added special handling for duration validations:

  • Custom translation registrations for the min and max tags when applied to time.Duration fields
  • Created Arabic translations for duration-related error messages

Expanded test coverage:

  • Added test cases for all new validation tags
  • Added test expectations for duration, boolean, cve, and other validators

@nodivbyzero nodivbyzero merged commit bae7f6d into go-playground:master Mar 5, 2025
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants