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

docs(guide): add complex overwrites example #3207

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion docs/guide/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,49 @@ This allows the value to be more reasonable based on the provided arguments.

Unlike the `_id` property that uses an `uuid` implementation, which has a low chance of duplicates, the `email` function is more likely to produce duplicates, especially if the call arguments are similar. We have a dedicated guide page on generating [unique values](unique).

Congratulations, you should now be able to create any complex object you desire. Happy faking 🥳.
The example above demonstrates how to generate complex objects.
To gain more control over the values of specific properties, you can introduce `overwrites`, `options` or similar parameters:

```ts {3,17}
import { faker } from '@faker-js/faker';

function createRandomUser(overwrites: Partial<User> = {}): User {
const {
_id = faker.string.uuid(),
avatar = faker.image.avatar(),
birthday = faker.date.birthdate(),
sex = faker.person.sexType(),
firstName = faker.person.firstName(sex),
lastName = faker.person.lastName(),
email = faker.internet.email({ firstName, lastName }),
subscriptionTier = faker.helpers.arrayElement([
'free',
'basic',
'business',
]),
} = overwrites;

return {
_id,
avatar,
birthday,
email,
firstName,
lastName,
sex,
subscriptionTier,
};
}

const user = createRandomUser();
const userToReject = createRandomUser({ birthday: new Date('2124-10-20') });
```

A potential `options` parameter could be used to:

- control which optional properties are included,
- control how nested elements and arrays are merged or replaced,
- or specify the number of items to generate for nested lists.

Congratulations, you should now be able to create any complex object you desire.
Happy faking 🥳.