-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BDRSPS-913 Added site visit id cross validation input to site visit t…
…emplate. (#264) * Added site visit id cross validation input to site visit template. * Removed logical or foreign key logic. * Created enhanced required custom check and used that within the site visit validation. * Removed schema patching fixture from site visit tests.
- Loading branch information
1 parent
3523f9f
commit 4d9c9f6
Showing
10 changed files
with
536 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"""Provides extra frictionless bypassable row-wise required checks for the package""" | ||
|
||
# Third-Party | ||
import frictionless | ||
import frictionless.errors | ||
import attrs | ||
|
||
# Typing | ||
from typing import Iterable | ||
|
||
|
||
@attrs.define(kw_only=True, repr=False) | ||
class RequiredEnhanced(frictionless.Check): | ||
"""Checks whether specified columns are all provided in a row-wise manner. | ||
It also allows the bypassing of the constraint through the provision | ||
of whitelists. Note: This check is only effective when the original | ||
schema for each field is `required = False`, otherwise the check, | ||
does nothing. | ||
Attributes: | ||
field_names (list[str]): Names of fields in the row to be checked. | ||
whitelists (dict[str, set]): A dictionary with the key corresponding | ||
to a fieldname in the row, not necessarily provided in `field_names, | ||
which contains a set of values which will allow the check to be | ||
bypassed, if encountered as a value in any of that given row's | ||
corresponding fields' cells | ||
""" | ||
|
||
# Check Attributes | ||
type = "required-enhanced" | ||
Errors = [frictionless.errors.RowConstraintError] | ||
|
||
# Attributes specific to this check | ||
field_names: list[str] | ||
whitelists: dict[str, set] = {} | ||
|
||
def validate_start(self) -> Iterable[frictionless.Error]: | ||
"""Called to validate the resource after opening | ||
Yields: | ||
Error: found errors | ||
""" | ||
# Check whitelist keys correspond to fields | ||
for f in self.whitelists: | ||
if f not in self.resource.schema.field_names: | ||
note = f"required enhanced value check requires field {f} to exist" | ||
yield frictionless.errors.CheckError(note=note) | ||
|
||
def validate_row(self, row: frictionless.Row) -> Iterable[frictionless.Error]: | ||
"""Called to validate the given row (on every row). | ||
Args: | ||
row (frictionless.Row): The row to check the required enhanced of. | ||
Yields: | ||
frictionless.Error: For when the required enhanced is violated. | ||
""" | ||
# Retrieve Field Names for Missing Cells | ||
missing = [f for f in self.field_names if not row[f]] | ||
|
||
# Check to see if any missing fields found | ||
if len(missing) == 0: | ||
return | ||
|
||
# Determine if rule is bypassable | ||
bypassable_values = [row[k] for k, v in self.whitelists.items() if row[k] in v] | ||
if len(bypassable_values) > 0: | ||
return | ||
|
||
note = f"the columns {self.field_names} are all required" | ||
if self.whitelists: | ||
note += ( | ||
f" unless the values provided in fields {self.whitelists.keys()}" | ||
" match any of those in their supplied whitelists" | ||
) | ||
note += f" ({missing} are missing)." | ||
|
||
# Yield Error | ||
yield frictionless.errors.RowConstraintError.from_row( | ||
row=row, | ||
note=note, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.