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

The number field rejects a string that is a number #113

Closed
lindyhopchris opened this issue Aug 18, 2021 · 1 comment
Closed

The number field rejects a string that is a number #113

lindyhopchris opened this issue Aug 18, 2021 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@lindyhopchris
Copy link
Contributor

In the Eloquent package, the Number field only accepts an integer or float. This is correct for JSON because a JSON number decodes to a PHP integer or float.

However, Laravel's integer and numeric validation rules pass with a string that represents a number - because Laravel's validation is loosely-typed. This means that a string number value can pass the validation but then cause an exception to be thrown from the Number field.

We should fix in two ways:

  1. Provide a number validation rule that can be used for a JSON number - ensuring the PHP value is validated as either an integer or float. This should be the recommended way of validating the number field.

  2. We could provide a acceptStrings() method on the number field - when called, the field would also accept a numeric string. Personally I wouldn't use this but it would mean the developer can opt-in to numeric strings.

@lindyhopchris lindyhopchris added the bug Something isn't working label Aug 18, 2021
@lindyhopchris
Copy link
Contributor Author

Worth noting that a number/integer validation rule was added in 2.0. Here are the docs:
https://laraveljsonapi.io/docs/2.0/requests/resources.html#validating-numbers

@lindyhopchris lindyhopchris added this to the 2.x milestone Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant