easy-json-schema-mock

0.0.3 • Public • Published

按jsonSchema生成mock数据

API

function getMockData(jsonSchema, rules)

传入jsonSchemarules规则,返回匹配的mock数据。

参数

参数名 类型 说明
jsonSchema Object(JSONSchema) jsonSchema数据
rules Array<Object> 模型数据

返回

返回json object

jsonSchema示例

{
  "type": "object",
  "properties": {
    "code": {
      "type": "number"
    },
    "message": {
      "type": "string"
    },
    "data": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "img": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "userStatus": {
            "type": "integer",
            "mock": {
              "mock": "@integer(60, 100)"
            }
          },
        },
        "required": [
          "img",
          "name",
          "userStatus",
        ]
      }
    }
  },
  "required": [
    "code",
    "message",
    "data"
  ]
}

rules示例

[
  {
    "dataType": [1],
    "matchMode": 3,
    "matchRule": "code",
    "matchCase": 1,
    "mockRule": "@integer(200, 600)",
    "description": null
  },
  {
    "dataType": [1],
    "matchMode": 3,
    "matchRule": "img",
    "matchCase": 1,
    "mockRule": "@image('200x100', '#FF6600')",
    "description": null
  },
  {
    "dataType": [1, 2, 3],
    "matchMode": 2,
    "matchRule": "id|num|code|amount|quantity|price|discount|balance|money",
    "matchCase": 1,
    "mockRule": "@natural(1,100)",
    "description": "常见数字型"
  },
  {
    "dataType": [1],
    "matchMode": 2,
    "matchRule": "image|img|photo|pic",
    "matchCase": 1,
    "mockRule": "@image('400x400')",
    "description": "图片"
  },
  {
    "dataType": [1],
    "matchMode": 2,
    "matchRule": "avatar|icon",
    "matchCase": 1,
    "mockRule": "@image(//\"200x100//\")",
    "description": "头像、icon"
  }
]

node中使用(cjs)

// easy-json-schema-mock
const ejsMock = require('easy-json-schema-mock/dist/mock.cjs.js')
// schema data
const schema = require('./data/data02.json')
// rules
const rules = require('./data/rules.json')
// get mockData
const mockData = ejsMock.getMockData(schema, rules)

前端中使用(esm)

// easy-json-schema-mock
import ejsMock from 'easy-json-schema-mock'
// schema data
const schema = require('./data/data02.json')
// rules
const rules = require('./data/rules.json')
// get mockData
const mockData = ejsMock.getMockData(schema, rules)

Readme

Keywords

none

Package Sidebar

Install

npm i easy-json-schema-mock

Weekly Downloads

2

Version

0.0.3

License

ISC

Unpacked Size

29.6 kB

Total Files

8

Last publish

Collaborators

  • zhijie.huang