【问题标题】:make json schema pattern case insensitive使 json 模式模式不区分大小写
【发布时间】:2017-06-12 00:05:39
【问题描述】:

在我的 json 架构中,我为这样的“颜色”做了一个定义

{
    "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"
    }
}

我怎样才能使这个模式不区分大小写?

谢谢

【问题讨论】:

  • 没有办法在规范中做到这一点。不过,这可能会有所帮助:github.com/epoberezkin/ajv-keywords#regexp
  • 您实际上打算如何不区分大小写?键pattern 的值?例如,将它们转换为小写并对小写值进行比较。
  • 如果我将“红色”或“红色”写为颜色,我希望 json 模式验证两者
  • 您可以在图案中添加红色和红色。通过保持它不区分大小写,您将允许您可能不喜欢的 rEd、rED 或 RED。

标签: jsonschema json-schema-validator


【解决方案1】:

你可以使用pattern='^(?i)(AliceBlue|AntiqueWhite)$'

你可以这样使用。

aliceblue - pass

antIquEWhiTe - pass

Black - fail

【讨论】:

【解决方案2】:

我们可以使用模式实现对 anum 的不区分大小写。但是 json 模式不支持对正则表达式不敏感的“/i”。所以我们可以用我们自己的正则表达式模式来实现,而不需要使用 /i。

枚举:

月:{类型:'字符串',枚举:['可能','六月','七月']},

正则表达式模式:

月:{类型:'字符串',模式:'^([Mm][Aa][Yy]|[Jj][Uu][Nn][Ee]| [Jj][Uu][Ll[Yy])$',},

我们已经使用上述模式对输入参数进行了 fastify 模式验证。

【讨论】:

    【解决方案3】:

    为此使用 ajv 关键字:

    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

    【讨论】:

      【解决方案4】:

      您可以为存储所有模式值的模式创建一个枚举类。然后创建一个注释,该注释对于您在模式中拥有的任何值都是正确的;并在设置检查条件时执行var1.equalsignorecase(var2)

      注解将放在使用它的类上。

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

      【讨论】:

        猜你喜欢
        • 2011-03-08
        • 2012-11-04
        • 2014-07-14
        • 2016-08-21
        • 1970-01-01
        • 2019-02-18
        • 1970-01-01
        • 2015-03-03
        • 2012-12-09
        相关资源
        最近更新 更多