Beispiel #1
0
 def extract_ids(self) -> Dict[str, List[str]]:
     ids: Dict = defaultdict(list)
     for row in self.body:
         resource_id = row[0].get("id")
         resource_type = row[0].get("resourceType")
         if not resource_id:
             raise ValidationError(
                 "failed to extract IDs from EngineResult: missing id in resource"
             )
         if not resource_type:
             raise ValidationError(
                 "failed to extract IDs from EngineResult: "
                 "missing resourceType in resource")
         ids[resource_type].append(resource_id)
     return ids
Beispiel #2
0
 def validate(self):
     """ """
     # xxx: required validate ```comparison_operator```
     # lt,le,gt,ge only for Date,DateTime, Integer, Float
     if self.path.context.type_is_primitive:
         if self.path.context.type_name not in (
                 "integer",
                 "decimal",
                 "instant",
                 "date",
                 "dateTime",
                 "time",
                 "unsignedInt",
                 "positiveInt",
         ) and self.comparison_operator in (
                 OPERATOR.lt,
                 OPERATOR.le,
                 OPERATOR.gt,
                 OPERATOR.ge,
         ):
             raise ValidationError(
                 f"Operator '{self.comparison_operator}' is not allowed "
                 f"for value type {self.path.context.type_name}'")
     else:
         # don't have usecase yet!
         raise NotImplementedError
Beispiel #3
0
 def validate(self, fhir_release):
     """ """
     if self.star or self._non_fhir is True:
         # no validation STAR or Non FHIR Path
         return
     context = PathInfoContext.context_from_path(self._path, fhir_release)
     if context is None:
         raise ValidationError(
             "'{0}' is not valid path for FHIR Release '{1}'".format(
                 self._raw, fhir_release.name))
Beispiel #4
0
    def _validate_root_path(self, path_string: str):
        """ """
        root_path = path_string.split(".")[0]

        if self._from:
            match = any(alias == root_path for alias, _ in self._from)
        else:
            # FIXME: find a better way to validate that we're searching on all resources
            match = root_path == "Resource"

        if not match:
            raise ValidationError(
                f"Root path '{root_path}' must be matched with from models")
Beispiel #5
0
 def validate(self):
     """ """
     # xxx: required validate ```comparison_operator```
     # lt,le,gt,ge only for Date,DateTime, Integer, Float
     if self.value.__visit_name__ not in (
             "integer",
             "decimal",
             "instant",
             "date",
             "dateTime",
             "time",
             "unsignedInt",
             "positiveInt",
     ) and self.comparison_operator in (
             OPERATOR.lt,
             OPERATOR.le,
             OPERATOR.gt,
             OPERATOR.ge,
     ):
         raise ValidationError(
             "Operator '{0!s}' is allowed for value type '{1!s}'".format(
                 self.comparison_operator.__name__, self.value.__name__))
Beispiel #6
0
 def _validate(self):
     """ """
     # validate select elements
     if any([el.star for el in self._select]) and len(self._select) > 1:
         raise ValidationError(
             "select(*) cannot co-exists other select element!")
Beispiel #7
0
    def _traverse_for_value(self, source, path_):
        """Looks path_ is innocent string key, but may content expression, function."""
        if isinstance(source, dict):
            # xxx: validate path, not blindly sending None
            if CONTAINS_INDEX_OR_FUNCTION.search(
                    path_) and CONTAINS_FUNCTION.match(path_):
                raise ValidationError(
                    f"Invalid path {path_} has been supllied!"
                    "Path cannot contain function if source type is dict")
            if CONTAINS_INDEX.match(path_):
                return navigate_indexed_path(source, path_)
            if path_ == "*":
                # TODO check if we can have other keys than resource
                return source[list(source.keys())[0]]

            return source.get(path_, None)

        elif isinstance(source, list):
            if not CONTAINS_FUNCTION.match(path_):
                raise ValidationError(
                    f"Invalid path {path_} has been supllied!"
                    "Path should contain function if source type is list")
            parts = path_.split("(")
            func_name = parts[0]
            index = None
            if len(parts[1]) > 1:
                index = int(parts[1][:-1])
            if func_name == "count":
                return len(source)
            elif func_name == "first":
                return source[0]
            elif func_name == "last":
                return source[-1]
            elif func_name == "Skip":
                new_order = list()
                for idx, no in enumerate(source):
                    if idx == index:
                        continue
                    new_order.append(no)
                return new_order
            elif func_name == "Take":
                try:
                    return source[index]
                except IndexError:
                    return None
            else:
                raise NotImplementedError
        elif isinstance(source, (bytes, str)):
            if not CONTAINS_FUNCTION.match(path_):
                raise ValidationError(
                    f"Invalid path {path_} has been supplied!"
                    "Path should contain function if source type is list")
            parts = path_.split("(")
            func_name = parts[0]
            index = len(parts[1]) > 1 and int(parts[1][:-1]) or None
            if func_name == "count":
                return len(source)
            else:
                raise NotImplementedError

        else:
            raise NotImplementedError