willfarrell / apidoc-plugin-example

@apiExample
MIT License
4 stars 8 forks source link

Use random value from examples instead of generating fake data #2

Open sspat opened 6 years ago

sspat commented 6 years ago

Hi! Awesome plugin, appreciate your work!

It would be great to have the next feature: If there are any examples specified, the plugin could randomly take a value from the examples, instead of generating fake data. That would give more control of the generated data and create more relevant examples.

Right now, using the following schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "city": {
      "type": "string",
      "examples": [
        "Moscow"
      ]
    }
  },
  "required": [
    "city"
  ]
}

The plugin generates a success example like this:

{
    "city": "eiusmod dolor"
}

It would be great, if it could generate instead this:

{
    "city": "Moscow"
}

So i can provide examples where they are relevant and leave other fields for random fake data generation.

Arxcis commented 5 years ago

Have you considered using the 'pattern'-property to achieve your goal?

Example:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "city": {
      "type": "string",
      "pattern": "Moscow|Brussels|Oslo|Stockholm|Copenhagen|London"
    }
  },
  "required": [
    "city"
  ]
}

Check that it works here -> https://json-schema-faker.js.org

Arxcis commented 5 years ago

@sspat Maybe the chance or faker-property already supports this?

{
  "type": "object",
  "properties": {
    "food": {
      "type": "string",
      "chance": {
        "pickone": [
          [
            "banana",
            "apple",
            "orange"
          ]
        ]
      }
    }
  },
  "required": [
    "food"
  ]
}

I haven't tried it but it is described here: https://github.com/json-schema-faker/json-schema-faker/blob/master/docs/USAGE.md#user-content-advanced-usage-of-fakerjs-and-chancejs