Skip to content

Add ability to use whitelist conditions#1668

Merged
snuyanzin merged 4 commits intodatafaker-net:mainfrom
snuyanzin:whitelist
Sep 23, 2025
Merged

Add ability to use whitelist conditions#1668
snuyanzin merged 4 commits intodatafaker-net:mainfrom
snuyanzin:whitelist

Conversation

@snuyanzin
Copy link
Copy Markdown
Collaborator

Sometimes especially for prod systems it is required to have kind of limitations like whitelist or blacklist for providers.

Instead of having both, just add predicate where condition could be supplied

without predicate it behaves as before

@what-the-diff
Copy link
Copy Markdown

what-the-diff bot commented Sep 23, 2025

PR Summary

  • New Constructor in Faker related classes for Whitelisting Providers
    In this PR, we have added a new way of initializing classes related to 'Faker': BaseFaker, FoodFaker, HealthcareFaker, SportFaker, and VideoGameFaker. This new way allows the user to define which 'providers' are safe to use or 'whitelisted', using a 'whitelist predicate'.

  • Updated getProvider method to respect the whitelist
    The getProvider method in BaseFaker has been modified in order to respect our new whitelist. If the class of the provider is not on the whitelist, this method will now raise an exception, ensuring that only approved providers can be called.

  • Ensuring Whitelisting Functionality Works Correctly
    We have also introduced new test cases in the FakerTest class. These tests emulate various scenarios to confirm that the new whitelisting feature works correctly, helping to ensure the safety and integrity of output from our 'Faker' classes.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 23, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 52.63158% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.44%. Comparing base (2923e4a) to head (e40aa95).

Files with missing lines Patch % Lines
...n/java/net/datafaker/providers/food/FoodFaker.java 0.00% 2 Missing ⚠️
...atafaker/providers/healthcare/HealthcareFaker.java 0.00% 2 Missing ⚠️
...java/net/datafaker/providers/sport/SportFaker.java 0.00% 2 Missing ⚠️
.../datafaker/providers/videogame/VideoGameFaker.java 0.00% 2 Missing ⚠️
...n/java/net/datafaker/providers/base/BaseFaker.java 88.88% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1668      +/-   ##
============================================
- Coverage     92.45%   92.44%   -0.02%     
- Complexity     3388     3396       +8     
============================================
  Files           333      333              
  Lines          6697     6714      +17     
  Branches        665      666       +1     
============================================
+ Hits           6192     6207      +15     
- Misses          346      350       +4     
+ Partials        159      157       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asolntsev
Copy link
Copy Markdown
Collaborator

To be honest, I don't understand why it's needed.

Do I correctly understand that

  1. new Faker.expression("#{Name.fullName}") return a random name
  2. Faker(..., c -> c != Name.class).expression("#{Name.fullName}") throws an exception

But if you don't need names, just don't call *.expression("#{Name.fullName}")... What's the problem?

}

public BaseFaker(FakeValuesService fakeValuesService, FakerContext context) {
this(fakeValuesService, context, null);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor thing: maybe instead of null, it's better to pass a Predicate that always returns true? (to avoid potential NPEs and simplify the logic)

@snuyanzin
Copy link
Copy Markdown
Collaborator Author

snuyanzin commented Sep 23, 2025

Do I correctly understand that
new Faker.expression("#{Name.fullName}") return a random name
Faker(..., c -> c != Name.class).expression("#{Name.fullName}") throws an exception

yes

But if you don't need names, just don't call *.expression("#{Name.fullName}")... What's the problem?

it works if you control the input

however if input comes from a third party then you can not say just don't call smth... or even if you say you can not be sure in it

@asolntsev asolntsev added this to the 2.5.1 milestone Sep 23, 2025
@asolntsev asolntsev added the enhancement New feature or request label Sep 23, 2025
@snuyanzin snuyanzin merged commit fd174b1 into datafaker-net:main Sep 23, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants