Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: process pattern in generated example responses #951

Open
llllvvuu opened this issue Aug 19, 2023 · 1 comment
Open

feature: process pattern in generated example responses #951

llllvvuu opened this issue Aug 19, 2023 · 1 comment

Comments

@llllvvuu
Copy link

Right now the pattern just returns the raw stringified regex, for example

"orderExpirationTimestamp": {
  "oneOf": [
    {
      "type": "string",
      "pattern": "^\\d+$"
    },
    {
      "type": "integer",
      "minimum": 0
    }
  ],

renders as:

"orderExpirationTimestamp": "^\d+$"

A nicer string could be generated using https://github.com/fent/randexp.js . Instead of a randomly generated string, the PRNG can be overridden to return the simplest/cleanest string:

> const RandExp = require('randexp')
> RandExp.prototype.randInt = (from, to) => from
> /** e-mail */ new RandExp(/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/).gen()
'_@_._'
> /** numeric string */ new RandExp(/^\d+/).gen() // numeric string
'0'
> /* ISO 8601 */ new RandExp(/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/).gen()
'0000-00-00T00:00:00.0+00:00'
> /** airport code */ new RandExp(/^[A-Z]{3}/).gen()
'AAA'
> /** URL */ new RandExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)/).gen()
'http://--.aa'
> /** IPv6 */ new RandExp(/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/).gen()
'0:0:0:0:0:0:0:0'
> /** Phone number */ new RandExp(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/).gen()
'0000000000'

I'm happy to make a PR if this is considered an acceptable feature. It would be easiest if someone could show me where the examples are generated, but I'm sure I can find it.

@j3rem1e
Copy link
Contributor

j3rem1e commented Aug 19, 2023

I have added in this PR a generation of strings based on the pattern: #947

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants