yspec
v0.1.0
YSPEC是一个致命的简单检查器,用于结构。它对于验证不同的yaml/json文件特别有用
yspec ./schema.yaml /tmp/data.json注意:Yspec可以使用YAML/JSON进行模式和数据
from yspec . checker import check
# Some code that prepares data and rules
check ( data , rules )架构是规则的命令。每个规则都根据“匹配”字段进行检查。模式必须包括“根”规则,该规则应用于结构中的顶部对象。
例如结构(在yaml中):
---
- ' string1 '
- ' string2 '
对模式有效(在YAML中):
---
root :
match : list
item : string
string :
match : string my_awesome_string :
match : string my_awesome_bool :
match : bool my_awesome_int :
match : int my_awesome_float :
match : float empty_object :
match : none any_type :
match : any其中有两个:dict和列表。两者都具有相同的递归逻辑。首先,我们对对象本身进行一些检查,然后将另一个(或相同的)规则应用于儿童(列表元素或dict的值)。
my_list :
match : list
item : some_other_rule列表是一种经常性类型。拳头检查是对象列表,然后根据“项目” attr中的规则检查每个元素。
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
default_item : some_other_rule
required_items :
- key2这是一个描述具有两个键的dict(key1和key2)的规则。键之一(key2)是必须的。 KEY1应根据“ String_rule”规则检查,而其他任何其他具有任何其他(NON KEY1或KEY2)名称的键将根据Some_other_rule进行检查。
如果我们删除了default_item
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
required_items :
- key2一个dict可能是两个键(key1,key2)最大值
模式:
---
boolean :
match : bool
string :
match : string
integer :
match : int
float :
match : float
list :
match : list
item : string
root :
match : dict
items :
key1 : boolean
key2 : string
key3 : integer
key4 : float
key5 : list数据:
---
key1 : true
key2 : " That is a string "
key3 : 1
key4 : 1.0
key5 :
- " One more string "
- " Another string " constraint_list_item :
match : one_of
variants :
- integer_rule
- some_other_rule单一匹配成功,如果“变体”成功的任何规则。
假设您需要检查您的小水果清单:
---
- apple
- plum您可以使用设置匹配项。在set Match的变体子句中,您列出了此类型的所有提出值:
---
root :
match : list
item : fruits
fruits :
match : set
variants :
- apple
- orange
- plum 有时您需要对具有一些密钥/值对的DICE进行单独检查。
例如,您有以下数据:
---
- type : type1
payload :
- 1
- 1
- type : type2
payload :
- " that is a string "
- " that is a string2 "在那个示例中,您的dict wich具有不同的有效载荷取决于类型密钥。可以使用以下模式来描述:
---
root :
match : list
item : list_item
list_item :
match : dict_key_selection
selector : type
variants :
type1 : dict_with_int
type2 : dict_with_string
dict_with_int :
match : dict
items :
type : string
payload : list_of_int
required_items :
- type
- payload
dict_with_string :
match : dict
items :
type : string
payload : list_of_string
required_items :
- type
- payload
list_of_int :
match : list
item : int
int :
match : int
list_of_string :
match : list
item : string
string :
match : string