Skip to content
Node module to create random responses to requests based on RAML rest definition.
JavaScript RAML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

raml-mocker Build Status NPM version Dependency Status

Node module to create random responses to requests based on RAML rest definition.


The goal of this plugin is to provide automatic mocked responses that honor your defined RAML REST contracts. These contracts are evaluated and the plugin provides functions to generate random responses.

The RAML files should contain HTTP responses for each request you want to mock, application/json and a valid json schema.


  • Yo need to define a RAML file like this: definition.raml
  • Import the json-schema file or define it inside the raml file.


var ramlMocker = require('raml-mocker');
var options = {
    path: 'test/raml'
var callback = function (requests){
ramlMocker.generate(options, callback);

Or defining a collection of files instead of a path:

var options = {
    files: ['definition1.raml', 'folder/definition2.raml']

If your properties in the schema need an alternative logic you should use format. For example the schema in the test schema or in schemaTest:

'foo': {
    'type': 'string',
    'format': 'foo'

And in your options provide format definitions:

var options = {
    path: 'test/raml',
    formats : {
        foo: function foo(Faker, schema) {
            return Faker.Name.firstName();
    parserOptions: {
      dereferenceSchemas: true

As you could see the random generation is based on faker, so the function to generate a specific format receives the faker object and the schema. You can use Faker for implementing the logic (check the documentation).

And the RESULT in your callback

This generated request will return an array of objects like this:

        /** URI of the request to mock */
        uri: '/test/:id/objectDef'
        /** Method of the request (get, post, ...) */
        method: 'get',
        /** Function by default to return the mock (codes 2XX defined in the RAML). */
        mock: [Function]
        /** If you don't define a 2XX code or want to use randomly other code responses. You can use this function
          * Just use instead of mock(); -> mockByCode(418);
        mockByCode: [Function](code)
        /** Function by default to return the example (codes 2XX defined in the RAML). */
        example: [Function]
        /** The same as mockByCode but applied to examples */
        exampleByCode: [Function](code)

If you have express and underscore or lodash in your project, you can use a callback like this to create the mocked requests:

var callback = function (requestsToMock){
    _.each(requestsToMock, function(reqToMock){
        app[reqToMock.method](reqToMock.uri, function(req,res){
            var code = 200;
            if (reqToMock.defaultCode) {
                code = reqToMock.defaultCode;
            res.send(code ,reqToMock.mock());

Json-Schema Support Draft-04


This plugin supports internal references and also references to json files under the working folder of the node process running raml-mocker. For example:

    "$schema": "",
    "type": "object",
    "properties": {
        "test": {
            "format": "timestamp",
            "$ref": "#/definitions/other"
    "required": ["test"],
    "definitions": {
        "other": {
            "type": "number"
// schemas/thisSchema.json
    "$schema": "",
    "type": "object",
    "properties": {
        "test": {
            "format": "timestamp",
            "$ref": "schemas/otherSchema.json#"
    "required": ["test"],

// schemas/otherSchema.json
    "$schema": "",
    "type": "object",
    "properties": {
        "test": {
            "format": "timestamp",
            "type": "integer"

Keywords support

  • enum : ✓ yes
  • type: ✓ yes
  • allOf: ✓ yes
  • anyOf: ✓ yes
  • oneOf: ✓ yes
  • not: NO (very difficult to generate a random mock from not json-schemas)

If you need an example please check the test schema


If you have any improvement please open an issue. If you want to collaborate do not hesitate to request it or do a fork + pull request. I'll be grateful.

Part of the schema.js code is based in json-schema-mock.

History Log

  • Adds support for PATCH request
  • Fixes an issue when parsing raml files
  • Adds parserOptions (e.g. dereferenceSchemas by default is true)
  • Adds support for baseUriParameters
  • Updates raml-js-parser dependency to the new version
  • The raml-js-parser is now a git+https dependency
  • Work-around to fix dependencies resolution with circular references
  • Dependency resolution by $ref now is delegated to raml-parser
  • Adding baseUri to resource paths.
  • Accepting all media types stated in RAML 0.8 spec
  • Upgrades dependencies
  • Upgrades dependencies
  • Fixes compatibility issues.
  • Fixes null responses crash in getResponsesByCode
  • JSON schema now supports references to external files.
  • Adds the possibility to get also the responses examples defined inside RAML files.
  • Allows upper case in the format name definition. Fixes #1
  • Adds support for uniqueItems schema keyword.
  • Adds the possibility to return only the http code without a json.
  • Fixes some issues.
  • Removes unneeded packages.
  • Adds support to multiple http codes per uri & method in raml files.
  • Fixes an error without showing it in the console.
You can’t perform that action at this time.