prism
prism copied to clipboard
Dynamic generation of pattern with invalid value should be overwritten by example value
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
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"
]
}
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.