RapiDoc icon indicating copy to clipboard operation
RapiDoc copied to clipboard

feature: process `pattern` in generated example responses

Open llllvvuu opened this issue 2 years ago • 1 comments

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.

llllvvuu avatar Aug 19 '23 01:08 llllvvuu

I have added in this PR a generation of strings based on the pattern: https://github.com/rapi-doc/RapiDoc/pull/947

j3rem1e avatar Aug 19 '23 14:08 j3rem1e