Validate a message for a schema

This document shows you how to validate schemas for Pub/Sub topics.

You can validate that messages adhere to a certain schema, before or after you create a schema resource. This step ensures that the messages you intend to send through a topic associated with a schema actually match before applying the schema.

Before you begin

Required roles and permissions

To get the permissions that you need to validate schema messages and manage them, ask your administrator to grant you the Pub/Sub Editor (roles/pubsub.editor) IAM role on your project. For more information about granting roles, see Manage access.

This predefined role contains the permissions required to validate schema messages and manage them. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to validate schema messages and manage them:

  • Create schema: pubsub.schemas.create
  • Attach schema to topic: pubsub.schemas.attach
  • Commit a schema revision: pubsub.schemas.commit
  • Delete a schema or a schema revision: pubsub.schemas.delete
  • Get a schema or schema revisions: pubsub.schemas.get
  • List schemas: pubsub.schemas.list
  • List schema revisions: pubsub.schemas.listRevisions
  • Rollback a schema: pubsub.schemas.rollback
  • Validate a message: pubsub.schemas.validate
  • Get the IAM policy for a schema: pubsub.schemas.getIamPolicy
  • Configure the IAM policy for a schema: pubsub.schemas.setIamPolicy

You might also be able to get these permissions with custom roles or other predefined roles.

You can grant roles and permissions to principals such as users, groups, domains, or service accounts. You can create a schema in one project and attach it to a topic located in a different project. Ensure that you have the required permissions for each project.

Validate a message for a schema


  1. In the Google Cloud console, go to the Pub/Sub schemas page.

    Go to Schemas

    The list of schemas is displayed.

  2. Click the Schema ID of an existing schema.

    The Schema details page for the schema opens.

  3. In the Revisions section, click the revision for which you want to validate a message.

  4. In the Details section, click Test message.

  5. In the Test message window, select a type of Message encoding.

  6. In the Message body, enter a test message.

  7. Click Test.


  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. To validate a message against a schema use the gcloud pubsub schemas validate-message command.

    gcloud pubsub schemas validate-message --message=MESSAGE \
        --message-encoding=MESSAGE_ENCODING \
        (--schema-name=SCHEMA_NAME | \
        --type=TYPE \
        (--definition=DEFINITION | \

    Replace the following:

    • MESSAGE: message to validate against the schema

    • MESSAGE_ENCODING: encoding of the message. Specify one of the following values: binary or json.

    • SCHEMA_NAME: name or full path of an existing schema.

    • TYPE: type of inline schema. Specify one of the following values: avroor protocol-buffer.

    • DEFINITION: inline schema definition.

    • DEFINITION_FILE: file containing the schema definition.

What's next