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
- Understand how Pub/Sub schemas work.
- Create a schema.
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 to projects, folders, and organizations.
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
Console
In the Google Cloud console, go to the Pub/Sub schemas page.
The list of schemas is displayed.
Click the Schema ID of an existing schema.
The Schema details page for the schema opens.
In the Revisions section, click the revision for which you want to validate a message.
In the Details section, click Test message.
In the Test message window, select a type of Message encoding.
In the Message body, enter a test message.
Click Test.
gcloud
-
In the Google Cloud console, 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.
- 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 | \ --definition-file=DEFINITION_FILE))
Replace the following:
MESSAGE: message to validate against the schema
MESSAGE_ENCODING: encoding of the message. Specify one of the following values:
binary
orjson
.SCHEMA_NAME: name or full path of an existing schema.
TYPE: type of inline schema. Specify one of the following values:
avro
orprotocol-buffer
.DEFINITION: inline schema definition.
DEFINITION_FILE: file containing the schema definition.