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

Add required_with, required_with_all, required_without, required_without_all validators #469

Merged
merged 12 commits into from
May 23, 2019

Conversation

mashmooli
Copy link
Contributor

@mashmooli mashmooli commented Apr 29, 2019

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

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

Change Details:
add these validators

  • required_with: The field under validation must be present and not empty only if any of the other specified fields are not empty, (required_with:field1,field2);
  • required_with_all: The field under validation must be present and not empty only if all of the other specified fields are not empty, (required_with_all:field1,field2);
  • required_without: The field under validation must be present and not empty only when any of the other specified fields are empty, (required_without:field1,field2);
  • required_without_all: The field under validation must be present and not empty only when all of the other specified fields are empty, (required_without_all:field1,field2).

@go-playground/admins

@coveralls
Copy link

coveralls commented Apr 29, 2019

Coverage Status

Coverage remained the same at 100.0% when pulling 8c22169 on mashmooli:v9 into 46b4b1e on go-playground:v9.

@deankarn
Copy link
Contributor

Hey @mashmooli thanks for the PR! nice work!

I have a couple of requests before I can merge:

  • can you add some more test cases? it looks like the current ones don't test all of the new code paths yet.
  • can you add these new methods to the docs here, I'm sure a ton of other people will find the new validations useful as well :)

@mashmooli
Copy link
Contributor Author

Hi @deankarn thanks for the reply.
I added new methods to the docs and I'll be working on test cases soon.

@deankarn deankarn merged commit 56b7caa into go-playground:v9 May 23, 2019
@deankarn
Copy link
Contributor

@mashmooli merged, I think that a cool addition would be allowing this to work with nested structures also :)

The helper functions exist within this repo if you're interested :)
eg. https://github.com/go-playground/validator/blob/v9/util.go#L60

fairyhunter13 added a commit to fairyhunter13/validator that referenced this pull request Jul 12, 2020
Add `required_with`, `required_with_all`, `required_without`, `required_without_all` validators
@andig
Copy link

andig commented Sep 28, 2020

@mashmooli hope it is ok to be asking here. I had expected the following to throw an error but it isn't:

func TestModbusConfigFoo(t *testing.T) {
	type Settings struct {
		Opt, Val string
	}

	cc := struct {
		Model    string
		Value    string
		Settings *Settings `validate:"required_without_all=Model Value"`
	}{
		Model: "foo",
		// Value: "bar",
	}

	validate := validator.New()
	if err := validate.Struct(cc); err == nil {
		t.Error(err)
	}
}

Shouldn't that be covered by required_without_all?

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.

None yet

4 participants