code: def _validate(
      *,
      cls_instance: object,
      method: Callable,
      data: Mapping[str, Any],
      spec: Spec,
      filter: bool = False,
      tag: str = "input",
  ) -> NestedDict:
      """Validate the data against the spec.
  
      Args:
          cls_instance: The class instance that the method belongs to.
          method: The method to apply the spec checking to.
          data: The data to validate.
          spec: The spec to validate against.
          filter: If True, the data will be filtered to only include the keys that are
              specified in the spec.
          tag: The tag of the spec to check. Either "input" or "output". This is used
              internally to defined an internal cache storage attribute based on the tag.
  
      Returns:
          The data, filtered if filter is True.
      """
      cache_miss = _should_validate(cls_instance, method, tag=tag)
  
      if isinstance(spec, SpecDict):
          if not isinstance(data, abc.Mapping):
              raise ValueError(
                  f"{tag} must be a Mapping, got {type(data).__name__}"
              )
          if cache_miss or filter:
              data = NestedDict(data)
  
      if cache_miss:
          try:
              spec.validate(data)
          except ValueError as e:
              raise SpecCheckingError(
                  f"{tag} spec validation failed on "
                  f"{cls_instance.__class__.__name__}.{method.__name__}, {e}."
              )
  
      return data
  
file_path: ray/rllib/core/models/specs/checker.py
imports: import functools
  import logging
  from collections import abc
  from typing import Union, Mapping, Any, Callable
  from ray.rllib.core.models.specs.specs_base import Spec, TypeSpec
  from ray.rllib.core.models.specs.specs_dict import SpecDict
  from ray.rllib.core.models.specs.typing import SpecType
  from ray.rllib.utils.nested_dict import NestedDict
  from ray.util.annotations import DeveloperAPI
label_desc: Use image node_id 3 for calling a global function with example usage: _validate() and returns: data