Resources

Response Customization

By default, list responses are arrays

[
  {
    "name": "Peter",
    "email": "pete@company.com"
  },
  {
    "name": "Mary",
    "email": "mary@company.com"
  }
]

you can customize your resource ro return the elements inside an Envelope with

routes:
    - path: "/users"
    - path: "/users/{id}"
resources:
    - name: "users"
      envelope: items
{
  "items": [
    {
      "name": "Peter",
      "email": "pete@company.com"
    },
    {
      "name": "Mary",
      "email": "mary@company.com"
    }
  ]
}

when using an Envelope you can also define a field that will contain the total number of elements returned

routes:
    - path: "/users"
    - path: "/users/{id}"
resources:
    - name: "users"
      envelope: items
      total-elements: totalCount
{
  "items": [
    {
      "name": "Peter",
      "email": "pete@company.com"
    },
    {
      "name": "Mary",
      "email": "mary@company.com"
    }
  ],
  "totalCount": 2
}

POST

POST will generate ids automatically, by default all ids are random Strings and, it's assumed that the name od the id field is id (lower case)

to changes this behaviour you can add the following configuration

routes:
    - path: "/users"
    - path: "/users/{userId}"
resources:
    - name: "users"
      id-field: userId
      id-type: Number
      id-length: 6

Possible Id types are: String, Number

PUT

By default, PUT only tries to update existing elements, if you want to allow creation on PUT, the url id will be used and any id on the request body will be ignored

routes:
    - path: "/users"
    - path: "/users/{id}"
resources:
    - name: "users"
      create-on-put: true

Validation

json-mockr will use the first element of a collection to validate the following requests.

Seed data

If your configuration only contains routes you will start with an empty set of resources, but you can provide seed data to json-mockr

routes:
    - path: "/users"
    - path: "/users/{id}"
resources:
    - name: "users"
      seed-data:
        file-path: /data/users.json

the seed data must be a JSON Array, ex:

[
  {
    "name": "Peter",
    "email": "pete@company.com",
    "age": 45
  },
  {
    "name": "Mary",
    "email": "mary@company.com",
    "age": 28
  }
]

if your json file is not an array you can point to the field you want to use by defining a JSON-Pointer ex:

routes:
    - path: "/users"
    - path: "/users/{id}"
resources:
    - name: "users"
      seed-data:
        file-path: /data/users.json
        json-pointer: "/items"

this will read the items JSON Array:

{
  "items": [
    {
      "name": "Peter",
      "email": "pete@company.com",
      "age": 45
    },
    {
      "name": "Mary",
      "email": "mary@company.com",
      "age": 28
    }
  ]
}

Data generation (experimental)

It's possible to generate "Fake Data" to initialize resources by configuring generators and assign them to resources. Generators must always begin with #

routes:
    - path: "/users"
    - path: "/users/{id}"
resources:
    - name: "users"
      generator:
          name: user-gen
          quantity: 50

generators:
    - name: "user-gen"
      spec: >
      {  
          "id": "#number(5)",
          "name": "#fullName()",
          "email": "#email()",
          "age": "#number(2)"
      }

Available generators:

  • text(length: Int)
  • number(length: Int)
  • name()
  • fullName()
  • email()
  • price(min: Int, max: Int)
  • references(resource: String, field: String)