Skip to content

Commit e58942d

Browse files
author
Tom Reitz
committed
updates per review from John
1 parent bcec891 commit e58942d

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

lightbeam/validate.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Validator:
1414
MAX_VALIDATION_ERRORS_TO_DISPLAY = 10
1515
MAX_VALIDATE_TASK_QUEUE_SIZE = 100
1616
DEFAULT_VALIDATION_METHODS = ["schema", "descriptors", "uniqueness"]
17+
DEFAULT_FAIL_FAST_THRESHOLD = 10
1718

1819
EDFI_GENERICS_TO_RESOURCES_MAPPING = {
1920
"educationOrganizations": ["localEducationAgencies", "stateEducationAgencies", "schools"],
@@ -29,16 +30,17 @@ class Validator:
2930
def __init__(self, lightbeam=None):
3031
self.lightbeam = lightbeam
3132
self.logger = self.lightbeam.logger
32-
33+
self.fail_fast_threshold = self.lightbeam.config.get("validate",{}).get("references",{}).get("max_failures", DEFAULT_FAIL_FAST_THRESHOLD)
34+
self.validation_methods = self.lightbeam.config.get("validate",{}).get("methods",self.DEFAULT_VALIDATION_METHODS)
35+
if type(validation_methods)==str and (validation_methods=="*" or validation_methods.lower()=='all'):
36+
validation_methods = self.DEFAULT_VALIDATION_METHODS
37+
validation_methods.append("references")
38+
3339
# Validates (selected) endpoints
3440
def validate(self):
3541
self.lightbeam.api.load_swagger_docs()
36-
validation_methods = self.lightbeam.config.get("validate",{}).get("methods",self.DEFAULT_VALIDATION_METHODS)
37-
if type(validation_methods)==str and validation_methods=="*":
38-
validation_methods = self.DEFAULT_VALIDATION_METHODS
39-
validation_methods.append("references")
40-
self.logger.info(f"validating by methods {validation_methods}...")
41-
if "descriptors" in validation_methods:
42+
self.logger.info(f"validating by methods {self.validation_methods}...")
43+
if "descriptors" in self.validation_methods:
4244
# load remote descriptors
4345
asyncio.run(self.lightbeam.api.load_descriptors_values())
4446
self.lightbeam.reset_counters()
@@ -52,7 +54,7 @@ def validate(self):
5254
self.local_reference_cache = {}
5355

5456
for endpoint in self.lightbeam.endpoints:
55-
if "references" in validation_methods and "Descriptor" not in endpoint: # Descriptors have no references:
57+
if "references" in self.validation_methods and "Descriptor" not in endpoint: # Descriptors have no references:
5658
# We don't want every `do_validate_payload()` to separately have to open and scan
5759
# local files looking for a matching payload; this pre-loads local data that
5860
# might resolve references from within payloads of this endpoint.
@@ -169,7 +171,6 @@ def get_swagger_definition_for_endpoint(self, endpoint):
169171

170172
# Validates a single endpoint based on the Swagger docs
171173
async def validate_endpoint(self, endpoint):
172-
fail_fast_threshold = self.lightbeam.config.get("validate",{}).get("references",{}).get("max_failures", 10)
173174
definition = self.get_swagger_definition_for_endpoint(endpoint)
174175
data_files = self.lightbeam.get_data_files_for_endpoint(endpoint)
175176
tasks = []
@@ -185,14 +186,14 @@ async def validate_endpoint(self, endpoint):
185186
tasks.append(asyncio.create_task(
186187
self.do_validate_payload(endpoint, file_name, data, line_counter)))
187188

188-
if total_counter%self.MAX_VALIDATE_TASK_QUEUE_SIZE==0:
189+
if len(tasks) >= self.MAX_VALIDATE_TASK_QUEUE_SIZE:
189190
await self.lightbeam.do_tasks(tasks, total_counter, log_status_counts=False)
190191
tasks = []
191192
if total_counter%1000==0:
192193
self.logger.info(f"(processed {total_counter}...)")
193194

194195
# implement "fail fast" feature:
195-
if self.lightbeam.num_errors >= fail_fast_threshold:
196+
if self.lightbeam.num_errors >= self.fail_fast_threshold:
196197
self.logger.critical(f"... STOPPING; found {self.lightbeam.num_errors} >= validate.references.max_failures={fail_fast_threshold} VALIDATION ERRORS.")
197198
break
198199

@@ -207,10 +208,6 @@ async def validate_endpoint(self, endpoint):
207208

208209

209210
async def do_validate_payload(self, endpoint, file_name, data, line_counter):
210-
validation_methods = self.lightbeam.config.get("validate",{}).get("methods",self.DEFAULT_VALIDATION_METHODS)
211-
if type(validation_methods)==str and (validation_methods=="*" or validation_methods.lower()=='all'):
212-
validation_methods = self.DEFAULT_VALIDATION_METHODS
213-
validation_methods.append("references")
214211
definition = self.get_swagger_definition_for_endpoint(endpoint)
215212
if "Descriptor" in endpoint:
216213
swagger = self.lightbeam.api.descriptors_swagger
@@ -239,7 +236,7 @@ async def do_validate_payload(self, endpoint, file_name, data, line_counter):
239236
return
240237

241238
# check payload obeys Swagger schema
242-
if "schema" in validation_methods:
239+
if "schema" in self.validation_methods:
243240
try:
244241
validator.validate(payload)
245242
except Exception as e:
@@ -252,7 +249,7 @@ async def do_validate_payload(self, endpoint, file_name, data, line_counter):
252249
return
253250

254251
# check descriptor values are valid
255-
if "descriptors" in validation_methods:
252+
if "descriptors" in self.validation_methods:
256253
error_message = self.has_invalid_descriptor_values(payload, path="")
257254
if error_message != "":
258255
if self.lightbeam.num_errors < self.MAX_VALIDATION_ERRORS_TO_DISPLAY:
@@ -261,7 +258,7 @@ async def do_validate_payload(self, endpoint, file_name, data, line_counter):
261258
return
262259

263260
# check natural keys are unique
264-
if "uniqueness" in validation_methods:
261+
if "uniqueness" in self.validation_methods:
265262
params = json.dumps(util.interpolate_params(params_structure, payload))
266263
params_hash = hashlog.get_hash(params)
267264
if params_hash in distinct_params:
@@ -272,7 +269,7 @@ async def do_validate_payload(self, endpoint, file_name, data, line_counter):
272269
else: distinct_params.append(params_hash)
273270

274271
# check references values are valid
275-
if "references" in validation_methods and "Descriptor" not in endpoint: # Descriptors have no references
272+
if "references" in self.validation_methods and "Descriptor" not in endpoint: # Descriptors have no references
276273
self.lightbeam.api.do_oauth()
277274
error_message = self.has_invalid_references(payload, path="")
278275
if error_message != "":

0 commit comments

Comments
 (0)