2

I'm using the json-schema-faker and i'm using it with Faker which allows me to get random "real" format values, like emails, image url etc.
this scheme works well:

{
  "type": "object",
  "properties": {
    "myPattern":{
      "type": "string",
      "pattern": "pattern1||pattern2"
    },
    "image": {
      "type": "string",
      "faker": "image.city"
    }
  },
  "required": [
    "myPattern",
    "image"
  ]
}

But what i really wants is that the faker will get a pattern like the property myPattern gets.
I've tried some variations but none works.
Some syntax I've tried:
"faker": "image.city||image.food"

  "faker": {
    "fake": {
      "pattern": "image.city||image.food"
    }
  }

-

  "faker": {
    "pattern": "image.city||image.food"
  }
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99

1 Answers1

2

when you use:

"faker": {
  "pattern": "image.city||image.food"
}

the code executed behind will be similar to this: faker.pattern("image.city||image.food")

To fix your specific case you need to wrap possible variations of faker calls as oneOf:

{
  "type": "object",
  "properties": {
    "myPattern":{
      "type": "string",
      "pattern": "pattern1||pattern2"
    },
    "image": {
      "type": "string",
      "oneOf": [
        { "faker": "image.city" },
        { "faker": "image.food" }
      ]
    }
  },
  "required": [
    "myPattern",
    "image"
  ]
}

It seems to be working now: http://json-schema-faker.js.org/#gist/9c5cb08965aeeb46b11ca6856251aa80/0.4.3