1

我想使用 json 模式来验证这样的事情:

{
  "function_mapper": {
    "critical": [
      "DataContentValidation.get_multiple_types_columns",
      "DataContentValidation.get_invalid_session_ids"
    ],
    "warning": [
      "DataContentValidation.get_columns_if_most_data_null",
      "FeatureContentValidation.detect_inconsistencies"
    ]
  }
}

Class.function我想使用正则表达式来检查列表内容是否看起来像'dataContentValidation.get_multiple_types_columns' 这样

{
    "type": "object",
    "properties": {
        "function_mapper": {
            "type": "object",
            "properties": {
                "critical": {
                    "type": "array",
                    "uniqueItems": True,
                    "items":
                        {
                            "type": "string",
                            "pattern": r"[A-Z]\w+\.\w+"
                            # TODO add pattern that represent a class and function i.e: Class.function
                        }
                },
                "error": {
                    "type": "array",
                    "items": [
                        {
                            "type": "string",
                            "pattern": r"[A-Z]\w+\.\w+"
                            # TODO add pattern that represent a class and function i.e: Class.function
                        }
                    ]
                },
                "informative": {
                    "type": "array",
                    "items": [
                        {
                            "type": "string",
                            "pattern": r"[A-Z]\w+\.\w+"
                            # TODO add pattern that represent a class and function i.e: Class.function
                        }
                    ]
                }
            }
        },
        "days_gap": {"type": "integer", "minimum": 0},
        "timestamp_column": {"type": "string"},
        "missing_data_median_epsilon": {"type": "number", "minimum": 0, "maximum": 1},
        "group_by_time_unit": {"type": "string", "enum": ["d", "w", "m", "h", "T", "min", "s"]},
        "null_data_percentage": {"type": "number", "minimum": 0, "maximum": 1},
        "common_feature_threshold": {"type": "number", "minimum": 0, "maximum": 1},
        "columns_to_count": {"type": "array", "items": {"type": "string"}},
        "cluster_median_epsilon": {"type": "number", "minimum": 0, "maximum": 1},
        "app_session_id_column": {"type": "string"}
    }
}

我也尝试用项目替换包含但它仍然不起作用。我究竟做错了什么?

4

2 回答 2

0

我以为您使用的是粘贴为 python 的内容dict,但@Relequestual 的评论让我意识到这可能只是一个 JSON 问题。

这是我在 Python 中所做的一个最小示例,这有帮助吗?

import jsonschema
from pprint import pprint

schema = {
    "type": "object",
    "properties": {
        "array_of_strings": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": r"\w\d",  # a letter and a number
            }
        }
    },
    "additionalProperties": False,
}

validator = jsonschema.Draft4Validator(schema)

def check(obj):
    pprint(obj)
    result = "VALID" if validator.is_valid(obj) else "INVALID"
    print(f"=> {result}")

然后通过一些测试用例,它按预期通过和失败:

>>> check({"array_of_strings": []})
{'array_of_strings': []}
=> VALID

>>> check({"array_of_strings": [""]})
{'array_of_strings': ['']}
=> INVALID

>>> check({"array_of_strings": ["A4"]})
{'array_of_strings': ['A4']}
=> VALID

>>> check({"array_of_strings": ["A4", "4A"]})
{'array_of_strings': ['A4', '4A']}
=> INVALID

>>> check({"array_of_strings": ["A4"], "other_key": "123"})
{'array_of_strings': ['A4'], 'other_key': '123'}
=> INVALID

>>> check({})
{}
=> VALID

>>> check({"other_key": []})
{'other_key': []}
=> INVALID

>>> check({"array_of_strings": {}})
{'array_of_strings': {}}
=> INVALID
于 2021-06-03T13:48:32.773 回答
0

我看到您的架构有两个问题,这意味着它不是有效的 JSON(除了评论)。

出于某种原因,您r在正则表达式打开引号之前有一个。这使得 JSON 无效。

您需要在 JSON 中转义斜杠。将架构粘贴到支持 JSON 的编辑器中会突出显示此错误。

JSON 中的字符串需要一些转义...

Backspace is replaced with \b
Form feed is replaced with \f
Newline is replaced with \n
Carriage return is replaced with \r
Tab is replaced with \t
Double quote is replaced with \"
Backslash is replaced with \\

根据https://www.json.org

如果没有进一步的提示说明出了什么问题,我不确定我能否提供进一步的帮助。

于 2021-06-03T13:11:16.957 回答