0.3.0 • Published 7 months ago

@melt-inc/buf-json-schema v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

Protobuf to JSON Schema

Convert Protobuf descriptors to JSON Schema. Powered by Buf.

API

!WARNING API is currently in Alpha and is subject to breaking changes.

messageToJSONSchema

messageToJSONSchema(proto: DescriptorProto, descriptors?: DescriptorSet): any

Creates a JSON Schema from the given DescriptorProto. If a DescriptorSet is also given, it will be used to resolve any references to other types.

fileToJSONSchema

fileDescriptorToJSONSchema(proto: FileDescriptorProto, descriptors?: DescriptorSet): any

Creates a JSON Schema from the given FileDescriptorProto. If a DescriptorSet is also given, it will be used to resolve any references to other types. Every message type in the file descriptor will exist in the JSON Schema definitions section, and a oneOf constraint will ensure only one message matches validation. See test for some examples.

Example usage

Getting a descriptor

You can load a descriptor set from binary or JSON.

import { FileDescriptorSet, createDescriptorSet } from "@bufbuild/protobuf";

// load descriptors from file (or anywhere)
const buffer = fs.readFileSync("test/example-descriptors.bin");
const fileDescriptors = FileDescriptorSet.fromBinary(buffer)

// create descriptor set
let descriptorSet = createDescriptorSet(fileDescriptors);

// get message descriptor
let messageDescriptor = descriptorSet.messages.get("mypackage.MyProtoMessage")

Then import proto-json-schema and use the relevant top-level function:

import { messageDescriptor } from "@melt-inc/proto-json-schema"
let schema = messageToJSONSchema(messageDescriptor.proto, descriptorSet)

TypeScript Extension Methods

For convenience, the .toJSONSchema() module augmentation is available on Buf descriptor classes.

import "@melt-inc/proto-json-schema/extensions"

// use extension method              ↓↓↓↓↓↓↓↓↓↓↓↓
let schema = messageDescriptor.proto.toJSONSchema(descriptorSet)

Progress

Currently only a subset of valid protobuf definitions are supported. Track the progress of development below:

  • Resolve references from descriptor set
  • Message
  • File
    • Tests
  • Enum
  • Field
  • Well Known
    • Any (message)
    • BoolValue (message)
    • BytesValue (message)
    • DoubleValue (message)
    • Duration (message)
    • Empty (message)
    • Enum (message)
    • EnumValue (message)
    • FieldMask (message)
    • FloatValue (message)
    • Int32Value (message)
    • Int64Value (message)
    • ListValue (message)
    • NullValue (enum)
    • StringValue (message)
    • Struct (message)
    • Timestamp (message)
    • UInt32Value (message)
    • UInt64Value (message)
    • Value (message)
  • Future features
0.3.0

7 months ago

0.2.4

7 months ago

0.2.3

7 months ago

0.2.2

7 months ago

0.2.1

7 months ago

0.2.0

7 months ago

0.1.0

7 months ago