Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $ protobuf-jsonschema --help
-h, --help output usage information
-V, --version output the version number
-f, --format [format] output format: json or yaml [json]
-a, --allow true if validator should allow additional properties, false if not. This will apply to all objects in the schema.
```

In node, `protobuf-jsonschema` exports a single function that returns an object
Expand All @@ -30,8 +31,8 @@ with the JSON Schema model.
```javascript
var compile = require('protobuf-jsonschema');

var all = compile('models.proto');
var single = compile('models.proto', 'MyModel');
var all = compile({ allow_additional_props: true }, 'models.proto');
var single = compile({ allow_additional_props: true }, 'models.proto', 'MyModel');
```

## License
Expand Down
4 changes: 3 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ commander
.version(require('./package.json').version)
.arguments('<file> [model]')
.option('-f, --format [format]', 'output format: json or yaml [json]', 'json')
.option('-a, --allow [allow]', 'allow additional properties: true or false', 'true')
.action(function(file, model) {
var format = commander.format || 'json';
var result = compile(file, model);
var opts = { allow_additional_props : !(commander.allow == 'false') };
var result = compile(opts, file, model);

if (format === 'json')
process.stdout.write(JSON.stringify(result, false, 2) + '\n');
Expand Down
32 changes: 17 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ Compiler.prototype.visit = function(schema, prefix) {
* compiles just that type and its dependencies. Otherwise,
* compiles all types in the file.
*/
Compiler.prototype.compile = function(type) {
Compiler.prototype.compile = function(opts, type) {
this.root = {
definitions: {},
used: {}
};

if (type) {
this.resolve(type, '');
this.resolve(opts, type, '');
} else {
this.schema.messages.forEach(function(message) {
this.resolve(message.id, '');
this.resolve(opts, message.id, '');
}, this);

this.schema.enums.forEach(function(e) {
this.resolve(e.id, '');
this.resolve(opts, e.id, '');
}, this);
}

Expand All @@ -76,7 +76,8 @@ Compiler.prototype.compile = function(type) {
* Resolves a type name at the given path in the schema tree.
* Returns a compiled JSON schema.
*/
Compiler.prototype.resolve = function(type, from, base, key) {
Compiler.prototype.resolve = function(opts, type, from, base, key) {

if (primitive[type])
return primitive[type];

Expand All @@ -98,7 +99,7 @@ Compiler.prototype.resolve = function(type, from, base, key) {
// Compile the message or enum
var res;
if (this.messages[id])
res = this.compileMessage(this.messages[id]);
res = this.compileMessage(opts, this.messages[id]);

if (this.enums[id])
res = this.compileEnum(this.enums[id]);
Expand All @@ -124,8 +125,8 @@ Compiler.prototype.resolve = function(type, from, base, key) {
/**
* Compiles and assigns a type
*/
Compiler.prototype.build = function(type, from, base, key) {
var res = this.resolve(type, from, base, key);
Compiler.prototype.build = function(opts, type, from, base, key) {
var res = this.resolve(opts, type, from, base, key);
if (base)
base[key] = res;
};
Expand All @@ -146,11 +147,12 @@ Compiler.prototype.compileEnum = function(enumType, root) {
/**
* Compiles a protobuf message to JSON schema
*/
Compiler.prototype.compileMessage = function(message, root) {
Compiler.prototype.compileMessage = function(opts, message, root) {
var res = {
title: message.name,
type: 'object',
properties: {},
additionalProperties: opts.allow_additional_props,
required: []
};

Expand All @@ -164,17 +166,17 @@ Compiler.prototype.compileMessage = function(message, root) {
additionalProperties: null
};

this.build(field.map.to, message.id, f, 'additionalProperties');
this.build(opts, field.map.to, message.id, f, 'additionalProperties');
} else {
if (field.repeated) {
var f = res.properties[field.name] = {
type: 'array',
items: null
};
this.build(field.type, message.id, f, 'items');

this.build(opts, field.type, message.id, f, 'items');
} else {
this.build(field.type, message.id, res.properties, field.name);
this.build(opts, field.type, message.id, res.properties, field.name);
}
}

Expand All @@ -188,7 +190,7 @@ Compiler.prototype.compileMessage = function(message, root) {
return res;
};

module.exports = function(filename, model) {
module.exports = function(opts, filename, model) {
var compiler = new Compiler(filename);
return compiler.compile(model);
return compiler.compile(opts, model);
};