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

It does not work if "data" value is an empty object. #27

Closed
AlighaThor opened this issue Jul 31, 2020 · 10 comments
Closed

It does not work if "data" value is an empty object. #27

AlighaThor opened this issue Jul 31, 2020 · 10 comments
Assignees

Comments

@AlighaThor
Copy link

Hey, good library. I'm very used to Laravel, so this implementation of yours is a good help.

I found probably an issue regarding if "data" is an empty object or not. Right now I'm working with Express, and after noticing that I'm receiving an empty object from req.body, I cannot make the validator fails.

Is this the intended behaviour? If I recall correctly, in Laravel if a Validation.make() instance does not receive a filled value, the fails() method gets called. This is not happening with this implementation. I mean, I think if we are validating some rules against empty/null/undefined data the validator should fail.

Steps to reproduce:

  • Create an empty data object.
  • Apply it to the validator instancie.
  • Call fails() or passes()

Result:

  • The expected validation result is misleading.
@jfstn
Copy link
Collaborator

jfstn commented Jul 31, 2020

Hello!

Thanks a lot for your input. That behaviour was recently changed to not fail if validating empty objects.

As this library is based on Laravel I might revert this decision on the next release. I'll leave this issue open until that new release is published.

@jfstn jfstn self-assigned this Jul 31, 2020
@jfstn
Copy link
Collaborator

jfstn commented Sep 16, 2020

Hello 👋

The current behavior will stay as I believe that if no data is present then there is nothing to validate even if that's laravel default behavior.

If you do not agree with this feel free to tell me. I'll close the issue for now.

Thanks,
João Faustino

@jfstn jfstn closed this as completed Sep 16, 2020
@AlighaThor
Copy link
Author

Hi Joao. I ended using other validation solution. Thanks. Keep on going with this library. Regards.

@guyaumetremblay
Copy link

I'm sorry to reply to an old thread but I think this is a bad decision. We just updated our dependency to 1.1.2 and some validations stop working due to this. I'll explain why I think this is a bad decision.

We'll take one of our situation as an exemple:

We have a POST request containing only one field. So, our code look like this:

const validator = Validator.make(req.body, {
    foo: "required",
});

So, this validation stop working if we receive a call on our endpoint without the only one and required field!

Now, we need to do something like that:

if (validator.fails() || Object.keys(req.body).length === 0) {
...
}

Or just drop the validator...

Could you re-think your decision please?

@jfstn
Copy link
Collaborator

jfstn commented May 28, 2021

Let me see If I understood correctly.

  1. You are trying to validate an object that should have a foo property, therefore required.
  2. Validations fails because an object without that property, foo was passed.

Is that it? :)

@guyaumetremblay
Copy link

guyaumetremblay commented May 28, 2021 via email

@jfstn
Copy link
Collaborator

jfstn commented May 28, 2021

I'll look at this and get back to you this weekend.

@jfstn
Copy link
Collaborator

jfstn commented May 31, 2021

I'll revert this decision on the next release.

@guyaumetremblay
Copy link

@jfstn Thanks a lot 👍

@jfstn jfstn reopened this Jul 6, 2021
@jfstn
Copy link
Collaborator

jfstn commented Aug 1, 2021

@guyaumetremblay I will release a new version today that fixes this behavior.

  1. If there are no required fields the validation should pass;
  2. If there are required fields the validation should fail;

Thank you.

@jfstn jfstn closed this as completed in 77cec4e Aug 1, 2021
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

No branches or pull requests

3 participants