Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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('models.proto', true);
var single = compile('models.proto', 'MyModel', true);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the API compile(filename, model, opts) where opts is an object {additionalProperties: true}.

```

## 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 allow_additional_props = !(commander.allow == 'false');
var result = compile(file, null,allow_additional_props);

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(type, allow_additional_props) {
this.root = {
definitions: {},
used: {}
};

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

this.schema.enums.forEach(function(e) {
this.resolve(e.id, '');
this.resolve(e.id, '', allow_additional_props);
}, 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(type, from, allow_additional_props, 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(this.messages[id],root,allow_additional_props);

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(type, from, allow_additional_props, base, key) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make opts the last argument.

var res = this.resolve(type, from, allow_additional_props, 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(message, root, allow_additional_props) {
var res = {
title: message.name,
type: 'object',
properties: {},
additionalProperties: 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(field.map.to, message.id, allow_additional_props, 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(field.type, message.id, allow_additional_props, f, 'items');
} else {
this.build(field.type, message.id, res.properties, field.name);
this.build(field.type, message.id, allow_additional_props, 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(filename, model,allow_additional_props) {
var compiler = new Compiler(filename);
return compiler.compile(model);
return compiler.compile(model,allow_additional_props);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind that model is optional, so the third argument could become the second. I'd use something like

if (typeof model === 'object') {
  opts = model;
  model = null;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a problem that can also happen in another method.. I think it's resolve. How about we make the opts solution you described above the 1st argument in all these methods. That way you have an easy way to support other flags later on. Opinion ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean make model part of the opts? We could, but that would be backward incompatible...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant make this one compile(opts, model) and make all the other function calls also use opts as the first parameter.

};