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.json
{
  "Book": {
    "_": {
      "data": [{ "title": "Title 1" }, { "title": "Title 2" }]
    }
  }
}

When listing all resources, Mockend will send:

GET mockend.com/.../books

[
  { "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. _.items to control how many resources should be returned
  2. random string generator for title and author
.mockend.json
{
  "Book": {
    "_": {
      "items": 10,
      "data": [{ "title": "Title 1" }, { "title": "Title 2" }]
    }
  },
  "title": { "loremWords": { "minLength": 5, "maxLength": 20 } },
  "author": { "loremWord": {} }
}
[
  { "id": 1, "title": "Title 1", "author": "et" },
  { "id": 2, "title": "Title 2", "author": "veniam" },
  { "id": 3, "title": "similique iste", "author": "nam" },
  // ...
  { "id": 10, "title": "id perferendis aut", "author": "repellendus" }
]

Setting data by id

You can also update values for specific resources. Set id to update specific fields.

.mockend.json
{
  "Book": {
    "_": {
      "items": 10,
      "data": [{ "id": 10, "author": "Shakespeare" }]
    },
    "title": { "loremWords": { "minLength": 5, "maxLength": 20 } }
  }
}
[
  { "id": 1, "title": "Title 1" },
  { "id": 2, "title": "Title 2" }
  { "id": 3, "title": "lorem ipsum" },
  // ...
  { "id": 10, "title": "lorem ipsum", "author": "Shakespeare" }
]

Using random pickers

Mockend can also pick random values that you provide.

.mockend.json
{
  "Book": {
    "category": {
      "string": ["One", "Two"]
    },
    "code": {
      "int": [100, 200, 300]
    }
  }
}

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())
})
 
JSON.stringify({
  User: {
    // Mockend will use firstNames provided by FakerJS
    name: { string: 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.

Last updated on February 18, 2023