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(number): bigint shouldn't throw when max is negative #3363

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

soc221b
Copy link

@soc221b soc221b commented Jan 3, 2025

No description provided.

@soc221b soc221b requested a review from a team as a code owner January 3, 2025 04:06
@soc221b soc221b changed the base branch from next to v9 January 3, 2025 04:06
Copy link

netlify bot commented Jan 3, 2025

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit 5e4aa59
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/6777f953afe8d50008f24e5e
😎 Deploy Preview https://deploy-preview-3363.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@xDivisionByZerox xDivisionByZerox added c: bug Something isn't working p: 1-normal Nothing urgent m: number Something is referring to the number module labels Jan 3, 2025
@xDivisionByZerox xDivisionByZerox added this to the v9.x milestone Jan 3, 2025
@xDivisionByZerox
Copy link
Member

I confirmed that this is a bug in the current version (v9.3.0) of Faker. Adding the acceped label.

@xDivisionByZerox xDivisionByZerox added the s: accepted Accepted feature / Confirmed bug label Jan 3, 2025
@ST-DDT
Copy link
Member

ST-DDT commented Jan 3, 2025

I'm not sure about it.
Afaict all our number methods return 0-max if only max is specified. So either we have to change all methods to behave like this or the user has to specify both min and max (and maybe we have to adjust the error message accordingly).
I personally prefer the later as arbitarily designating boundaries like this feels wrong. The user supposed knows their intend better than we do, so we shouldnt guess it.

Defaults to 0n or max - 999999999999999n if max is negative.

Vs

FakerError: Min not specified when using negative max option.

Or something similar.

If you just need a negative value you could just use -faker.number.bigInt()

Copy link

codecov bot commented Jan 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.97%. Comparing base (ece4f16) to head (5e4aa59).

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #3363      +/-   ##
==========================================
- Coverage   99.97%   99.97%   -0.01%     
==========================================
  Files        2811     2811              
  Lines      217023   217028       +5     
  Branches      942      944       +2     
==========================================
+ Hits       216972   216975       +3     
- Misses         51       53       +2     
Files with missing lines Coverage Δ
src/modules/number/index.ts 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@matthewmayer
Copy link
Contributor

I'm not sure about it.

Afaict all our number methods return 0-max if only max is specified. So either we have to change all methods to behave like this or the user has to specify both min and max (and maybe we have to adjust the error message accordingly).

I personally prefer the later as arbitarily designating boundaries like this feels wrong. The user supposed knows their intend better than we do, so we shouldnt guess it.

Defaults to 0n or max - 999999999999999n if max is negative.

Vs

FakerError: Min not specified when using negative max option.

Or something similar.

If you just need a negative value you could just use -faker.number.bigInt()

I agree

I think it's fairly intuitive what faker.number.int({max:10}) returns

I don't think it's intuitive what

faker.number.int({max:-10}) returns

@ST-DDT
Copy link
Member

ST-DDT commented Jan 3, 2025

@soc221b Could you please explain your thoughts regarding this change?
(Or the original issue you ran into?)

@ST-DDT ST-DDT changed the base branch from v9 to next January 3, 2025 14:50
@ST-DDT
Copy link
Member

ST-DDT commented Jan 3, 2025

I changed the target branch from v9 to next, as v9 is our release branch and all our development happens on the next branch.
(Unless we specifically back port changes to older versions)

@soc221b
Copy link
Author

soc221b commented Jan 3, 2025

Thanks for your comments about this.


Could you please explain your thoughts regarding this change?

I personally think giving a negative max is a valid usage even it doesn't seem intuitive.

the original issue you ran into?

I'm building a library to generate fake data based on faker.js and ran into this issue: https://github.com/soc221b/zod-schema-faker/blob/1.1.0/src/zod-bigint-faker.ts#L26-L29


I created an example of form validation using <input max="-10">, it works as expected:
https://stackblitz.com/edit/does-negative-max-work?file=index.html

@ST-DDT
Copy link
Member

ST-DDT commented Jan 3, 2025

I'm building a library to generate fake data based on faker.js and ran into this issue: https://github.com/soc221b/zod-schema-faker/blob/1.1.0/src/zod-bigint-faker.ts#L26-L29

I've looked at your code and noticed that you also have some code for multipleOf (similar to our faker.number.int({ multipleOf })).
Would you like to create a feature request issue for that as well?

@soc221b
Copy link
Author

soc221b commented Jan 3, 2025

Would you like to create a feature request issue for that as well?

Sure, I'll create another PR for that. 😃

@ST-DDT
Copy link
Member

ST-DDT commented Jan 3, 2025

Workaround for faker.number.bigInt({ max: -1n}):

-faker.number.bigInt({min: 1})

or with full context:

if (min == null && max != null && max < 0) {
  return -faker.number.bigInt({ min: -max });
}

@ST-DDT ST-DDT added the has workaround Workaround provided or linked label Jan 3, 2025
@ST-DDT
Copy link
Member

ST-DDT commented Jan 3, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working has workaround Workaround provided or linked m: number Something is referring to the number module p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants