Define a Contant String Value

7 views
Skip to first unread message

Greg Fox

unread,
Apr 18, 2018, 5:22:18 PM4/18/18
to Swagger
In my API I have a set of regex patterns that I use throughout the document for the "pattern" field. I want to make some sort of a constant values that I can re-use instead of having a bunch of copies of the same regex pattern. This is for readability and to reduce the locations that have to be updated if the regex is changed.

I've tried a few things. The most recent was to define an extension as shown below. The example shows two ways that I tried to do it. The first was just direct properties with values and the last one, "objected" was by creating an extension called "x-val" so that I could specify the type as "string.

Is there a way to do what I am trying to do with Swagger 2.0?

x-formatting:
  username: "/^[A-Z0-9][A-Z0-9-]+[A-Z0-9]$/i"
  targetname: "/^[A-Z0-9\\-\\._]+$/i"
  groupname: "/^[A-Z0-9][A-Z0-9-]+[A-Z0-9]$/i"
  email: "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,20}$/i"
  objectid:
    type: string
    x-val: "/^[0-9a-fA-F]{24}$/"

... and here are the attempted references

pattern:
    $ref: "#/x-formatting/username"

pattern:
    $ref: "#/x-formatting/objectid/x-val"

The error I am getting is that the pattern field is expecting a "string"

Ron Ratovsky

unread,
May 1, 2018, 3:22:53 PM5/1/18
to swagger-sw...@googlegroups.com

Unfortunately, the spec doesn’t support referencing pattern values, you’d have to copy them, or reuse the object that contains them.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
swagger-swaggers...@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages