예제 #1
0
    def process_object(self, new, old=None):
        """Validate records against collection or bucket schema, if any."""
        new = super().process_object(new, old)

        # Is schema validation enabled?
        settings = self.request.registry.settings
        schema_validation = "experimental_collection_schema_validation"
        if not asbool(settings.get(schema_validation)):
            return new

        # Remove internal and auto-assigned fields from schemas and record.
        ignored_fields = (
            self.model.modified_field,
            self.schema_field,
            self.model.permissions_field,
        )

        # The schema defined on the collection will be validated first.
        if "schema" in self._collection:
            schema = self._collection["schema"]
            try:
                validate_schema(new,
                                schema,
                                ignore_fields=ignored_fields,
                                id_field=self.model.id_field)
            except ValidationError as e:
                raise_invalid(self.request,
                              name=e.field,
                              description=e.message)
            except RefResolutionError as e:
                raise_invalid(self.request, name="schema", description=str(e))

            # Assign the schema version to the record.
            schema_timestamp = self._collection[self.model.modified_field]
            new[self.schema_field] = schema_timestamp

        # Validate also from the record:schema field defined on the bucket.
        validate_from_bucket_schema_or_400(
            new,
            resource_name="record",
            request=self.request,
            ignore_fields=ignored_fields,
            id_field=self.model.id_field,
        )

        return new
예제 #2
0
    def process_record(self, new, old=None):
        """Validate records against collection or bucket schema, if any."""
        new = super().process_record(new, old)

        # Is schema validation enabled?
        settings = self.request.registry.settings
        schema_validation = "experimental_collection_schema_validation"
        if not asbool(settings.get(schema_validation)):
            return new

        # Remove internal and auto-assigned fields from schemas and record.
        internal_fields = (
            self.model.id_field,
            self.model.modified_field,
            self.schema_field,
            self.model.permissions_field,
        )

        # The schema defined on the collection will be validated first.
        if "schema" in self._collection:
            schema = self._collection["schema"]

            try:
                validate_schema(new, schema, ignore_fields=internal_fields)
            except ValidationError as e:
                raise_invalid(self.request, name=e.field, description=e.message)
            except RefResolutionError as e:
                raise_invalid(self.request, name="schema", description=str(e))

            # Assign the schema version to the record.
            schema_timestamp = self._collection[self.model.modified_field]
            new[self.schema_field] = schema_timestamp

        # Validate also from the record:schema field defined on the bucket.
        validate_from_bucket_schema_or_400(
            new, resource_name="record", request=self.request, ignore_fields=internal_fields
        )

        return new
예제 #3
0
 def test_validate_schema(self):
     validate_schema(VALID_RECORD, SCHEMA, id_field="id")