Custom Data

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:

mockend.yml
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:

  1. count to control how many resources should be returned
  2. random string generator for title and author
mockend.yml
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:

Demo

As always you can check our demo repository .mockend.json (opens in a new tab) which contains custom data and random values.