1.0.0 • Published 4 years ago

openapi-mock-generator v1.0.0

Weekly downloads
6
License
-
Repository
github
Last release
4 years ago

OpenAPI Mock Generator

Create realistic mocked data from an OpenAPI specification, using faker.js and Chance random data generators.

npm.io

Features

  • Support for OpenAPI 3.0 (and 2.0 with conversion to 3.0)
  • Generate realistic mocks for paths/schemas using JSON Schema properties (like format. minimum, maximum, etc...), faker.js or Chance data generators.
  • Autocomplete for OpenAPI properties, Faker.js methods, and Chance methods
  • Multiple projects support with data stored locally in the browser
  • Offline support

Usage

Once you have loaded an OpenApi specification, you can navigate the tree with paths and schemas, and load a definition in the editor. You can modify the selected definition and see the generated mocked data in the editor on the right. The modified definition will be taken also by other elements that have a reference to that definition.

In the tree, there is also a special Playground item that can be used to generate data "on the fly" without modifying an existing definition.

JSON Schema properties examples

Data can be generated from the format property:

{
  "type": "object",
  "properties": {
    "date": {
      "type": "string",
      "format": "date-time"
    },
    "uuid": {
      "type": "string",
      "format": "uuid"
    }
  }
}
{
  "date": "1986-09-22T16:44:15.119Z",
  "uuid": "daf0229f-cc3b-2b50-bbe4-6dea4837b0bf"
}

Number ranges can be controlled with minimum and maximum:

{
  "type": "integer",
  "minimum": 10,
  "maximum": 42
}
21

Array items length can be controlled with minItems and maxItems:

{
  "type": "array",
  "minItems": 5,
  "maxItems": 10,
  "items": {
    "type": "number"
  }
}
[
  71856238.93752578,
  -9957501.746244192,
  -7145041.207682312,
  -53358509.39871331,
  -83770088.68054512,
  -25448017.578881666,
  45764879.26211512,
  -5112859.453620076,
  -74878171.17192386,
  46948099.995764345
]

Faker.js and Chance methods

You can use the name of a faker.js or Chance method using the x-faker or x-chance properties:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "x-chance": "company"
    },
    "description": {
      "type": "string",
      "x-faker": "company.catchPhrase"
    }
  }
}
{
  "name": "Citigroup, Inc",
  "description": "Vision-oriented foreground middleware"
}

You can pass parameters to the method using an object with the method name as a key and an array of values as parameters:

{
  "type": "object",
  "properties": {
    "date": {
      "type": "string",
      "format": "date",
      "x-faker": {
        "date.between": ["2000", "2050"]
      }
    }
  }
}
{
  "date": "2040-05-06"
}

You can pass also object as a single parameter:

{
  "type": "object",
  "properties": {
    "email": {
      "type": "string",
      "format": "email",
      "x-chance": {
        "email": {
          "domain": "acme.com"
        }
      }
    }
  }
}
{
  "email": "aci@acme.com"
}

For Faker.js, you can interpolate multiple results with the fake method:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "x-faker": {
        "fake": ["{{name.lastName}}, {{name.firstName}} {{name.suffix}}"]
      }
    }
  }
}
{
  "name": "Schmeler, Odie Jr."
}

The code generation is based on json-schema-faker, you can also check the project documentation for other examples.

License

MIT