Exemplo n.º 1
0
    def _validate_event(self, raw_event: str, event_type: str) -> None:
        """
        Checks if an event is registered and validates it based on
        a registered schema.
        Args:
            raw_event: The event to be validated, as a JSON-encoded string
            event_type: The type of an event, which corresponds
            to a generated model
        Returns:
            Does not return, but throws exceptions if validation fails.
        """
        event = json.loads(raw_event)

        # Event not in registry
        if event_type not in self.event_registry:
            logging.debug(
                'Event type %s not among registered event types (%s)',
                event_type, self.event_registry)
            raise KeyError(
                'Event type {} not registered, '
                'please add it to the eventd config'.format(event_type))

        # swagger_spec exists because we load it up for every event_type
        # in load_specs_from_registry()
        swagger_spec = self.event_type_to_spec[event_type]

        # Field and type checking
        bravado_spec = Spec.from_dict(swagger_spec,
                                      config={'validate_swagger_spec': False})
        bravado_validate(
            bravado_spec,
            swagger_spec['definitions'][event_type],
            event)
Exemplo n.º 2
0
    def validate_event(self, raw_event: str, event_type: str) -> None:
        """
        Checks if an event is registered and validates it based on
        a registered schema.
        Args:
            raw_event: The event to be validated, as a JSON-encoded string
            event_type: The type of an event, which corresponds
            to a generated model
        Returns:
            Does not return, but throws exceptions if validation fails.
        """
        event = json.loads(raw_event)

        # Event not in registry
        if event_type not in self.event_registry:
            logging.debug(
                'Event type %s not among registered event types (%s)',
                event_type, self.event_registry)
            raise KeyError(
                'Event type {} not registered, '
                'please add it to the EventD config'.format(event_type))
        filename = self.event_registry[event_type][FILENAME]
        bravado_validate(
            self.specs_by_filename[filename][BRAVADO_SPEC],
            self.specs_by_filename[filename][SWAGGER_SPEC][event_type], event)