import json
from frictionless import Resource


def is_valid(resource, file):
    """Check if the HTTP response is a valid JSON document."""

    with open(file.name, "rb") as f:
        try:
            json_data = json.load(f)

            if (
                "path" in json_data
                and "schema" in json_data
                and (
                    json_data.get("profile") == "tabular-data-resource"
                    or json_data.get("type") == "table"
                )
            ):
                # There is a good chance that this is a Frictionless Data Resource.
                res = Resource(json_data)
                resource["schema_valid"] = res.validate().valid
                return resource["schema_valid"]

            return True
        except Exception as e:
            resource["error"] = str(e)
            return False