Commit 20a9e22e authored by qloridant's avatar qloridant
Browse files

Example custom check for siret and rna

parent 5164408d
Pipeline #3953 passed with stage
in 3 minutes and 33 seconds
import stdnum.fr.siret
from frictionless import Check, errors
from .utils import build_check_error
# Name ideas :
# - transfer requirements constraint
# - dual requirement constraint
#
class SiretOrRnaError(errors.CellError):
"""Custom error."""
code = "one-of-the-two-required"
name = "Partage de la contrainte de valeur obligatoire sur 2 colonnes"
tags = ["#body"]
template = "{note}"
description = (
"Parmis ces deux colonnes, il faut obligatoirement au moins une valeur")
class SiretOrRna(Check):
"""Check that one of the two value is valid and respect a custom required
constraint"""
code = "siret-or-rna"
possible_Errors = [SiretOrRnaError]
def __init__(self, descriptor=None):
super().__init__(descriptor)
self.__column1 = self.get("column1")
self.__column2 = self.get("column2")
self.__column_siret_key_word = self.get("column1_key_word")
def validate_start(self):
if self.__column1 not in self.resource.schema.field_names:
note = f"La colonne {self.__column1!r} n'est pas trouvée."
yield build_check_error(SiretOrRnaError.code, note)
elif self.__column2 not in self.resource.schema.field_names:
note = f"La colonne {self.__column2!r} n'est pas trouvée."
yield build_check_error(SiretOrRnaError.code, note)
elif not self.__column_siret_key_word:
note = f"Le mot clé {self.__column_siret_key_word!r} est vide."
yield build_check_error(SiretOrRnaError.code, note)
def validate_row(self, row):
siret_value = row[self.__column_siret]
rna_value = row[self.__column_rna]
# Check SIRET on column 1
result_column1 = stdnum.fr.siret.is_valid(siret_value)
if result_column1:
# SIRET value is completely valid. No need to check column2
return
elif result_column1 == self.__column_siret_key_word:
# Transfer requirement constraint of column 2
if rna_value:
return
else:
note = (f"La valeur de la colonne {self.__column2!r} "
f"`{rna_value}` n'est pas valide. La contrainte de "
f"valeur obligatoire SIRET ou RNA n'est pas valide.")
yield SiretOrRnaError.from_row(row, note=note,
field_name=self.__column1)
else:
note = (
f"La valeur de la colonne {self.__column1!r} `{siret_value}`"
f" n'est pas valide et ne permet pas de transférer la "
f"contrainte de valeur obligatoire sur la colonne "
f"{self.__column2}")
yield SiretOrRnaError.from_row(row, note=note,
field_name=self.__column1)
metadata_profile = { # type: ignore
"type": "object",
"required": ["column_siret", "column_rna", "column_siret_key_word"],
"properties": {"column": {}, "column2": {}, "op": {"type": "string"}}}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment