Exemplo n.º 1
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if contains := scope.sibling("contains"):
         if (contains_annotation := contains.annotations.get("contains")) and \
                 len(contains_annotation.value) > self.json:
             scope.fail(
                 instance, 'The array has too many elements matching the '
                 f'"contains" subschema (maximum {self.json})')
Exemplo n.º 2
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        if len(instance) == 0:
            return

        elif isinstance(self.json.value, bool):
            self.json.evaluate(instance, scope)

        elif isinstance(self.json, JSONSchema):
            for index, item in enumerate(instance):
                self.json.evaluate(item, scope)

            if scope.valid:
                scope.annotate(instance, "items", True)

        elif self.json.type == "array":
            eval_index = None
            err_indices = []
            for index, item in enumerate(instance[:len(self.json)]):
                eval_index = index
                with scope(str(index)) as subscope:
                    self.json[index].evaluate(item, subscope)
                    if not subscope.valid:
                        err_indices += [index]

            if err_indices:
                scope.fail(instance,
                           f"Array elements {err_indices} are invalid")
            else:
                scope.annotate(instance, "items", eval_index)
Exemplo n.º 3
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        self.json.evaluate(instance, scope)

        if scope.valid:
            scope.fail(instance,
                       'The instance must not be valid against the subschema')
        else:
            scope.errors.clear()
Exemplo n.º 4
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     try:
         if instance.value % self.json.value != 0:
             scope.fail(instance,
                        f"The value must be a multiple of {self.json}")
     except decimal.InvalidOperation:
         scope.fail(instance,
                    f"Invalid operation: {instance} % {self.json}")
Exemplo n.º 5
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     scope.annotate(instance, self.key, self.json.value)
     if self.validator is not None:
         try:
             self.validator(instance.value)
         except ValueError as e:
             scope.fail(instance, f'The instance is invalid against the "{self.json.value}" format: {e}')
     else:
         scope.noassert()
Exemplo n.º 6
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        err_names = []
        for name in instance:
            if not self.json.evaluate(JSON(name), scope).valid:
                err_names += [name]

        if err_names:
            scope.errors.clear()
            scope.fail(instance, f"Property names {err_names} are invalid")
Exemplo n.º 7
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        if not self.json.value:
            return

        uniquified = []
        for item in instance:
            if item not in uniquified:
                uniquified += [item]

        if len(instance) > len(uniquified):
            scope.fail(instance, "The array's elements must all be unique")
Exemplo n.º 8
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        types = tuplify(self.json.value)
        if instance.type in types:
            valid = True
        elif instance.type == "number" and "integer" in types:
            valid = instance.value == int(instance.value)
        else:
            valid = False

        if not valid:
            scope.fail(instance, f"The instance must be of type {self.json}")
Exemplo n.º 9
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        err_indices = []
        for index, subschema in enumerate(self.json):
            with scope(str(index)) as subscope:
                subschema.evaluate(instance, subscope)
                if not subscope.valid:
                    err_indices += [index]

        if err_indices:
            scope.fail(
                instance,
                f'The instance is invalid against subschemas {err_indices}')
Exemplo n.º 10
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        valid = False
        for index, subschema in enumerate(self.json):
            with scope(str(index)) as subscope:
                subschema.evaluate(instance, subscope)
                if subscope.valid:
                    valid = True

        if not valid:
            scope.fail(
                instance,
                f'The instance must be valid against at least one subschema')
Exemplo n.º 11
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        missing = {}
        for name, dependents in self.json.items():
            if name in instance:
                missing_deps = [
                    dep for dep in dependents if dep.value not in instance
                ]
                if missing_deps:
                    missing[name] = missing_deps

        if missing:
            scope.fail(
                instance,
                f"The object is missing dependent properties {missing}")
Exemplo n.º 12
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        annotation = []
        for index, item in enumerate(instance):
            if self.json.evaluate(item, scope).valid:
                annotation += [index]
            else:
                scope.errors.clear()

        scope.annotate(instance, self.key, annotation)
        if not annotation:
            scope.fail(
                instance,
                'The array does not contain any element that is valid '
                f'against the "{self.key}" subschema')
