YSPEC es un verificador simple mortal para estructuras. Es especialmente útil para la validación de diferentes archivos YAML/JSON
yspec ./schema.yaml /tmp/data.jsonNota: YSPEC puede tomar YAML/JSON para el esquema y para los datos
from yspec . checker import check
# Some code that prepares data and rules
check ( data , rules )El esquema es un dictado de reglas. Cada regla hace alguna verificación de acuerdo con el campo 'coincidir'. El esquema debe incluir la regla 'raíz' que se aplica en el objeto superior en la estructura.
Por ejemplo, estructura (en yaml):
---
- ' string1 '
- ' string2 '
Será válido para el esquema (en 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 : anyHay dos de ellos: dict y lista. Ambos tienen la misma lógica de recursión. Al principio aplicamos algunas verificaciones en el objeto en sí, y luego aplicamos otra (o la misma) regla a los niños (elementos de lista o valores de DICT).
my_list :
match : list
item : some_other_ruleLa lista es un tipo de recurrente. Fist IT verifica el objeto una lista, luego verifica cada elemento de acuerdo con la regla en el attr.
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
default_item : some_other_rule
required_items :
- key2Esa es una regla que describe un dict que tiene dos claves (Key1 y Key2). Una de las claves (Key2) es obligatoria. Key1 debe verificarse de acuerdo con la regla 'String_rule', mientras que cualquier otra tecla con cualquier otro nombre (no clave1 o key2) se verificará de acuerdo con algunos_other_rule.
Si tenemos eliminación de default_item
my_list :
match : dict
items :
key1 : string_rule
key2 : integer_rule
required_items :
- key2Un dict podría ser con dos teclas (Key1, Key2) máximo
Esquema:
---
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 : listDatos:
---
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_ruleOne de COMPLETA El éxito si alguna de las reglas del éxito de las "variantes".
Supongamos que necesita verificar su pequeña lista de frutas:
---
- apple
- plumPuede usar Set Match para ello. En la cláusula Variants de Set Match, enumera todos los valores posibles para este tipo:
---
root :
match : list
item : fruits
fruits :
match : set
variants :
- apple
- orange
- plum Algunas veces debe hacer una comprobación separada para DICT que tengan algún par de clave/valor.
Por ejemplo, tiene los siguientes datos:
---
- type : type1
payload :
- 1
- 1
- type : type2
payload :
- " that is a string "
- " that is a string2 "En ese ejemplo, tiene un dict que tiene una carga útil diferente depende de la clave de tipo. Eso es posible describir con el siguiente esquema:
---
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