Skip to content

Commit

Permalink
New: Verifiers return an error if multiple fields part of the same on…
Browse files Browse the repository at this point in the history
…eof are set, see #710
  • Loading branch information
dcodeIO committed Mar 20, 2017
1 parent d7493ef commit adb4bb0
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 218 deletions.
17 changes: 13 additions & 4 deletions src/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ function verifier(mtype) {
var gen = util.codegen("m")
("if(typeof m!==\"object\"||m===null)")
("return%j", "object expected");
var oneofs = mtype.oneofsArray,
seenFirstField = {};
if (oneofs.length) gen
("var p={}");

for (var i = 0; i < /* initializes */ mtype.fieldsArray.length; ++i) {
var field = mtype._fieldsArray[i].resolve(),
Expand Down Expand Up @@ -150,11 +154,16 @@ function verifier(mtype) {

// required or present fields
} else {
if (!field.required) {
if (field.resolvedType && !(field.resolvedType instanceof Enum)) gen
if (!field.required) gen
("if(%s!==undefined&&%s!==null){", ref, ref);
else gen
("if(%s!==undefined){", ref);
if (field.partOf) {
var oneofProp = util.safeProp(field.partOf.name);
if (seenFirstField[field.partOf.name] === 1) gen
("if(p%s===1)", oneofProp)
("return%j", field.partOf.name + ": multiple values");
seenFirstField[field.partOf.name] = 1;
gen
("p%s=1", oneofProp);
}
genVerifyValue(gen, field, i, ref);
if (!field.required) gen
Expand Down
6 changes: 3 additions & 3 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ $root.Test1 = (function() {
Test1.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.field1 !== undefined)
if (message.field1 !== undefined && message.field1 !== null)
if (!$util.isString(message.field1))
return "field1: string expected";
if (message.field2 !== undefined)
if (message.field2 !== undefined && message.field2 !== null)
if (!$util.isInteger(message.field2))
return "field2: integer expected";
if (message.field3 !== undefined)
if (message.field3 !== undefined && message.field3 !== null)
if (typeof message.field3 !== "boolean")
return "field3: boolean expected";
return null;
Expand Down
8 changes: 4 additions & 4 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ $root.Message = (function() {
Message.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.stringVal !== undefined)
if (message.stringVal !== undefined && message.stringVal !== null)
if (!$util.isString(message.stringVal))
return "stringVal: string expected";
if (message.stringRepeated !== undefined) {
Expand All @@ -250,7 +250,7 @@ $root.Message = (function() {
if (!$util.isString(message.stringRepeated[i]))
return "stringRepeated: string[] expected";
}
if (message.uint64Val !== undefined)
if (message.uint64Val !== undefined && message.uint64Val !== null)
if (!$util.isInteger(message.uint64Val) && !(message.uint64Val && $util.isInteger(message.uint64Val.low) && $util.isInteger(message.uint64Val.high)))
return "uint64Val: integer|Long expected";
if (message.uint64Repeated !== undefined) {
Expand All @@ -260,7 +260,7 @@ $root.Message = (function() {
if (!$util.isInteger(message.uint64Repeated[i]) && !(message.uint64Repeated[i] && $util.isInteger(message.uint64Repeated[i].low) && $util.isInteger(message.uint64Repeated[i].high)))
return "uint64Repeated: integer|Long[] expected";
}
if (message.bytesVal !== undefined)
if (message.bytesVal !== undefined && message.bytesVal !== null)
if (!(message.bytesVal && typeof message.bytesVal.length === "number" || $util.isString(message.bytesVal)))
return "bytesVal: buffer expected";
if (message.bytesRepeated !== undefined) {
Expand All @@ -270,7 +270,7 @@ $root.Message = (function() {
if (!(message.bytesRepeated[i] && typeof message.bytesRepeated[i].length === "number" || $util.isString(message.bytesRepeated[i])))
return "bytesRepeated: buffer[] expected";
}
if (message.enumVal !== undefined)
if (message.enumVal !== undefined && message.enumVal !== null)
switch (message.enumVal) {
default:
return "enumVal: enum value expected";
Expand Down
30 changes: 15 additions & 15 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,25 +394,25 @@ $root.vector_tile = (function() {
Value.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.stringValue !== undefined)
if (message.stringValue !== undefined && message.stringValue !== null)
if (!$util.isString(message.stringValue))
return "stringValue: string expected";
if (message.floatValue !== undefined)
if (message.floatValue !== undefined && message.floatValue !== null)
if (typeof message.floatValue !== "number")
return "floatValue: number expected";
if (message.doubleValue !== undefined)
if (message.doubleValue !== undefined && message.doubleValue !== null)
if (typeof message.doubleValue !== "number")
return "doubleValue: number expected";
if (message.intValue !== undefined)
if (message.intValue !== undefined && message.intValue !== null)
if (!$util.isInteger(message.intValue) && !(message.intValue && $util.isInteger(message.intValue.low) && $util.isInteger(message.intValue.high)))
return "intValue: integer|Long expected";
if (message.uintValue !== undefined)
if (message.uintValue !== undefined && message.uintValue !== null)
if (!$util.isInteger(message.uintValue) && !(message.uintValue && $util.isInteger(message.uintValue.low) && $util.isInteger(message.uintValue.high)))
return "uintValue: integer|Long expected";
if (message.sintValue !== undefined)
if (message.sintValue !== undefined && message.sintValue !== null)
if (!$util.isInteger(message.sintValue) && !(message.sintValue && $util.isInteger(message.sintValue.low) && $util.isInteger(message.sintValue.high)))
return "sintValue: integer|Long expected";
if (message.boolValue !== undefined)
if (message.boolValue !== undefined && message.boolValue !== null)
if (typeof message.boolValue !== "boolean")
return "boolValue: boolean expected";
return null;
Expand Down Expand Up @@ -711,7 +711,7 @@ $root.vector_tile = (function() {
Feature.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.id !== undefined)
if (message.id !== undefined && message.id !== null)
if (!$util.isInteger(message.id) && !(message.id && $util.isInteger(message.id.low) && $util.isInteger(message.id.high)))
return "id: integer|Long expected";
if (message.tags !== undefined) {
Expand All @@ -721,7 +721,7 @@ $root.vector_tile = (function() {
if (!$util.isInteger(message.tags[i]))
return "tags: integer[] expected";
}
if (message.type !== undefined)
if (message.type !== undefined && message.type !== null)
switch (message.type) {
default:
return "type: enum value expected";
Expand Down Expand Up @@ -1066,7 +1066,7 @@ $root.vector_tile = (function() {
return "values." + error;
}
}
if (message.extent !== undefined)
if (message.extent !== undefined && message.extent !== null)
if (!$util.isInteger(message.extent))
return "extent: integer expected";
return null;
Expand Down Expand Up @@ -1142,18 +1142,16 @@ $root.vector_tile = (function() {
object.values = [];
}
if (options.defaults) {
object.version = 1;
object.name = "";
object.extent = 4096;
object.version = 1;
}
if (message.version !== undefined && message.version !== null && message.hasOwnProperty("version"))
object.version = message.version;
if (message.name !== undefined && message.name !== null && message.hasOwnProperty("name"))
object.name = message.name;
if (message.features !== undefined && message.features !== null && message.hasOwnProperty("features")) {
object.features = [];
for (var j = 0; j < message.features.length; ++j)
object.features[j] = $types[2].toObject(message.features[j], options);
object.features[j] = $types[1].toObject(message.features[j], options);
}
if (message.keys !== undefined && message.keys !== null && message.hasOwnProperty("keys")) {
object.keys = [];
Expand All @@ -1163,10 +1161,12 @@ $root.vector_tile = (function() {
if (message.values !== undefined && message.values !== null && message.hasOwnProperty("values")) {
object.values = [];
for (var j = 0; j < message.values.length; ++j)
object.values[j] = $types[4].toObject(message.values[j], options);
object.values[j] = $types[3].toObject(message.values[j], options);
}
if (message.extent !== undefined && message.extent !== null && message.hasOwnProperty("extent"))
object.extent = message.extent;
if (message.version !== undefined && message.version !== null && message.hasOwnProperty("version"))
object.version = message.version;
return object;
};

Expand Down
32 changes: 16 additions & 16 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,33 +340,33 @@ $root.Package = (function() {
Package.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.name !== undefined)
if (message.name !== undefined && message.name !== null)
if (!$util.isString(message.name))
return "name: string expected";
if (message.version !== undefined)
if (message.version !== undefined && message.version !== null)
if (!$util.isString(message.version))
return "version: string expected";
if (message.versionScheme !== undefined)
if (message.versionScheme !== undefined && message.versionScheme !== null)
if (!$util.isString(message.versionScheme))
return "versionScheme: string expected";
if (message.description !== undefined)
if (message.description !== undefined && message.description !== null)
if (!$util.isString(message.description))
return "description: string expected";
if (message.author !== undefined)
if (message.author !== undefined && message.author !== null)
if (!$util.isString(message.author))
return "author: string expected";
if (message.license !== undefined)
if (message.license !== undefined && message.license !== null)
if (!$util.isString(message.license))
return "license: string expected";
if (message.repository !== undefined && message.repository !== null) {
var error = $types[6].verify(message.repository);
if (error)
return "repository." + error;
}
if (message.bugs !== undefined)
if (message.bugs !== undefined && message.bugs !== null)
if (!$util.isString(message.bugs))
return "bugs: string expected";
if (message.homepage !== undefined)
if (message.homepage !== undefined && message.homepage !== null)
if (!$util.isString(message.homepage))
return "homepage: string expected";
if (message.keywords !== undefined) {
Expand All @@ -376,7 +376,7 @@ $root.Package = (function() {
if (!$util.isString(message.keywords[i]))
return "keywords: string[] expected";
}
if (message.main !== undefined)
if (message.main !== undefined && message.main !== null)
if (!$util.isString(message.main))
return "main: string expected";
if (message.bin !== undefined) {
Expand Down Expand Up @@ -419,7 +419,7 @@ $root.Package = (function() {
if (!$util.isString(message.devDependencies[key[i]]))
return "devDependencies: string{k:string} expected";
}
if (message.types !== undefined)
if (message.types !== undefined && message.types !== null)
if (!$util.isString(message.types))
return "types: string expected";
if (message.cliDependencies !== undefined) {
Expand Down Expand Up @@ -551,7 +551,6 @@ $root.Package = (function() {
if (options.defaults) {
object.name = "";
object.version = "";
object.versionScheme = "";
object.description = "";
object.author = "";
object.license = "";
Expand All @@ -560,21 +559,20 @@ $root.Package = (function() {
object.homepage = "";
object.main = "";
object.types = "";
object.versionScheme = "";
}
if (message.name !== undefined && message.name !== null && message.hasOwnProperty("name"))
object.name = message.name;
if (message.version !== undefined && message.version !== null && message.hasOwnProperty("version"))
object.version = message.version;
if (message.versionScheme !== undefined && message.versionScheme !== null && message.hasOwnProperty("versionScheme"))
object.versionScheme = message.versionScheme;
if (message.description !== undefined && message.description !== null && message.hasOwnProperty("description"))
object.description = message.description;
if (message.author !== undefined && message.author !== null && message.hasOwnProperty("author"))
object.author = message.author;
if (message.license !== undefined && message.license !== null && message.hasOwnProperty("license"))
object.license = message.license;
if (message.repository !== undefined && message.repository !== null && message.hasOwnProperty("repository"))
object.repository = $types[6].toObject(message.repository, options);
object.repository = $types[5].toObject(message.repository, options);
if (message.bugs !== undefined && message.bugs !== null && message.hasOwnProperty("bugs"))
object.bugs = message.bugs;
if (message.homepage !== undefined && message.homepage !== null && message.hasOwnProperty("homepage"))
Expand Down Expand Up @@ -618,6 +616,8 @@ $root.Package = (function() {
for (var j = 0; j < message.cliDependencies.length; ++j)
object.cliDependencies[j] = message.cliDependencies[j];
}
if (message.versionScheme !== undefined && message.versionScheme !== null && message.hasOwnProperty("versionScheme"))
object.versionScheme = message.versionScheme;
return object;
};

Expand Down Expand Up @@ -749,10 +749,10 @@ $root.Package = (function() {
Repository.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.type !== undefined)
if (message.type !== undefined && message.type !== null)
if (!$util.isString(message.type))
return "type: string expected";
if (message.url !== undefined)
if (message.url !== undefined && message.url !== null)
if (!$util.isString(message.url))
return "url: string expected";
return null;
Expand Down
4 changes: 2 additions & 2 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ $root.MyRequest = (function() {
MyRequest.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.path !== undefined)
if (message.path !== undefined && message.path !== null)
if (!$util.isString(message.path))
return "path: string expected";
return null;
Expand Down Expand Up @@ -336,7 +336,7 @@ $root.MyResponse = (function() {
MyResponse.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.status !== undefined)
if (message.status !== undefined && message.status !== null)
if (!$util.isInteger(message.status))
return "status: integer expected";
return null;
Expand Down
Loading

0 comments on commit adb4bb0

Please sign in to comment.