Exemplo n.º 13
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        eval_index = None
        err_indices = []
        for index, item in enumerate(instance[:len(self.json)]):
            eval_index = index
            with scope(str(index)) as subscope:
                self.json[index].evaluate(item, subscope)
                if not subscope.valid:
                    err_indices += [index]

        if err_indices:
            scope.fail(instance, f"Array elements {err_indices} are invalid")
        elif eval_index is not None:
            if eval_index == len(instance) - 1:
                eval_index = True
            scope.annotate(instance, self.key, eval_index)
Exemplo n.º 14
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        annotation = []
        err_names = []
        for name, item in instance.items():
            if name in self.json:
                with scope(name) as subscope:
                    self.json[name].evaluate(item, subscope)
                    if subscope.valid:
                        annotation += [name]
                    else:
                        err_names += [name]

        if err_names:
            scope.fail(instance, f"Properties {err_names} are invalid")
        else:
            scope.annotate(instance, self.key, annotation)
Exemplo n.º 15
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        if contains := scope.sibling("contains"):
            contains_count = len(contains_annotation.value) \
                if (contains_annotation := contains.annotations.get("contains")) \
                else 0

            valid = contains_count >= self.json

            if valid and not contains.valid:
                max_contains = scope.sibling("maxContains")
                if not max_contains or max_contains.valid:
                    contains.errors.clear()

            if not valid:
                scope.fail(
                    instance, 'The array has too few elements matching the '
                    f'"contains" subschema (minimum {self.json})')
Exemplo n.º 16
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        matched_names = set()
        err_names = []
        for name, item in instance.items():
            for regex, subschema in self.json.items():
                if re.search(regex, name) is not None:
                    with scope(regex) as subscope:
                        subschema.evaluate(item, subscope)
                        if subscope.valid:
                            matched_names |= {name}
                        else:
                            err_names += [name]

        if err_names:
            scope.fail(instance, f"Properties {err_names} are invalid")
        else:
            scope.annotate(instance, self.key, list(matched_names))
Exemplo n.º 17
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        valid_indices = []
        err_indices = []
        for index, subschema in enumerate(self.json):
            with scope(str(index)) as subscope:
                subschema.evaluate(instance, subscope)
                if subscope.valid:
                    valid_indices += [index]
                else:
                    err_indices += [index]

        if len(valid_indices) != 1:
            scope.fail(
                instance,
                'The instance must be valid against exactly one subschema; '
                f'it is valid against {valid_indices} and invalid against {err_indices}'
            )
Exemplo n.º 18
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        annotation = []
        err_names = []
        for name, subschema in self.json.items():
            if name in instance:
                with scope(name) as subscope:
                    subschema.evaluate(instance, subscope)
                    if subscope.valid:
                        annotation += [name]
                    else:
                        err_names += [name]

        if err_names:
            scope.fail(
                instance, f'Properties {err_names} are invalid against '
                f'the corresponding "dependentSchemas" subschemas')
        else:
            scope.annotate(instance, self.key, annotation)
Exemplo n.º 19
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if len(instance) < self.json:
         scope.fail(
             instance,
             f"The object has too few properties (minimum {self.json})")
Exemplo n.º 20
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if instance >= self.json:
         scope.fail(instance, f"The value must be less than {self.json}")
Exemplo n.º 21
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if len(instance) > self.json:
         scope.fail(
             instance,
             f"The text is too long (maximum {self.json} characters)")
Exemplo n.º 22
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if len(instance) < self.json:
         scope.fail(
             instance,
             f"The text is too short (minimum {self.json} characters)")
Exemplo n.º 23
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if instance > self.json:
         scope.fail(instance,
                    f"The value may not be greater than {self.json}")
Exemplo n.º 24
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if instance != self.json:
         scope.fail(instance, f"The value must be equal to {self.json}")
Exemplo n.º 25
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if instance not in self.json:
         scope.fail(instance, f"The value must be one of {self.json}")
Exemplo n.º 26
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if self.regex.search(instance.value) is None:
         scope.fail(
             instance,
             f"The text must match the regular expression {self.json}")
Exemplo n.º 27
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     if len(instance) < self.json:
         scope.fail(
             instance,
             f"The array has too few elements (minimum {self.json})")
Exemplo n.º 28
0
 def evaluate(self, instance: JSON, scope: Scope) -> None:
     missing = [name for name in self.json if name.value not in instance]
     if missing:
         scope.fail(instance,
                    f"The object is missing required properties {missing}")