Ejemplo n.º 1
0
 def validate_value_objects(self, value):
     if value["value"] is None:
         return None
     msgs = []
     if self.min is not None:
         for min_vo in self.min:
             if np.any(np.array(value["value"]) < min_vo["value"]):
                 msgs.append(
                     (self.error_min or self.message_min).format(
                         input=value["value"],
                         min=min_vo["value"],
                         min_op="less than",
                         labels=utils.make_label_str(value),
                         oth_labels=utils.make_label_str(min_vo),
                     )
                 )
     if self.max is not None:
         for max_vo in self.max:
             if np.any(np.array(value["value"]) > max_vo["value"]):
                 msgs.append(
                     (self.error_max or self.message_max).format(
                         input=value["value"],
                         max=max_vo["value"],
                         max_op="greater than",
                         labels=utils.make_label_str(value),
                         oth_labels=utils.make_label_str(max_vo),
                     )
                 )
     if msgs:
         raise ValidationError(
             msgs if len(msgs) > 1 else msgs[0], level=self.level
         )
     return value
Ejemplo n.º 2
0
 def _get_choice_validator(
     self,
     vname,
     choice_dict,
     param_name,
     param_spec,
     raw_data,
     ndim_restriction=False,
 ):
     choices = choice_dict["choices"]
     labels = utils.make_label_str(param_spec)
     label_suffix = f" for labels {labels}" if labels else ""
     if len(choices) < 20:
         error_template = (
             '{param_name} "{input}" must be in list of choices '
             "{choices}{label_suffix}.")
     else:
         error_template = '{param_name} "{input}" must be in list of choices{label_suffix}.'
     error = error_template.format(
         param_name=param_name,
         labels=labels,
         input="{input}",
         choices="{choices}",
         label_suffix=label_suffix,
     )
     return contrib.validate.OneOf(choices,
                                   error=error,
                                   level=choice_dict.get("level"))
Ejemplo n.º 3
0
    def apply_validator(self, value, when_value, labels, when_labels, ix=None):
        def ix2string(ix):
            return (
                f"[index={', '.join(map(str, ix))}]" if ix is not None else ""
            )

        msgs = []
        is_val_cond = self.evaluate_is_value(when_value)
        if is_val_cond:
            for validator in self.then_validators:
                try:
                    validator(value, is_value_object=True)
                except ValidationError as ve:
                    msgs.append(
                        self.then_message.format(
                            is_val=f"{self.is_operator.replace('_', ' ')} {self.is_val}",
                            submsg=str(ve),
                            labels=utils.make_label_str(labels),
                            when_labels=utils.make_label_str(when_labels),
                            ix=ix2string(ix),
                        )
                    )
        else:
            for validator in self.otherwise_validators:
                try:
                    validator(value, is_value_object=True)
                except ValidationError as ve:
                    msgs.append(
                        self.otherwise_message.format(
                            is_val=f"{self.is_operator.replace('_', ' ')} {self.is_val}",
                            submsg=str(ve),
                            labels=utils.make_label_str(labels),
                            when_labels=utils.make_label_str(when_labels),
                            ix=ix2string(ix),
                        )
                    )
        return msgs