faker icon indicating copy to clipboard operation
faker copied to clipboard

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

Open h2oearth opened this issue 2 years ago • 13 comments

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

h2oearth avatar Jul 27 '22 08:07 h2oearth

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

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

ST-DDT avatar Jul 27 '22 14:07 ST-DDT

@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.

h2oearth avatar Jul 27 '22 23:07 h2oearth

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

Shinigami92 avatar Jul 30 '22 10:07 Shinigami92

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.

h2oearth avatar Jul 30 '22 12:07 h2oearth

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

Minozzzi avatar Jul 30 '22 16:07 Minozzzi

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

Minozzzi avatar Aug 01 '22 16:08 Minozzzi

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?

ST-DDT avatar Aug 01 '22 16:08 ST-DDT

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

Minozzzi avatar Aug 01 '22 16:08 Minozzzi

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.

ST-DDT avatar Jan 19 '23 16:01 ST-DDT

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:

Shinigami92 avatar Jan 20 '23 20:01 Shinigami92

lucy me that I have a workaround lol

h2oearth avatar Jan 23 '23 05:01 h2oearth

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 🙏🏽

lewxdev avatar Feb 27 '24 15:02 lewxdev

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)

matthewmayer avatar Feb 27 '24 15:02 matthewmayer