Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free

Mock.js Syntax Specification

Start for free
Contents
Home / Basic Knowledge / Mock.js Syntax Specification

Mock.js Syntax Specification

Introduction to the syntax specification of Mock.js, which is divided into two parts: the definition specification of data templates and the definition specification of data placeholders. This will help you better use Mock.js to simulate data and improve development efficiency.

Mock.js is a commonly used JavaScript library for generating random data and intercepting Ajax requests. When calling Mock. To generate Mock data, you need to follow the syntax specification of Mock.js.

The syntax specification of Mock.js includes two parts:

  1. Data Template Definition, DTD
  2. Data Placeholder Definition, DPD

DTD (Data Template Definition)

Each property in the data template consists of three parts: property name, generation rule, and property value.

'name|rule': value

In this syntax, 'name' is the property name, 'rule' is the generation rule, and 'value' is the property value. Note: The meaning of the generation rule is related to the property value type. In other words, the same generation rule may have different meanings for different property value types.

Here we will introduce this specification in conjunction with different property value types.

When the property value is a string.

  1. 'name|min-max': string

Generate a string by repeating another string, with the number of repetitions greater than or equal to min and less than or equal to max. For example, to generate a string of the "star" character repeated 2-5 times:

Mock.mock({
  'content|2-5': 'star'
})
// => {content: 'starstarstar'}
  1. 'name|count': string

Generate a string by repeating another string, with the number of repetitions equal to count. For example, to generate a string of the "star" character repeated 5 times:

Mock.mock({
  'content|5': 'star'
})
// => {content: 'starstarstarstarstar'}

When the property value is a number

  1. 'name|+1': number

Automatically increment the property value by 1, with the initial value set to number. This is typically used in generating list data. For example, to generate an array of IDs with auto-incremented values:

Mock.mock({
// The value of the 'list' property is an array containing 1 to 10 elements
'list|1-10': [{
'id|+1': 1, // The 'id' property increments starting from 1
}]
})
// => [{id: 1}, {id: 2}, {id: 3}]
  1. 'name|min-max': number

To generate an integer greater than or equal to min and less than or equal to max, using the number property value to determine the type. For example, to generate an integer between 10 and 100:

Mock.mock({
  'count|10-100': 1 
})
// => {count: 72}
  1. 'name|min-max.dmin-dmax': number

To generate a floating-point number with an integer part greater than or equal to min and less than or equal to max, and a decimal part with dmin to dmax digits. For example, to generate a floating-point number between 10 and 100, with 1-2 decimal places:

Mock.mock({
  'count|10-100.1-2': 1.1
})
// => {count: 13.18}

When the Property Value is a Boolean

  1. 'name|1': boolean

To randomly generate a boolean value, with a 50% chance of being true and a 50% chance of being false. For example:

Mock.mock({
  'value|1': true
})
// => {value: true}
  1. 'name|min-max': value

To randomly generate a boolean value, with a probability of min / (min + max) of being !value and a probability of max / (min + max) of being 'not value'. For example, to generate a boolean value with a 20% chance of being true:

Mock.mock({
  'value|1-4': true
})
// => {value: false}

When the Property Value is an Object

  1. 'name|count': object

Randomly select count attributes from the attribute values in the object. For example:

Mock.mock({
  'info|1': {id: 1, name: 'Joel', phone: '138xxxx', email: 'joel@xx.com'}
})
// => {info: {phone: '138xxxx'}}
  1. 'name|min-max': object

Randomly select min to max number of attributes from the attribute values in the object. For example:

Mock.mock({
  'info|1-3': {id: 1, name: 'Joel', phone: '138xxxx', email: 'joel@xx.com'}
})
// => {info: {phone: '138xxxx', name: 'Joel' }}

When the Property Value is an Array

  1. 'name|1': array

Randomly select one element from the attribute values array as the final value. For example:

Mock.mock({
  'info|1': [1, 2, 3, 4, 5]
})
// => {info: 2}
  1. 'name|+1': array

Randomly select one element from the attribute values array as the final value. For example:

const arr = [1, 2, 3, 4, 5]
Mock.mock({
    'list|1-3': [{
        'value|+1': arr, // The id starts from 1 and increments itself.
    }]
})
// => {list: [{value: 1}, {value: 2}]}
  1. 'name|min-max': array

Generate a new array by repeating the attribute values in array, with the number of repetitions greater than or equal to min and less than or equal to max. For example:

const arr = [1, 2, 3]
Mock.mock({
    'list|1-6': arr
})
// => {list: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]}
  1. 'name|count': array

Generate a new array by repeating the attribute values in array, with the number of repetitions equal to count. For example:

const arr = [1, 2, 3]
Mock.mock({
    'list|2': arr
})
// => {list: [1, 2, 3, 1, 2, 3]}

When the property value is a Function

'name': function

Execute thefunction and take its return value as the final attribute value. The context of the function should be the object where the attribute 'name'is located. For example:

Mock.mock({
    'name': () => 'Joel'
})

// => {name: 'Joel'}

When the property Value is a Regexp

'name': regexp

Generate a string that can match the regular expression regexp. This is useful for generating custom-formatted strings. For example, to generate a random string of length 6 that contains numbers or letters:

Mock.mock({
    'str': /[0-9a-zA-Z]{6}/
})

// => {str: 'p2otzf'}

DPD (Data Placeholder Definition)

A placeholder is just a placeholder in the attribute value string and does not appear in the final attribute value.

The format of a placeholder is:

@placeholder
@placeholder(argument [, argument])

The placeholder references a method in Mock.Random or an attribute in the data template. If an attribute in the data template has the same name as a method in Mock.Random, the attribute in the data template takes precedence. For example:

Mock.mock({
    name: {
        first: '@FIRST',
        last: '@LAST',
        full: '@first @last'
    }
})
// => {name: {first: 'Cynthia', last: 'Williams', full: 'Cynthia Williams'}}

In the code above, the first and last attributes reference Mock.Random.first() andMock.Random.last(), respectively. The full attribute references an attribute in the data template.

The Mock.js syntax definition ends here. Do you want to master Mock? Mocking is made simpler with Apidog, a potent API documentation management tool. The Apidog syntax extends some Mock-incompatible syntax and is entirely compatible with Mock. Test it out!