-
Notifications
You must be signed in to change notification settings - Fork 8
/
validation.py
281 lines (235 loc) · 11 KB
/
validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Módulo 'validator' de Pydatajson
Contiene los métodos para validar el perfil de metadatos de un catálogo.
"""
from __future__ import unicode_literals, print_function
from __future__ import with_statement, absolute_import
import logging
import os
import platform
import jsonschema
from pydatajson.validators.consistent_distribution_fields_validator \
import ConsistentDistributionFieldsValidator
from pydatajson.validators.distribution_urls_validator \
import DistributionUrlsValidator
from pydatajson.validators.landing_pages_validator \
import LandingPagesValidator
from pydatajson.validators.theme_ids_not_repeated_validator \
import ThemeIdsNotRepeatedValidator
from . import readers
ABSOLUTE_PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
ABSOLUTE_SCHEMA_DIR = os.path.join(ABSOLUTE_PROJECT_DIR, "schemas")
DEFAULT_CATALOG_SCHEMA_FILENAME = "catalog.json"
logger = logging.getLogger('pydatajson')
class Validator(object):
def __init__(self, schema_filename=DEFAULT_CATALOG_SCHEMA_FILENAME,
schema_dir=ABSOLUTE_SCHEMA_DIR):
self.jsonschema_validator = \
self.init_jsonschema_validator(schema_dir, schema_filename)
def init_jsonschema_validator(self, schema_dir, schema_filename):
schema_path = os.path.join(schema_dir, schema_filename)
schema = readers.read_json(schema_path)
# Según https://github.com/Julian/jsonschema/issues/98
# Permite resolver referencias locales a otros esquemas.
if platform.system() == 'Windows':
base_uri = "file:///" + schema_path.replace("\\", "/")
else:
base_uri = "file://" + schema_path
resolver = jsonschema.RefResolver(base_uri=base_uri, referrer=schema)
format_checker = jsonschema.FormatChecker()
return jsonschema.Draft4Validator(
schema=schema, resolver=resolver, format_checker=format_checker)
def is_valid(self, catalog, broken_links=False, verify_ssl=True,
url_check_timeout=1, broken_links_threads=1):
return not self._get_errors(catalog,
broken_links=broken_links,
verify_ssl=verify_ssl,
url_check_timeout=url_check_timeout,
broken_links_threads=broken_links_threads)
def validate_catalog(self, catalog, only_errors=False,
broken_links=False, verify_ssl=True,
url_check_timeout=1):
default_response = self._default_response(catalog)
errors = self._get_errors(catalog, broken_links=broken_links,
verify_ssl=verify_ssl,
url_check_timeout=url_check_timeout)
response = default_response.copy()
for error in errors:
response = self._update_validation_response(
error, response)
# filtra los resultados que están ok, para hacerlo más compacto
if only_errors:
response["error"]["dataset"] = [
dataset for dataset in response["error"]["dataset"] if
dataset["status"] == "ERROR"]
return response
def _get_errors(self, catalog, broken_links=False, verify_ssl=True,
url_check_timeout=1, broken_links_threads=1):
errors = list(
self.jsonschema_validator.iter_errors(catalog)
)
try:
for error in self._custom_errors(
catalog, broken_links=broken_links,
verify_ssl=verify_ssl,
url_check_timeout=url_check_timeout,
broken_links_threads=broken_links_threads):
errors.append(error)
except Exception as e:
print(e)
logger.warning("Error de validación")
return errors
def _default_response(self, catalog):
return {
"status": "OK",
"error": {
"catalog": {
"status": "OK",
"title": catalog.get("title"),
"errors": []
},
# "dataset" contiene lista de rtas default si el catálogo
# contiene la clave "dataset" y además su valor es una lista.
# En caso contrario "dataset" es None.
"dataset": [
{
"status": "OK",
"title": dataset.get("title"),
"identifier": dataset.get("identifier"),
"list_index": index,
"errors": []
} for index, dataset in enumerate(catalog["dataset"])
] if ("dataset" in catalog and
isinstance(catalog["dataset"], list)) else None
}
}
# noinspection PyTypeChecker
def _custom_errors(self, catalog, broken_links=False, verify_ssl=True,
url_check_timeout=1, broken_links_threads=1):
"""Realiza validaciones sin usar el jsonschema.
En esta función se agregan bloques de código en python que realizan
validaciones complicadas o imposibles de especificar usando jsonschema
"""
validators = self._validators_for_catalog(catalog)
if broken_links:
validators.append(LandingPagesValidator(catalog, verify_ssl,
url_check_timeout,
broken_links_threads))
validators.append(DistributionUrlsValidator(catalog, verify_ssl,
url_check_timeout,
broken_links_threads))
for validator in validators:
for error in validator.validate():
yield error
@staticmethod
def _validators_for_catalog(catalog):
return [
ThemeIdsNotRepeatedValidator(catalog),
ConsistentDistributionFieldsValidator(catalog)
]
def _update_validation_response(self, error, response):
"""Actualiza la respuesta por default acorde a un error de
validación."""
new_response = response.copy()
# El status del catálogo entero será ERROR
new_response["status"] = "ERROR"
# Adapto la información del ValidationError recibido a los fines
# del validador de DataJsons
error_info = {
# Error Code 1 para "campo obligatorio faltante"
# Error Code 2 para "error en tipo o formato de campo"
"error_code": 1 if error.validator == "required" else 2,
"message": error.message,
"validator": error.validator,
"validator_value": error.validator_value,
"path": list(error.path),
# La instancia validada es irrelevante si el error es de tipo 1
"instance": (None if error.validator == "required"
else error.instance)
}
# Identifico a qué nivel de jerarquía sucedió el error.
if len(error.path) >= 2 and error.path[0] == "dataset":
# El error está a nivel de un dataset particular o inferior
position = new_response["error"]["dataset"][error.path[1]]
else:
# El error está a nivel de catálogo
position = new_response["error"]["catalog"]
position["status"] = "ERROR"
position["errors"].append(error_info)
return new_response
def is_valid_catalog(catalog, validator=None, verify_ssl=True,
url_check_timeout=1):
"""Valida que un archivo `data.json` cumpla con el schema definido.
Chequea que el data.json tiene todos los campos obligatorios y que
tanto los campos obligatorios como los opcionales siguen la estructura
definida en el schema.
Args:
catalog (str o dict): Catálogo (dict, JSON o XLSX) a ser validado.
Returns:
bool: True si el data.json cumple con el schema, sino False.
"""
catalog = readers.read_catalog(catalog)
if not validator:
if hasattr(catalog, "validator"):
validator = catalog.validator
else:
validator = Validator()
return validator.is_valid(catalog, verify_ssl=verify_ssl,
url_check_timeout=url_check_timeout)
def validate_catalog(catalog, only_errors=False, fmt="dict",
export_path=None, validator=None,
verify_ssl=True, url_check_timeout=1):
"""Analiza un data.json registrando los errores que encuentra.
Chequea que el data.json tiene todos los campos obligatorios y que
tanto los campos obligatorios como los opcionales siguen la estructura
definida en el schema.
Args:
catalog (str o dict): Catálogo (dict, JSON o XLSX) a ser validado.
only_errors (bool): Si es True sólo se reportan los errores.
fmt (str): Indica el formato en el que se desea el reporte.
"dict" es el reporte más verborrágico respetando la
estructura del data.json.
"list" devuelve un dict con listas de errores formateados para
generar tablas.
export_path (str): Path donde exportar el reporte generado (en
formato XLSX o CSV). Si se especifica, el método no devolverá
nada, a pesar de que se pase algún argumento en `fmt`.
Returns:
dict: Diccionario resumen de los errores encontrados::
{
"status": "OK", # resultado de la validación global
"error": {
"catalog": {
"status": "OK",
"errors": []
"title": "Título Catalog"},
"dataset": [
{
"status": "OK",
"errors": [],
"title": "Titulo Dataset 1"
},
{
"status": "ERROR",
"errors": [error1_info, error2_info, ...],
"title": "Titulo Dataset 2"
}
]
}
}
Donde errorN_info es un dict con la información del N-ésimo
error encontrado, con las siguientes claves: "path", "instance",
"message", "validator", "validator_value", "error_code".
"""
catalog = readers.read_catalog(catalog)
# Genero la lista de errores en la instancia a validar
if not validator:
if hasattr(catalog, "validator"):
validator = catalog.validator
else:
validator = Validator()
return validator.validate_catalog(catalog,
only_errors,
verify_ssl=verify_ssl,
url_check_timeout=url_check_timeout)