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

country_code alias panics on invalid iso3166_1_alpha2 and iso3166_1_alpha3 #860

Closed
2 tasks done
uberswe opened this issue Nov 23, 2021 · 4 comments · Fixed by #873
Closed
2 tasks done

country_code alias panics on invalid iso3166_1_alpha2 and iso3166_1_alpha3 #860

uberswe opened this issue Nov 23, 2021 · 4 comments · Fixed by #873

Comments

@uberswe
Copy link
Contributor

uberswe commented Nov 23, 2021

  • I have looked at the documentation here first?
  • I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10: v10

Issue, Question or Enhancement:

In #615 the country_code alias is introduced which calls 3 validators iso3166_1_alpha2|iso3166_1_alpha3|iso3166_1_alpha_numeric.

If you use the country_code to validate a string which has an invalid value you will receive a panic with the message Bad field type string. This is caused by the iso3166_1_alpha_numeric validation here

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
.

Code sample, to showcase or reproduce:

This is a test I wrote to confirm the issue, you will receive a panic on "1".

func TestCountryCodeValidation(t *testing.T) {
	tests := []struct {
		value    interface{}
		expected bool
	}{
		{248, true},
		{0, false},
		{1, false},
		{"NO", true},
		{"1", false},
		{"0", false},
	}

	validate := New()

	for i, test := range tests {

		errs := validate.Var(test.value, "country_code")

		if test.expected {
			if !IsEqual(errs, nil) {
				t.Fatalf("Index: %d country_code failed Error: %s", i, errs)
			}
		} else {
			if IsEqual(errs, nil) {
				t.Fatalf("Index: %d country_code failed Error: %s", i, errs)
			}
		}
	}
}

Expected behaviour

I would expect that the alias should not panic here but instead return false.

Fix

I was planning to make a PR for this issue but the fix may require some big changes and I thought it may be good to discuss them before making any kind of changes.

A simple solution would be to catch any panic for an alias, this makes sense for country_code which can have multiple valid types.

An easier solution would be to just remove the panic in the iso3166_1_alpha_numeric function but then it would differ from the rest of the package. That raises another question of whether the package should be throwing panics in the first place?

I am not sure what the best way to solve this would be and would look for input from maintainers or perhaps @krhubert who added this functionality.

@joekendal
Copy link

what a dreadful package this is

@deankarn
Copy link
Contributor

deankarn commented Jan 1, 2022

Hey @uberswe

It looks like when the country code validations were added support for string types wasn't added. I think all that needs to be done is to add a new case statement for string field data from a string to an int or uint and then do the same validation that exists today here

I'd rather keep the panic than returning false because string fields were NOT supported by this validation, returning false would give the indication it was working but...it's not.

It would be greatly appreciated if you could create a PR to add those other cases statements.

@deankarn
Copy link
Contributor

deankarn commented Jan 1, 2022

@joekendal #860 (comment)

Your comment is extremely unhelpful and reflects poorly on yourself. I recommend reading this.

@uberswe
Copy link
Contributor Author

uberswe commented Jan 1, 2022

Thank you for the feedback @deankarn, I agree with your suggestion and opened PR #873 which follows what you said.

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 a pull request may close this issue.

3 participants