code: def validate_template_context(
      engine: Optional[str], context: dict[str, Any]
  ) -> dict[str, Any]:
      if engine and engine in context:
          # validate engine context separately to allow for engine-specific methods
          engine_context = validate_context_types(context.pop(engine))
          valid_context = validate_context_types(context)
          valid_context[engine] = engine_context
          return valid_context
  
      return validate_context_types(context)
  
imports: import json
  import re
  from datetime import datetime
  from functools import lru_cache, partial
  from typing import Any, Callable, cast, Optional, TYPE_CHECKING, TypedDict, Union
  import dateutil
  from flask import current_app, g, has_request_context, request
  from flask_babel import gettext
  from jinja2 import DebugUndefined
  from jinja2.sandbox import SandboxedEnvironment
  from sqlalchemy.engine.interfaces import Dialect
  from sqlalchemy.sql.expression import bindparam
  from sqlalchemy.types import String
  from superset.commands.dataset.exceptions import DatasetNotFoundError
  from superset.constants import LRU_CACHE_MAX_SIZE
  from superset.exceptions import SupersetTemplateException
  from superset.extensions import feature_flag_manager
  from superset.utils.core import convert_legacy_filters_into_adhoc, get_user_id, merge_extra_filters
label_desc: Use image node_id 4 for calling a global function with example usage: validate_template_context(engine, context) and returns: validate_context_types, valid_context