prism icon indicating copy to clipboard operation
prism copied to clipboard

Dynamic generation of pattern with invalid value should be overwritten by example value

Open gbmarc1 opened this issue 3 years ago • 2 comments

Describe the bug

For some regex patterns, the json-schema-faker fails to generate a valid value that matches the pattern.

To Reproduce

This pattern rarely produces a valid response

    Language:
      type: object
      properties:
        languageId:
          description: ISO language code (http://www.mathguide.de/info/tools/languagecode.html)
          example: en-Latn-US
          type: string
          pattern: ^[aA-zZ]{2}(-[aA-zZ]{4})?(-[aA-zZ]{2})?$

Expected behavior

The invalid value should not be returned if there is an example in the object property.

Indeed, the invalid value will potentially create test failures that make tests flaky. I would like that the value returned by the example if provided in the case that the generated pattern doesn't match.

In the example provided above, the generator would try to create a valid value matching the pattern ^[aA-zZ]{2}(-[aA-zZ]{4})?(-[aA-zZ]{2})?$. In case of failure, the generated value would be overwritten by en-Latn-US

gbmarc1 avatar Nov 12 '21 22:11 gbmarc1

This appears to be a bug with json-schema-faker itself, i tried this in their online tool

{
  "type": "object",
  "properties": {
    "languageId": {
      "description": "ISO language code (http://www.mathguide.de/info/tools/languagecode.html)",
      "example": "en-Latn-US",
      "type": "string",
      "pattern": "^[aA-zZ]{2}(-[aA-zZ]{4})?(-[aA-zZ]{2})?$"
    }
  },
  "required": [
    "languageId"
  ]
}

daniel-white avatar Apr 22 '22 16:04 daniel-white

Yes indeed the json-schema-faker does not produce a valid pattern. However, prism could check that the generated pattern is indeed valid. If not, return the provided example in the specification as a fallback. However, I agree that the source of the problem should be solved in the json-schema-faker eventually.

gbmarc1 avatar Apr 22 '22 17:04 gbmarc1