10
votes

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

4
No way to do it within the specification. This could be helpful though: github.com/epoberezkin/ajv-keywords#regexpesp
what do you intend to make case-insensitive actually? the values of the key pattern? convert them to lowercase and do the comparison on lowercased values, for instance.holex
i want that json schema validates both if i write "red" or "Red" as colorsManhattan Lo Gnecco
You can add red and Red both in pattern. By keeping it case insensitive you would be allowing rEd,rED or RED which you may not like.Puneet

4 Answers

2
votes

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

like that you can use.

aliceblue - pass

antIquEWhiTe - pass

Black - fail
0
votes

Use ajv-keywords for that:

import Ajv from 'ajv';
import AjvKeywords from 'ajv-keywords';
// ajv-errors needed for errorMessage
import AjvErrors from 'ajv-errors';

const ajv = new Ajv.default({ allErrors: true });

AjvKeywords(ajv, "regexp");
AjvErrors(ajv);

// modification of regex by requiring Z https://www.regextester.com/97766
const ISO8601UTCRegex = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?Z$/;

const typeISO8601UTC = {
  "type": "string",
  "regexp": ISO8601UTCRegex.toString(),
  "errorMessage": "must be string of format 1970-01-01T00:00:00Z. Got ${0}",
};

const schema = {
  type: "object",
  properties: {
    foo: { type: "number", minimum: 0 },
    timestamp: typeISO8601UTC,
  },
  required: ["foo", "timestamp"],
  additionalProperties: false,
};

const validate = ajv.compile(schema);

const data = { foo: 1, timestamp: "2020-01-11T20:28:00" }

if (validate(data)) {
  console.log(JSON.stringify(data, null, 2));
} else {
  console.log(JSON.stringify(validate.errors, null, 2));
}

https://github.com/rofrol/ajv-regexp-errormessage-example

0
votes

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.

-2
votes

You can create an enum class for pattern which stores all the pattern values. Then create an annotation which results true for any value you have in pattern; and while putting the condition for check do var1.equalsignorecase(var2).

Annotation will be put on the class which is using it.

Class colour {
  Private string type;
  @mycustomannotation
  Private string pattern;
}