コード例 #1
0
    def basic(scope: Scope) -> Dict[str, Any]:
        result = {"valid": scope.valid}
        if result["valid"]:
            result["annotations"] = []
            for annotation in scope.collect_annotations():
                result["annotations"] += [{
                    "instanceLocation":
                    str(annotation.instance_path),
                    "keywordLocation":
                    str(annotation.evaluation_path),
                    "absoluteKeywordLocation":
                    str(annotation.absolute_uri),
                    "annotation":
                    annotation.value,
                }]
        else:
            result["errors"] = []
            for error in scope.collect_errors():
                result["errors"] += [{
                    "instanceLocation":
                    str(error.instance_path),
                    "keywordLocation":
                    str(error.evaluation_path),
                    "absoluteKeywordLocation":
                    str(error.absolute_uri),
                    "error":
                    error.message,
                }]

        return result
コード例 #2
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})')
コード例 #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()
コード例 #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}")
コード例 #5
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")
コード例 #6
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")
コード例 #7
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}")
コード例 #8
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}')
コード例 #9
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')
コード例 #10
0
ファイル: legacy.py プロジェクト: aviv-julienjehannet/jschon
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        if (items := scope.sibling("items")) and \
                (items_annotation := items.annotations.get("items")) and \
                type(items_annotation.value) is int:
            annotation = None
            for index, item in enumerate(instance[items_annotation.value +
                                                  1:]):
                annotation = True
                self.json.evaluate(item, scope)

            if scope.valid:
                scope.annotate(instance, "additionalItems", annotation)
コード例 #11
0
ファイル: legacy.py プロジェクト: aviv-julienjehannet/jschon
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        last_evaluated_item = -1
        for items_annotation in scope.parent.collect_annotations(
                instance, "items"):
            if items_annotation.value is True:
                scope.discard()
                return
            if type(items_annotation.value
                    ) is int and items_annotation.value > last_evaluated_item:
                last_evaluated_item = items_annotation.value

        for additional_items_annotation in scope.parent.collect_annotations(
                instance, "additionalItems"):
            if additional_items_annotation.value is True:
                scope.discard()
                return

        for unevaluated_items_annotation in scope.parent.collect_annotations(
                instance, "unevaluatedItems"):
            if unevaluated_items_annotation.value is True:
                scope.discard()
                return

        annotation = None
        for index, item in enumerate(instance[last_evaluated_item + 1:]):
            annotation = True
            self.json.evaluate(item, scope)

        if scope.valid:
            scope.annotate(instance, self.key, annotation)
コード例 #12
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}")
コード例 #13
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')
コード例 #14
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)
コード例 #15
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)
コード例 #16
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})')
コード例 #17
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))
コード例 #18
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}'
            )
コード例 #19
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)
コード例 #20
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        last_evaluated_item = -1
        for prefix_items_annotation in scope.parent.collect_annotations(
                instance, "prefixItems"):
            if prefix_items_annotation.value is True:
                scope.discard()
                return
            if prefix_items_annotation.value > last_evaluated_item:
                last_evaluated_item = prefix_items_annotation.value

        for items_annotation in scope.parent.collect_annotations(
                instance, "items"):
            if items_annotation.value is True:
                scope.discard()
                return

        for unevaluated_items_annotation in scope.parent.collect_annotations(
                instance, "unevaluatedItems"):
            if unevaluated_items_annotation.value is True:
                scope.discard()
                return

        contains_indices = set()
        for contains_annotation in scope.parent.collect_annotations(
                instance, "contains"):
            contains_indices |= set(contains_annotation.value)

        annotation = None
        for index, item in enumerate(
                instance[(start := last_evaluated_item + 1):], start):
            if index not in contains_indices:
                annotation = True
                self.json.evaluate(item, scope)
コード例 #21
0
ファイル: legacy.py プロジェクト: aviv-julienjehannet/jschon
    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)
コード例 #22
0
    def evaluate(self, instance: JSON, scope: Scope) -> None:
        evaluated_names = set()
        for properties_annotation in scope.parent.collect_annotations(
                instance, "properties"):
            evaluated_names |= set(properties_annotation.value)
        for pattern_properties_annotation in scope.parent.collect_annotations(
                instance, "patternProperties"):
            evaluated_names |= set(pattern_properties_annotation.value)
        for additional_properties_annotation in scope.parent.collect_annotations(
                instance, "additionalProperties"):
            evaluated_names |= set(additional_properties_annotation.value)
        for unevaluated_properties_annotation in scope.parent.collect_annotations(
                instance, "unevaluatedProperties"):
            evaluated_names |= set(unevaluated_properties_annotation.value)

        annotation = []
        for name, item in instance.items():
            if name not in evaluated_names:
                if self.json.evaluate(item, scope).valid:
                    annotation += [name]

        if scope.valid:
            scope.annotate(instance, self.key, annotation)
コード例 #23
0
ファイル: format.py プロジェクト: aviv-julienjehannet/jschon
 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()
コード例 #24
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})")
コード例 #25
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}")
コード例 #26
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}")
コード例 #27
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}")
コード例 #28
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}")
コード例 #29
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})")
コード例 #30
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}")