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