Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make json schema pattern case insensitive

in my json schema i made a definition for "colors" like this

{
    "colors": {
        "type":"string",
        "pattern": "AliceBlue|AntiqueWhite|Aqua|Aquamarine|Azure|Beige|Bisque|Black|BlanchedAlmond|Blue|BlueViolet|Brown|BurlyWood|CadetBlue|Chartreuse|Chocolate|Coral|CornflowerBlue|Cornsilk|Crimson|Cyan|DarkBlue|DarkCyan|DarkGoldenRod|DarkGray|DarkGrey|DarkGreen|DarkKhaki|DarkMagenta|DarkOliveGreen|DarkOrange|DarkOrchid|DarkRed|DarkSalmon|DarkSeaGreen|DarkSlateBlue|DarkSlateGray|DarkSlateGrey|DarkTurquoise|DarkViolet|DeepPink|DeepSkyBlue|DimGray|DimGrey|DodgerBlue|FireBrick|FloralWhite|ForestGreen|Fuchsia|Gainsboro|GhostWhite|Gold|Gray|Grey|Green|GreenYellow|HoneyDew|HotPink|IndianRed|Indigo|Ivory|Khaki|Lavender|LavenderBlush|LawnGreen|LemonChiffon|LightBlue|LightCoral|LightCyan|LightGoldenRodYellow|LightGray|LightGrey|LightGreen|LightPink|LightSalmon|LightSeaGreen|LightSkyBlue|LightSlateGray|LightSlateGrey|LightSteelBlue|LightYellow|Lime|LimeGreen|Linen|Magenta|Maroon|MediumAquaMarine|MediumBlue|MediumOrchid|MediumPurple|MediumSeaGreen|MediumSlateBlue|MediumSpringGreen|MediumTurquoise|MediumVioletRed|MidnightBlue|MintCream|MistyRose|Moccasin|NavajoWhite|Navy|OldLace|Olive|OliveDrab|Orange|OrangeRed|Orchid|PaleGoldenRod|PaleGreen|PaleTurquoise|PaleVioletRed|PapayaWhip|PeachPuff|Peru|Pink|Plum|PowderBlue|Purple|RebeccaPurple|Red|RosyBrown|RoyalBlue|SaddleBrown|Salmon|SandyBrown|SeaGreen|SeaShell|Sienna|Silver|SkyBlue|SlateBlue|SlateGray|SlateGrey|Snow|SpringGreen|SteelBlue|Tan|Teal|Thistle|Tomato|Turquoise|Violet|Wheat|White|WhiteSmoke|Yellow|YellowGreen"
    }
}

how can i make this pattern case insensitive?

thanks

like image 683
Manhattan Lo Gnecco Avatar asked Jan 26 '17 13:01

Manhattan Lo Gnecco


2 Answers

We can achieve the case insensitive for the anum using pattern. However json schema doesn't support "/i" for regex insensitive. So we can achieve with our own regular expressions pattern without using /i.

Enum :

month: { type: 'string', enum: ['may', 'June', 'July'] },

Regex pattern:

month: { type: 'string', pattern: '^([Mm][Aa][Yy]|[Jj][Uu][Nn][Ee]| [Jj][Uu][Ll[Yy])$',},

we have used the above pattern for fastify schema validation for input parameter.

like image 117
Samarendra Majhi Avatar answered Oct 19 '22 18:10

Samarendra Majhi


You can use pattern='^(?i)(AliceBlue|AntiqueWhite)$'

like that you can use.

aliceblue - pass

antIquEWhiTe - pass

Black - fail
like image 30
Prem Kandagattla Avatar answered Oct 19 '22 18:10

Prem Kandagattla