faker-js / faker

Generate massive amounts of fake data in the browser and node.js
https://fakerjs.dev
Other
13k stars 919 forks source link

Add Min/Max length for faker.name.fullName and faker.name.lastName #1203

Open h2oearth opened 2 years ago

h2oearth commented 2 years ago

Clear and concise description of the problem

Some input fields may require that the first/last name does not exceed X number of characters

Suggested solution

provide an option to set min/max string length, and otherwise use default values.

Alternative

No response

Additional context

No response

ST-DDT commented 2 years ago

Do you need this feature or is it just an idea?

How should users behave if their name is longer than the given limit?

h2oearth commented 2 years ago

@ST-DDT We need that feature. Otherwise, we will experience random failures because the firstName is too short. I am considering concatenating a constant number of characters to avoid that issue. Rather than increasing the minimum number of characters by default, IMO is better to allow the user to pick that option.

We do not have any issue if the name is too long because we cap the maximum length.

Shinigami92 commented 2 years ago

I'm not sure about this proposal Faker is a data generator which aims for producing random valid looking data, but Faker might not be a database seeder :thinking:

So the results produced by faker's names helps you to spot potential issues with your form assumptions You might then want to throw validation errors in your form or retry to generate a next value until you have one that fits your needs

This is just what came into my mind, so not a final decision or similar, we might want to discuss this with the team in one of your next meetings

h2oearth commented 2 years ago

I 100% agree that fakers help to spot potential issues with length validation, but allowing the user to pick a max/min range will provide additional flexibility to the user to spot issues. For instance, if I know the min length that some of my fields can accept is 5 characters. I would like to know how my system is going to behave if I set up the range to min= 3 and max = 4. I would like to have more control over the length.

Minozzzi commented 2 years ago

I understand the @h2oearth idea. Maybe the optional { min, max} options might be interesting.

Minozzzi commented 2 years ago

@ST-DDT @Shinigami92 does it make sense to implement a min and max parameter?

ST-DDT commented 2 years ago

Please wait a bit so we have time to discuss this in depth with the team.

What do you expect to happen if a locale does not have an entry matching the requested size?

Minozzzi commented 2 years ago

Please wait a bit so we have time to discuss this in depth with the team.

What do you expect to happen if a locale does not have an entry matching the requested size?

Alright, @ST-DDT 😁 I don't thought about this

ST-DDT commented 1 year ago

Team decision

There are names that are as short as a single character. In our opinion the app should handle names that are too short. e.g. by padding them with whitespace/trim them somehow (or generating a new one).

We can consider this again if there is more interest from the community.

Shinigami92 commented 1 year ago

Half a year later and I think I changed my mind at least a bit over my prev comment https://github.com/faker-js/faker/issues/1203#issuecomment-1200128665

IMO we could introduce a faker.helpers function that could do something similar to faker.helpers.unique :thinking: So you provide a callback + options{min,max} and then it generates values until it found one ... but somehow that function would need to be based on faker's underlying mersenne, otherwise it would not have any benefit over a random found package on npm

faker.unique / faker.helpers.unique is not based on faker's mersenne, but pre v6 "artefact" and widely used, so that is why we have it still in out project But we find often many such util/helpers that are very useful especially in combination with faker I will ask the team in next meeting what they thing about a @faker-js/utils/@faker-js/utilities package :eyes: :thinking:

h2oearth commented 1 year ago

lucy me that I have a workaround lol

lewxdev commented 8 months ago

I will ask the team in next meeting what they thing about a @faker-js/utils/@faker-js/utilities package 👀 🤔

@Shinigami92 Any word on this? Our team of testers would greatly benefit from generating fixed-length names (especially for testing input field max/min length requirements)

@h2oearth Also, if there is indeed a valid workaround, could you post an example? I'm not sure I caught this in the conversation, but it'd be greatly appreciated 🙏🏽

matthewmayer commented 8 months ago
const {faker} = require("@faker-js/faker")
console.log(faker.helpers.arrayElement(faker.rawDefinitions.person.first_name.filter(a => a.length == 6)))

or

const {faker} = require("@faker-js/faker")
let name
do {
    name = faker.person.firstName()
} while (name.length != 6)
console.log(name)