YSPEC는 구조에 대한 치명적인 단순 체커입니다. 다른 YAML/JSON 파일의 검증에 특히 유용합니다.
yspec ./schema.yaml /tmp/data.json참고 : YSPEC는 Schema 및 데이터에 대해 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 and list. 둘 다 동일한 재귀 논리를 가지고 있습니다. 처음에는 개체 자체에 약간의 검사를 적용한 다음 다른 (또는 동일한) 규칙을 어린이 (목록 요소 또는 DITT 값)에 적용합니다.
my_list :
match : list
item : some_other_rule목록은 반복 유형입니다. Fist It Checks는 목록의 개체이며 '항목'attr의 규칙에 따라 모든 요소를 확인합니다.
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
default_item : some_other_rule
required_items :
- key2그것은 두 개의 키 (key1 및 key2)가있는 덕트를 설명하는 규칙입니다. 키 (key2) 중 하나는 필수입니다. key1은 'string_rule'규칙에 따라 점검해야하며, 다른 키는 다른 키 (비 Key1 또는 key2) 이름을 가진 다른 키는 sode_other_rule에 따라 점검됩니다.
default_item을 제거한 경우
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
required_items :
- key2DICT는 두 개의 키 (key1, key2) Maxims가있을 수 있습니다.
개요:
---
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세트 일치를 사용할 수 있습니다. 세트 매치의 변형 절 에서이 유형의 모든 입상 가능한 값을 나열합니다.
---
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 "이 예에서는 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