Overview
Sometimes you may need to use your own data and not random values. Mockend is very flexible in this regard, you can set values for specific resources and optionally complete them with random ones if needed. Let's see how.
Setting data
If you don't want to have random values, use _.data
only. For example:
models:
Post:
data: [{ title: 'Title 1' }, { title: 'Title 2' }]
When listing all resources, Mockend will send:
GET mockend.com/.../posts
[
{ "id": 1, "title": "Title 1" },
{ "id": 2, "title": "Title 2" }
]
Adding random values
Considering our previous example, let's say that you would like Mockend to return 10 books instead of 2 with a random author name.
Change the previous config by adding:
count
to control how many resources should be returned- random string generator for
title
andauthor
models:
Post:
fake:
_count: 10
title: { loremWords: [1, 3] }
author: name
Post:
data: [
{ title: Title 1 },
{ title: Title 2 },
]
[
{ "id": 1, "title": "Title 1" },
{ "id": 2, "title": "Title 2" }
{ "id": 3, "title": "lorem ipsum" },
// ...
]
Using random pickers
Mockend can also pick random values that you provide.
models:
Post:
fake:
category: { stringOf: [a, b, c] }
code: { intOf: [1, 2, 3] }
External generator
In addition to all this, Mockend works really well with external libraries to generate common values for your .mockend.json
. Here's a JavaScript example using FakerJS (opens in a new tab):
const randomNames = []
// Create an array of 100 random names
for (let i = 0; i < 100; i++) {
randomNames.push(fake.name.firstName())
})
yaml.dump({
models:
User: {
// Mockend will use firstNames provided by FakerJS
fake: {
name: { stringOf: randomNames }
}
}
}
})
Other libraries:
- Python - https://github.com/joke2k/faker (opens in a new tab)
- Ruby - https://github.com/faker-ruby/faker (opens in a new tab)
- PHP - https://github.com/FakerPHP/Faker (opens in a new tab)
Demo
As always you can check our demo repository .mockend.json (opens in a new tab) which contains custom data and random values.