Skip to content

jeremy-ww/jsonschema-protobuf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonschema-protobuf2

Converts JSON Schema to Protocol Buffers.

Install

npm install -g jsonschema-protobuf

Example

$ jsonschema-protobuf test.jsonschema
syntax = "proto3";

message Person {
  message AlterEgos {
    message City {
      optional string name = 1;
    }

    repeated City city = 1;
    optional string state = 2;
  }

  message Location {
    optional string city = 1;
    optional string state = 2;
  }

  required string name = 1;
  required int32 age = 2;
  required int32 income = 3;
  optional string universe = 4;
  optional bool living = 5;
  repeated AlterEgos alterEgos = 6;
  optional Location location = 7;
}

test.jsonschema

{
  "type": "object",
  "name": "person",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer", "min": 0, "max": 120 },
    "income": { "type": "number", "min": 0 },
    "universe": { "type": "string", "enum": [ "Marvel", "DC" ] },
    "living": { "type": "boolean", "default": true },
    "alterEgos": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "city": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "name": { "type": "string" }
              }
            }
          },
          "state": { "type": "string", "regex": "/[A-Z]{2}/" }
        }
      }
    },
    "location": {
      "type": "object",
      "properties": {
        "city": { "type": "string" },
        "state": { "type": "string", "regex": "/[A-Z]{2}/" }
      }
    }
  },
  "required": [
    "name",
    "age",
    "income"
  ]
}

JS usage

var convert = require('jsonschema-protobuf')
var jsonschema = fs.readFileSync("test.jsonschema").toString()
var protobuf = convert(jsonschema)
console.log(protobuf)

TODO

  • Enum

About

Convert JSON Schema to Protocol Buffers

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%