Esempio 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})')
Esempio 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)
Esempio 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()
Esempio 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}")
Esempio 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()
Esempio 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")
Esempio 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")
Esempio 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}")
Esempio 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}')
Esempio 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')
Esempio 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}")
Esempio 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')
Esempio 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)
Esempio 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)
Esempio 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})')
Esempio 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))
Esempio 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}'
            )
Esempio 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)
Esempio 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})")
Esempio 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}")
Esempio 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)")
Esempio 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)")
Esempio 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}")
Esempio 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}")
Esempio 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}")
Esempio 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}")
Esempio 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})")
Esempio 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}")