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 )สคีมาเป็นกฎของกฎ กฎทุกข้อตรวจสอบตามฟิลด์ 'Match' สคีมาจะต้องมีกฎ 'รูท' ซึ่งใช้กับวัตถุด้านบนในโครงสร้าง
ตัวอย่างเช่นโครงสร้าง (ใน 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) เป็นสิ่งจำเป็น ควรตรวจสอบคีย์ 1 ตามกฎ 'String_Rule' ในขณะที่คีย์อื่น ๆ ที่มีชื่ออื่น ๆ (ไม่ใช่คีย์ 1 หรือคีย์ 2) จะถูกตรวจสอบตาม Some_other_Rule
หากเราลบ default_item
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
required_items :
- key2dict อาจอยู่กับสองปุ่ม (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คุณสามารถใช้ชุดจับคู่ได้ ในประโยคตัวแปรของชุดจับคู่คุณแสดงรายการค่าที่เป็นไปได้ทั้งหมดสำหรับประเภทนี้:
---
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