Skip to content

Commit

Permalink
docs(guide): add complex overwrites example (#3207)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Oct 21, 2024
1 parent f917f4f commit 0d1616d
Showing 1 changed file with 46 additions and 1 deletion.
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 🥳.

0 comments on commit 0d1616d

Please sign in to comment.