def validate_metadata(self, metadata: Dict[str, Dict[str, PrimitiveType]]):
        '''
        Validate a set of metadata key/value pairs.

        :param metadata: the metadata.
        :raises MetadataValidationError: if the metadata is invalid.
        '''
        # if not isinstance(metadata, dict):  # doesn't work
        if type(metadata) != dict and type(metadata) != _maps.FrozenMap:
            raise ValueError('metadata must be a dict')
        for k in metadata:
            sp = self._prefix_vals.shortest_prefix(k)
            if not self._vals.get(k) and not (sp and sp.value):
                raise _MetadataValidationError(
                    f'No validator available for metadata key {k}')
            for valfunc in self._vals.get(k, []):
                ret = valfunc(k, metadata[k])
                if ret:
                    raise _MetadataValidationError(f'Key {k}: ' + ret)
            for p in self._prefix_vals.prefixes(k):
                for f in p.value:
                    error = f(p.key, k, metadata[k])
                    if error:
                        raise _MetadataValidationError(
                            f'Prefix validator {p.key}, key {k}: {error}')
    def validate_metadata(self,
                          metadata: Dict[str, Dict[str, PrimitiveType]],
                          return_error_detail: bool = False):
        '''
        Validate a set of metadata key/value pairs.

        :param metadata: the metadata.
        :raises MetadataValidationError: if the metadata is invalid.

        :returns: list of errors raised during validation
        '''
        # if not isinstance(metadata, dict):  # doesn't work
        if type(metadata) != dict and type(metadata) != _maps.FrozenMap:
            raise ValueError('metadata must be a dict')
        errors = []
        for k in metadata:
            sp = self._prefix_vals.shortest_prefix(k)
            if not self._vals.get(k) and not (sp and sp.value):
                if return_error_detail:
                    errors.append(
                        self.build_error_detail(
                            f'Cannot validate controlled field "{k}", no matching validator found',
                            key=k))
                else:
                    raise _MetadataValidationError(
                        f'No validator available for metadata key {k}')
            for valfunc in self._vals.get(k, []):
                ret = valfunc(k, metadata[k])
                if ret:
                    try:
                        msg: str = ret['message']
                        subkey: Optional[str] = ret['subkey']
                    except:
                        msg = str(ret)
                        subkey = None
                    if return_error_detail:
                        errors.append(
                            self.build_error_detail(
                                f'Validation failed: "{msg}"',
                                dev_message=f'Key {k}: {msg}',
                                key=k,
                                subkey=subkey))
                    else:
                        raise _MetadataValidationError(f'Key {k}: ' + msg)
            for p in self._prefix_vals.prefixes(k):
                for f in p.value:
                    error = f(p.key, k, metadata[k])
                    if error:
                        if return_error_detail:
                            errors.append(
                                self.build_error_detail(
                                    f'Validation failed: "{error}" from validator for prefix "{p.key}"',
                                    dev_message=
                                    f'Prefix validator {p.key}, key {k}: {error}',
                                    key=k))
                        else:
                            raise _MetadataValidationError(
                                f'Prefix validator {p.key}, key {k}: {error}')
        return errors
示例#3
0
 def _validate_metadata(self, sample: Sample):
     for i, n in enumerate(sample.nodes):
         try:
             self._metaval.validate_metadata(n.controlled_metadata)
         except _MetadataValidationError as e:
             raise _MetadataValidationError(
                 f'Node at index {i}: {e.message}') from e
示例#4
0
    def _validate_metadata(self, sample: Sample, return_error_strings: bool=False):
        '''
        :params sample: sample to be validated
        :params return_exception: default=False, whether to return all errors found as a list exceptions.

        :returns: list of excpetions
        '''
        for i, n in enumerate(sample.nodes):
            try:
                error_strings = self._metaval.validate_metadata(n.controlled_metadata, return_error_strings)
            except _MetadataValidationError as e:
                raise _MetadataValidationError(f'Node at index {i}: {e.message}') from e
        return error_strings