• This repository has been archived on 21/Jun/2024
  • Stars
    star
    489
  • Rank 89,990 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Protobuf to JSON-Schema compiler

Protobuf to JSON-Schema compiler

This takes protobuf definitions and converts them into JSONSchemas, which can be used to dynamically validate JSON messages.

Useful for people who define their data using ProtoBuf, but use JSON for the "wire" format.

"Heavily influenced" by Google's protobuf-to-BigQuery-schema compiler.

Generated Schemas

  • One JSONSchema file is generated for each root-level proto message and ENUM. These are intended to be stand alone self-contained schemas which can be used to validate a payload derived from their source proto message
  • Nested message schemas become referenced "definitions". This means that you know the name of the proto message they came from, and their schema is not duplicated (within the context of one JSONSchema file at least)

Logic

  • For each proto file provided
    • Generates schema for each ENUM
      • JSONSchema filename deried from ENUM name
    • Generates schema for each Message
      • Builds a list of every nested message and converts them to JSONSchema
      • Recursively converts attributes and nested messages within the root message
        • Optionally makes all fields required
        • Optionally allows NULL values
        • Optionally allows additional properties
        • Optionally marks all fields required
        • Specially marked fields are labelled required (options.proto)
        • Specially marked fields are omitted (options.proto)
        • Special handling for "OneOf"
        • Special handling for arrays
        • Special handling for maps
      • Injects references to nested messages
      • JSONSchema filename derived from Message name
    • Bundles these into a protoc generator response

Installation

Note: This tool requires Go 1.11+ to be installed.

Install this plugin using Go:

go install github.com/chrusty/protoc-gen-jsonschema/cmd/protoc-gen-jsonschema@latest

Usage

Note: This plugin requires the protoc CLI to be installed.

protoc-gen-jsonschema is designed to run like any other proto generator. The following examples show how to use options flags to enable different generator behaviours (more examples in the Makefile too).

protoc \ # The protobuf compiler
--jsonschema_out=. \ # jsonschema out directory
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto # proto input directories and folders

Configuration Parameters

The following configuration parameters are supported. They should be added to the protoc command and can be combined as a comma-delimited string. Some examples are included in the following Examples section.

Options can also be provided in this format (which is easier on the eye):

protoc \
  --plugin=${HOME}/go/bin/protoc-gen-jsonschema \
  --jsonschema_opt=enforce_oneof
  --jsonschema_opt=file_extension=schema.json \
  --jsonschema_opt=disallow_additional_properties \
  --jsonschema_out=schemas \
  --proto_path=proto
CONFIG DESCRIPTION
all_fields_required Require all fields in schema
allow_null_values Allow null values in schema
debug Enable debug logging
disallow_additional_properties Disallow additional properties in schema
disallow_bigints_as_strings Disallow big integers as strings
enforce_oneof Interpret Proto "oneOf" clauses
enums_as_strings_only Only include strings in the allowed values for enums
file_extension Specify a custom file extension for generated schemas
json_fieldnames Use JSON field names only
prefix_schema_files_with_package Prefix the output filename with package
proto_and_json_fieldnames Use proto and JSON field names

Custom Proto Options

If you don't want to use the configuration parameters (admittedly quite a nasty cli syntax) then some of the generator behaviour can be controlled using custom proto options. These are defined in options.proto, and your protoc command will need to include this file. See the sample protos and generator commands in the Makefile.

Enum Options

These apply to specifically marked enums, giving you more finely-grained control than with the CLI flags.

Field Options

These apply to specifically marked fields, giving you more finely-grained control than with the CLI flags.

  • ignore: Ignore (omit) a specific field
  • required: Mark a specific field as being REQUIRED

File Options

These options apply to an entire proto file.

  • ignore: Ignore (skip) a specific file
  • extension: Specify a custom file-extension for the generated schema for this file

Message Options

These options apply to a specific proto message.

Validation Options

We are also beginning to support validation options from protoc-gen-validate.

At the moment the following are supported (but export more in the future):

  • Arrays
    • MaxItems
    • MinItems
  • Strings
    • MaxLength
    • MinLength
    • Pattern

Examples

Require all fields

Because proto3 doesn't accommodate this.

protoc \
--jsonschema_out=all_fields_required:. \
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto

Allow NULL values

By default, JSONSchemas will reject NULL values unless we explicitly allow them

protoc \
--jsonschema_out=allow_null_values:. \
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto

Enable debug logging

protoc \
--jsonschema_out=debug:. \
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto

Disallow additional properties

JSONSchemas won't validate JSON containing extra parameters

protoc \
--jsonschema_out=disallow_additional_properties:. \
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto

Disallow permissive validation of big-integers as strings

(eg scientific notation)

protoc \
--jsonschema_out=disallow_bigints_as_strings:. \
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto

Prefix generated schema files with their package name (as a directory)

protoc \
--jsonschema_out=prefix_schema_files_with_package:. \
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto

Target specific messages within a proto file

# Generates MessageKind10.jsonschema and MessageKind11.jsonschema
# Use this to generate json schema from proto files with multiple messages
# Separate schema names with '+'
protoc \
--jsonschema_out=messages=[MessageKind10+MessageKind11]:. \
--proto_path=testdata/proto testdata/proto/TwelveMessages.proto

Generate fields with JSON names

protoc \
--jsonschema_out=json_fieldnames:. \
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto

Custom schema file extension

The default file extension is json. You can override that with the "file_extension" parameter.

protoc \
--jsonschema_out=file_extension=jsonschema:. \
--proto_path=internal/converter/testdata/proto internal/converter/testdata/proto/ArrayOfPrimitives.proto

Sample protos (for testing)

Links

More Repositories

1

ctop

CTOP ("top" for Cassandra) - See what's going on in real-time
Go
141
star
2

nsq-to-s3

Stream an NSQ channel to S3
Go
33
star
3

prometheus_webhook_snmptrapper

Generates SNMP traps from Prometheus AlertManager WebHooks
Go
30
star
4

telegraf-cassandra

Config for monitoring Cassandra using Telegraf's "Jolokia" plugin
Python
12
star
5

devops-manifesto

Opinionated DevOps Philosophies
Shell
10
star
6

terraform-multipart-userdata

Cruft for Terraform
Go
9
star
7

openapi2jsonschema

Produce JSONSchemas from OpenAPI2 (Swagger) & OpenAPI3 definitions
Go
7
star
8

go-circle-to-polygon

Go library to convert a circle to a polygon
Go
6
star
9

go-mono

Toolkit for Golang monorepos
Go
6
star
10

lambda-dns

AWS Lambda function to maintain Route53 DNS records from autoscaling events
JavaScript
5
star
11

google-pubsub-logging

DataFlows to store logs centralised with Google PUB/SUB
Java
4
star
12

dns-from-gce

Populate a Google Cloud-DNS zone from the list of VM Instances in your GCE Project
Go
4
star
13

puppet-kapacitor

Puppet module for Kapacitor
HTML
3
star
14

go-tableprinter

Print a formatted table from GoLang interfaces
Go
3
star
15

kafka-cli

A powerful and easy-to-use CLI tool for Kafka
Go
1
star
16

tail-pubsub

Tail a Google PUB/SUB topic
Go
1
star
17

micro-pingpong

Ping and Pong services using Go-Micro
Go
1
star
18

dns-from-aws

Populate a DNS zone from the list of EC2 instances in your AWS account
Go
1
star
19

api-proxy

Routes requests to your API services (by the URL path)
Go
1
star
20

loosejson

Loose JSON unmarshalling (for when your JSON arrives in the wrong type but you still want the data)
Go
1
star