简体   繁体   English

如何使 json 模式足够灵活以处理枚举的区分大小写的输入?

[英]how to make the json schema flexible enough to handle case sensitive input for enum?

Please refer to this question for the introduction Two way binding dependences based on enum value in json schema . json schema 中基于枚举值的两种方式绑定依赖的介绍请参考这个问题。

Now the case is that if I pass userType = CUSTOMER then it doesn't accept the request and in also the case of userType = customer .现在的情况是,如果我通过userType = CUSTOMER那么它不接受请求,在userType = customer的情况下也是如此。

Can anyone suggest me JSON schema solution for this?任何人都可以为此建议我 JSON 模式解决方案吗?

There isn't a way to do this that isn't awful, but the least awful thing I can think of is to use pattern with a regex that includes both cases.没有办法做到这一点并不可怕,但我能想到的最不可怕的事情是将pattern与包含这两种情况的正则表达式一起使用。

{
  "type": "string",
  "anyOf": [
    { "title": "Customer", "pattern": "[Cc][Uu][Ss][Tt][Oo][Mm][Ee][Rr]" },
    { "title": "Admin", "pattern": "[Aa][Dd][Mm][Ii][Nn]" },
  ]
}

(The title isn't necessary, it's just a nicety for the poor dev who has to read this schema) title不是必需的,对于必须阅读此架构的可怜的开发人员来说只是一个好处)

This was the exact use case I was trying to solve from the last couple of days, One thing that is clear is schema does not support it directly.这是我过去几天试图解决的确切用例,很明显的一件事是架构不直接支持它。

Now if you are using some kind of third-party schema validator tool, they might give you some interface to write you own customize validator ( https://github.com/everit-org/json-schema )现在,如果您使用某种第三方模式验证器工具,他们可能会给您一些接口来编写您自己的自定义验证器( https://github.com/everit-org/json-schema

There is another way that we can do is preprocess the data before validating it.我们可以做的另一种方法是在验证数据之前对数据进行预处理。 Let's say in your case userType is the value that is case insensitive and is a set of values only (Customer, customer, CUSTOMER).假设在您的情况下,userType 是不区分大小写的值,并且仅为一组值(客户、客户、客户)。 So I would suggest keeping the enum value as customer [lowercase] and before your JSON is going to schema validator preprocess it to put all the values in the specific key as lowercase value.因此,我建议将枚举值保留为customer [小写],并且在您的 JSON 将模式验证器预处理之前,将特定键中的所有值作为小写值。

The code to preprocess your JSON might look as to below-预处理您的 JSON 的代码可能如下所示 -

CONVERT_TO_LOWER_CASE_KEY_SET - Set of keys that are case insensitive while validation. CONVERT_TO_LOWER_CASE_KEY_SET - 验证时不区分大小写的键集。

private JsonObject preProcessInputJsonObject(JsonObject inputJsonBody) {
   inputJsonBody.forEach(innerObject -> {
        if (innerObject.getValue() instanceof String) {
            String value = (String) innerObject.getValue();
            if (value.trim().isEmpty())
                inputJsonBody.putNull(innerObject.getKey());
            else if (CONVERT_TO_LOWER_CASE_KEY_SET.contains(innerObject.getKey()))
                inputJsonBody.put(innerObject.getKey(), value.trim().toLowerCase());
            else
                inputJsonBody.put(innerObject.getKey(), value.trim());
        }
        if (innerObject.getValue() instanceof JsonObject)
            inputJsonBody.put(innerObject.getKey(), preProcessInputJsonObject((JsonObject) innerObject.getValue()));
        if (innerObject.getValue() instanceof JsonArray)
            inputJsonBody.put(innerObject.getKey(), preProcessJsonArray((JsonArray) innerObject.getValue()));
    });
    return inputJsonBody;
}

private JsonArray preProcessJsonArray(JsonArray inputJsonArray) {
    List jsonArrayList = inputJsonArray.copy().getList();
    final int size = jsonArrayList.size();
    for (int iterator = 0; iterator < size; iterator++) {
        if (jsonArrayList.get(iterator) instanceof String) {
            String val = (String) jsonArrayList.get(iterator);
            if (val.trim().isEmpty())
                jsonArrayList.set(iterator, null);
            else
                jsonArrayList.set(iterator, val.trim());
        }
        if (jsonArrayList.get(iterator) instanceof JsonObject)
            jsonArrayList.set(iterator, preProcessInputJsonObject((JsonObject) jsonArrayList.get(iterator)));
        if (jsonArrayList.get(iterator) instanceof JsonArray)
            jsonArrayList.set(iterator, preProcessJsonArray((JsonArray) jsonArrayList.get(iterator)));
    }
    return new JsonArray(jsonArrayList);
}

Note: I also tried removing extra spaces in the string before validating it.注意:我还尝试在验证字符串之前删除字符串中的多余空格。

Hope its helpful, let me know if you see any concern in it.希望它有帮助,如果您看到任何问题,请告诉我。

One final thing my preprocess data code in java hopefully, you can write something similar in js as well.我希望用 java 预处理数据代码的最后一件事,你也可以用 js 编写类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM