Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Neste documento, mostramos como validar esquemas para tópicos do Pub/Sub.
Você pode validar se as mensagens aderem a um determinado esquema,
antes ou depois de criar um recurso de esquema. Essa etapa garante que as mensagens que você pretende enviar por um tópico associado a um esquema realmente correspondam antes de aplicar o esquema.
Para receber as permissões necessárias para
validar e gerenciar mensagens de esquema,
peça ao administrador para conceder a você o
papel do IAM de Editor do Pub/Sub (roles/pubsub.editor)
no seu projeto.
Para mais informações sobre a concessão de papéis, consulte Gerenciar o acesso a projetos, pastas e organizações.
Esse papel predefinido contém
as permissões necessárias para validar e gerenciar mensagens de esquema. Para conferir as permissões exatas
necessárias, expanda a seção Permissões necessárias:
Permissões necessárias
As seguintes permissões são necessárias para validar e gerenciar mensagens de esquema:
Criar esquema:
pubsub.schemas.create
Anexe o esquema ao tópico:
pubsub.schemas.attach
Confirmar uma revisão de esquema:
pubsub.schemas.commit
Excluir um esquema ou uma revisão de esquema:
pubsub.schemas.delete
Receber um esquema ou revisões de esquema:
pubsub.schemas.get
Listar esquemas:
pubsub.schemas.list
Listar revisões de esquema:
pubsub.schemas.listRevisions
Reverter um esquema:
pubsub.schemas.rollback
Validar uma mensagem:
pubsub.schemas.validate
Acessar a política do IAM de um esquema:
pubsub.schemas.getIamPolicy
Configure a política do IAM para um esquema:
pubsub.schemas.setIamPolicy
É possível conceder papéis e permissões a principais como usuários, grupos, domínios ou contas de serviço. É possível criar um esquema em um projeto e anexá-lo a um tópico localizado em outro projeto.
Verifique se você tem as permissões necessárias para
cada projeto.
Validar uma mensagem para um esquema
Console
No console Google Cloud , acesse a página Esquemas do Pub/Sub.
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.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-09-03 UTC."],[],[],null,["This document shows you how to validate schemas for Pub/Sub topics.\n\nYou can validate that messages adhere to a certain schema,\nbefore or after you create a schema resource. This step ensures that\nthe messages you intend to send through a topic associated with a\nschema actually match before applying the schema.\n\nBefore you begin\n\n- Understand how [Pub/Sub schemas work](/pubsub/docs/schemas).\n- [Create a schema](/pubsub/docs/create-schemas).\n\nRequired roles and permissions\n\n\nTo get the permissions that\nyou need to validate schema messages and manage them,\n\nask your administrator to grant you the\n\n\n[Pub/Sub Editor](/iam/docs/roles-permissions/pubsub#pubsub.editor) (`roles/pubsub.editor`)\nIAM role on your project.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nThis predefined role contains\n\nthe permissions required to validate schema messages and manage them. To see the exact permissions that are\nrequired, expand the **Required permissions** section:\n\n\nRequired permissions\n\nThe following permissions are required to validate schema messages and manage them:\n\n- Create schema: ` ``pubsub.schemas.create`\n- Attach schema to topic: ` ``pubsub.schemas.attach`\n- Commit a schema revision: ` ``pubsub.schemas.commit`\n- Delete a schema or a schema revision: ` ``pubsub.schemas.delete`\n- Get a schema or schema revisions: ` ``pubsub.schemas.get`\n- List schemas: ` ``pubsub.schemas.list`\n- List schema revisions: ` ``pubsub.schemas.listRevisions`\n- Rollback a schema: ` ``pubsub.schemas.rollback`\n- Validate a message: ` ``pubsub.schemas.validate`\n- Get the IAM policy for a schema: ` ``pubsub.schemas.getIamPolicy`\n- Configure the [IAM policy](/iam/docs/reference/rest/v1/Policy) for a schema: ` ``pubsub.schemas.setIamPolicy`\n\n\nYou might also be able to get\nthese permissions\nwith [custom roles](/iam/docs/creating-custom-roles) or\nother [predefined roles](/iam/docs/roles-overview#predefined).\n\nYou can grant roles and permissions to principals such as users, groups,\ndomains, or service accounts. You can create a schema in one project and\nattach it to a topic located in a different project.\nEnsure that you have the required permissions for\neach project.\n\nValidate a message for a schema \n\nConsole\n\n1. In the Google Cloud console, go to the **Pub/Sub schemas** page.\n\n [Go to Schemas](https://console.cloud.google.com/cloudpubsub/schema)\n\n The list of schemas is displayed.\n2. Click the **Schema ID** of an existing schema.\n\n The **Schema details** page for the schema opens.\n3. In the **Revisions** section, click the revision for which you want to\n validate a message.\n\n4. In the **Details** section, click **Test message**.\n\n5. In the **Test message** window, select a type of **Message encoding**.\n\n6. In the **Message** body, enter a test message.\n\n7. Click **Test**.\n\ngcloud\n\n\n1. In the Google Cloud console, activate Cloud Shell.\n\n [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n\n\n At the bottom of the Google Cloud console, a\n [Cloud Shell](/shell/docs/how-cloud-shell-works)\n session starts and displays a command-line prompt. Cloud Shell is a shell environment\n with the Google Cloud CLI\n already installed and with values already set for\n your current project. It can take a few seconds for the session to initialize.\n2. To validate a message against a schema use the [gcloud pubsub schemas validate-message](/sdk/gcloud/reference/pubsub/schemas/validate-message) command.\n\n \u003cbr /\u003e\n\n ```\n gcloud pubsub schemas validate-message --message=MESSAGE \\\n --message-encoding=MESSAGE_ENCODING \\\n (--schema-name=SCHEMA_NAME | \\\n --type=TYPE \\\n (--definition=DEFINITION | \\\n --definition-file=DEFINITION_FILE))\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eMESSAGE\u003c/var\u003e: message to validate against the schema\n\n - \u003cvar translate=\"no\"\u003eMESSAGE_ENCODING\u003c/var\u003e: encoding of the message. Specify\n one of the following values: `binary` or `json`.\n\n - \u003cvar translate=\"no\"\u003eSCHEMA_NAME\u003c/var\u003e: name or full path of an existing schema.\n\n - \u003cvar translate=\"no\"\u003eTYPE\u003c/var\u003e: type of inline schema. Specify one of the\n following values: `avro`or `protocol-buffer`.\n\n - \u003cvar translate=\"no\"\u003eDEFINITION\u003c/var\u003e: inline schema definition.\n\n - \u003cvar translate=\"no\"\u003eDEFINITION_FILE\u003c/var\u003e: file containing the schema definition.\n\n\u003cbr /\u003e\n\nWhat's next\n\n- [Validate a schema definition when you create a topic](/pubsub/docs/create-schemas#create-schema)"]]