Skip to content

Commit c9d5bba

Browse files
committed
fix(schema lint defaults): use a recursive function to strip all instances of "required" key from schema for linting
1 parent 639e419 commit c9d5bba

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

nf_core/pipelines/schema.py

+16-30
Original file line numberDiff line numberDiff line change
@@ -322,35 +322,7 @@ def validate_default_params(self):
322322
if self.schema is None:
323323
log.error("[red][✗] Pipeline schema not found")
324324
try:
325-
# TODO add support for nested parameters
326-
# Make copy of schema and remove required flags
327-
schema_no_required = copy.deepcopy(self.schema)
328-
if "required" in schema_no_required:
329-
schema_no_required.pop("required")
330-
for keyword in ["allOf", "anyOf", "oneOf"]:
331-
if keyword in schema_no_required:
332-
for i, kw_content in enumerate(schema_no_required[keyword]):
333-
if "required" in kw_content:
334-
schema_no_required[keyword][i].pop("required")
335-
schema_no_required[keyword] = [
336-
kw_content for kw_content in schema_no_required[keyword] if kw_content
337-
]
338-
if not schema_no_required[keyword]:
339-
schema_no_required.pop(keyword)
340-
for group_key, group in schema_no_required.get(self.defs_notation, {}).items():
341-
if "required" in group:
342-
schema_no_required[self.defs_notation][group_key].pop("required")
343-
for keyword in ["allOf", "anyOf", "oneOf"]:
344-
if keyword in group:
345-
for i, kw_content in enumerate(group[keyword]):
346-
if "required" in kw_content:
347-
schema_no_required[self.defs_notation][group_key][keyword][i].pop("required")
348-
schema_no_required[self.defs_notation][group_key][keyword] = [
349-
kw_content for kw_content in group[keyword] if kw_content
350-
]
351-
if not group[keyword]:
352-
schema_no_required[self.defs_notation][group_key].pop(keyword)
353-
jsonschema.validate(self.schema_defaults, schema_no_required)
325+
jsonschema.validate(self.schema_defaults, strip_required(self.schema))
354326
except jsonschema.exceptions.ValidationError as e:
355327
log.debug(f"Complete error message:\n{e}")
356328
raise AssertionError(f"Default parameters are invalid: {e.message}")
@@ -366,7 +338,7 @@ def validate_default_params(self):
366338
self.get_wf_params()
367339

368340
# Go over group keys
369-
for group_key, group in schema_no_required.get(self.defs_notation, {}).items():
341+
for group_key, group in self.schema.get(self.defs_notation, {}).items():
370342
group_properties = group.get("properties")
371343
for param in group_properties:
372344
if param in self.ignored_params:
@@ -1042,3 +1014,17 @@ def get_web_builder_response(self):
10421014
f"Pipeline schema builder returned unexpected status ({web_response['status']}): "
10431015
f"{self.web_schema_build_api_url}\n See verbose log for full response"
10441016
)
1017+
1018+
1019+
def strip_required(node):
1020+
if isinstance(node, dict):
1021+
return {
1022+
k: y
1023+
for k, v in node.items()
1024+
for y in [strip_required(v)]
1025+
if k != "required" and (y or y is False or y == "")
1026+
}
1027+
elif isinstance(node, list):
1028+
return [y for v in node for y in [strip_required(v)] if y or y is False or y == ""]
1029+
else:
1030+
return node

0 commit comments

Comments
 (0)