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それらの2つがあります:dictとlist。どちらも同じ再帰ロジックを持っています。最初は、オブジェクト自体にいくつかのチェックを適用してから、別の(または同じ)ルールを子供に適用します(リスト要素、またはDICTの値)。
my_list :
match : list
item : some_other_ruleリストはrecurentタイプです。 FIST IT CHECKSはオブジェクトAリストであり、「アイテム」attrのルールに従ってすべての要素をチェックします。
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
default_item : some_other_rule
required_items :
- key2これは、2つのキー(key1とkey2)を持つdictを説明するルールです。キーの1つ(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 :
- key2DICTは、最大2つのキー(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「バリアント」の成功からのルールのいずれかが成功した場合の1つの一致。
果物の小さなリストを確認する必要があると仮定します。
---
- apple
- plumセットマッチを使用できます。バリエーション節のセットマッチの句は、このタイプのすべてのposible値をリストします。
---
root :
match : list
item : fruits
fruits :
match : set
variants :
- apple
- orange
- plum キー/値のペアを持つDICTの個別のチェックを行う必要がある場合があります。
たとえば、次のデータがあります。
---
- type : type1
payload :
- 1
- 1
- type : type2
payload :
- " that is a string "
- " that is a string2 "その例では、型キーに依存する異なるペイロードの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