Many developers love the solution I wrote in How to Mock an API in ONE minute. In the article, I introduced an approach to mock an API rapidly.
But what I wrote is only a tiny part of the tool. After a period of exploring, I found more valuable features to mock complex data faster.
But some fields are complicated; you can't mock them without any settings.
For example, an enum field has several optional values. It usually appears in status fields.
Or an array with a fixed length. It's often used in a table where each row has several attributes, and each page has 20 or 50 rows.
Or, an object refers to a schema. OpenAPI specification allows an API to refer to a schema as an object or part of an object.
Luckily, now we can quickly mock all these complex JSON in two minutes in Apidog.
There are three steps to mock the API.
- Import API definitions.
- Set specific fields.
- Get the mock URL.
So easy. Let's mock it!
Step 1 is the same as How to Mock an API in ONE minute. You just need an OpenAPI JSON or YAML and import it into Apidog.
In this sample API, the 'status' field has three optional values: available, pending, and sold. So I hope this field in my mocking data has only one of these values too.
Click 'More' in the 'status' field. You'll see an 'Enum' switch. Turn it on, and you'll be able to add enum values. After that, data of this field will be mocked according to the settings.
PhotoUrls is an array field. I need it to have a fixed five items.
Click 'More' in the 'PhotoUrls' field. You'll see an 'Element Limit' setting. Set the minItems 5 and maxItems 5.
Apidog support schemas in OpenAPI specification. So you just need to select a Schema for an object, and Apidog will mock the object according to the schema.
For example, the 'Category' field.
Click ‘Save', and switch to the 'API' tab. Copy the URL in the "Local Mock" part which starts with "127.0.0.1".
Paste the URL into the browser.
Notice these fields: photoURLs with fixed item quantity(5), while URLs are mocked automatically with available image URLs; status with enum values(sold, pending, or available), and category refers to a schema. The other fields, like city and createdAt are mocked automatically.
And if you reload the URL, the mock data refreshes.
OpenAPI 3.0 data types are based on JSON Schema, which describes the data structure of APIs.
Apidog is compatible with OpenAPI. Length range, format, and pattern(regular expression) of strings, min and max value of integers, readonly/writeonly; all attributes in JSON Schema are supported.
Not only JSON but XML is also supported to mock. You can even set the tag names and prefixes of XML data.
Based on JSON Schema, any complex mock data can be generated easily with a few mouse clicks. Still, you don't need to write any scripts.
Just enjoy it.
Download Apidog here, and wait for my next post to see more powerful mock features.