Forum Discussion

chichepo's avatar
chichepo
Champion Level 3
2 years ago
Solved

Pattern vs. nullable

Hello the community,

 

I got this schema definition in my OpenAPI 3.0.3 Yaml file:

 

username:
   type: string
   pattern: "^[a-zA-Z]{10}$"
   example: John

 

 

For some reason, I can't use allOf but I need to support a possible null value.

So I wrote:

 

username:
    type: string
    pattern: "^[a-zA-Z]{10}$"
    nullable: true
    example: John

 

 

Does it make sense to add nullable: true since I can change the regex to include null/empty value? (i.e.: '^[a-zA-Z]{3}$|^$')

Will "nullable" contradict the pattern, if it does not include null value as well?

Without allOf, how the pattern regex and the sub type "nullable" are connected?

 

Thank you for your comments

  • Agreed. You have this covered I think !!


    ---
    Micheál Higgins | Senior Solutions Architect
    m: +353.86.235.4599
    e: micheal.higgins@smartbear.com
    [cid:image001.png@01D8C370.AE509620]<>

    smartbear.com<>
    [cid:image002.png@01D8C370.AE509620]<> [cid:image003.png@01D8C370.AE509620] <> [cid:image004.png@01D8C370.AE509620] <> [cid:image005.png@01D8C370.AE509620] <>



    We hope you found this email of interest; however, click here if you wish to unsubscribe<> or manage your email preferences. Privacy Policy.<>

    Disclaimer

    The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful.

3 Replies

  • I would’ve not use nullable. 

    I would change the pattern to be “10 upper-lower or null”  I’m not sure {0,10} would be what you want.

    • chichepo's avatar
      chichepo
      Champion Level 3

      Indeed, I do prefer to use a pattern only
      For example:

      username:
         type: string
         pattern: "^[a-zA-Z]{0,10}$"
         example: John

       or this one:

      username:
        type: string
        pattern: "^[a-zA-Z]{10}$|^$"
        example: John

       

      In any cases, "nullable: true" seems to interact quiet differently, depending on the other keywords (see this post and this one for more details). 

      So my question is: in which case,  "nullable: true" is the most appropriate?

      • mhiggins's avatar
        mhiggins
        Staff
        Agreed. You have this covered I think !!


        ---
        Micheál Higgins | Senior Solutions Architect
        m: +353.86.235.4599
        e: micheal.higgins@smartbear.com
        [cid:image001.png@01D8C370.AE509620]<>

        smartbear.com<>
        [cid:image002.png@01D8C370.AE509620]<> [cid:image003.png@01D8C370.AE509620] <> [cid:image004.png@01D8C370.AE509620] <> [cid:image005.png@01D8C370.AE509620] <>



        We hope you found this email of interest; however, click here if you wish to unsubscribe<> or manage your email preferences. Privacy Policy.<>

        Disclaimer

        The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful.