Contents

ARST打卡第68周[68/521]

Algorithm

201_数字范围按位与

Review

TED演讲:怎么才算成功的人生?如何才能走向成功?

相信自己,超越自己

Tips

[区块链] 共识算法之争(PBFT,Raft,PoW,PoS,DPoS,Ripple)

Share

参数校验(不懂就看schema官网:http://json-schema.org/understanding-json-schema/)

嵌套参数的参数校验

一般的简单参数校验, 其参数一般是没有嵌套的

1
2
3
4
{
    "status": "xxx",        //用户状态(enable启用/disable禁用)
    "ids": ["xxx", "xxx"]
}

因此其校验文本为

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
VALID_OF_ACCOUT_UPDATE_MANY_STATUS = {
	"definitions": {},
	"$schema": "http://json-schema.org/draft-07/schema#",
	"type": "object",
	"required": [
		"status",
		"ids"
	],
	"properties": {
		"status": {
			"type": "string",
			"enum": ["enable", "disable"]
		},
		"ids": {
			"type": "array",
			"items": {
				"type": "string",
				"pattern": "^(.*)$"
			}
		}
	}
}

然而面对比较复杂的参数,我们应该如何写参数校验?

  • 有嵌套
  • 有限定值
  • 有取值范围

举例数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
    "name": "模板名称",
    "desc": "模板策略描述",
    "redundancy_strategy": "rep2|ec",
    "small_file_merge": {
        "small_file_merge_enable": "enable",
        "file_size": 512
    },
    "client_cache": {
        "metadata_cache": "enable",
        "data_cache": 0, // 0关闭 1读 2读写
        "prefetch": "low"
    }
}

因此我们可以写校验文本如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## -*- coding: utf-8 -*-

VALID_OF_STORAGE_STRATEGY_CREAT = {
    "definitions": {},
	"$schema": "http://json-schema.org/draft-07/schema#",
	"type": "object",
	"required": [
		"name",
		"redundancy_strategy",
		"small_file_merge",
        "client_cache"
	],
	"properties": {
		"name": {
			"type": "string",
			"pattern": "^(.*)$"
		},
        "desc": {
			"type": "string",
			"pattern": "^(.*)$"
		},
		"redundancy_strategy": {
			"type": "string",
			"enum": ["rep2", "ec"]
		},
		"small_file_merge": {
            "type": "object",
            "properties": {
                "small_file_merge_enable": {
                    "type": "string",
                    "enum": ["enable", "disable"]
                },
                "file_size": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 4096
                }
            },
            "required": ["small_file_merge_enable"]
        },
        "client_cache": {
            "type": "object",
            "properties": {
                "metadata_cache": {
                    "type": "string",
                    "enum": ["enable", "disable"]
                },
                "data_cache": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 2
                },
                "prefetch": {
                    "type": "string",
                    "enum": ["low", "middle", "high"]
                }
            },
            "required": ["metadata_cache", "data_cache", "prefetch"]
        }
	}
}