Skip to content

Commit

Permalink
Improve node-avro-io error reporting on schema
Browse files Browse the repository at this point in the history
  • Loading branch information
thecsyco committed Mar 12, 2015
1 parent 387e4d2 commit 4ed52ee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 1 addition & 3 deletions lib/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,7 @@ DatumWriter.prototype = {
if (!(writersSchema instanceof Avro.Schema))
throw new AvroErrors.IOError("writersSchema is not a valid schema object, it is %j", writersSchema);

if (!writersSchema.validate(writersSchema.type, datum)) {
throw new AvroErrors.IOError("Data %j is not valid type of %j", datum, writersSchema.type);
}
writersSchema.validateAndThrow(writersSchema.type, datum);

switch(writersSchema.type) {
case "null": encoder.writeNull(datum); break;
Expand Down
29 changes: 20 additions & 9 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,39 +129,39 @@ _.extend(Schema.prototype, {
}
},

validate: function(schema, datum){
validateAndThrow: function(schema, datum){
// primitive types
switch (schema) {
case 'null':
if (!_.isNull(datum))
return false;
throw new AvroErrors.DataValidationError("Data [%j] is not null", datum);
break;
case 'boolean':
if (!_.isBoolean(datum))
return false;
throw new AvroErrors.DataValidationError("Data [%j] is not boolean", datum);
break;
case 'int':
case 'long':
case 'float':
case 'double':
if (!_.isNumber(datum) || datum === null)
return false;
throw new AvroErrors.DataValidationError("Data [%j] is not a number or not defined", datum);
break;
case 'bytes':
if (datum === null)
return false;
throw new AvroErrors.DataValidationError("Data [%j] not defined", datum);
break;
case 'string':
if (!_.isString(datum))
return false;
throw new AvroErrors.DataValidationError("Data [%j] is not a string", datum);
break;
case 'enum':
if (datum === null || _.indexOf(this.symbols, datum) === -1)
return false;
throw new AvroErrors.DataValidationError("Data [%j] not a valid enum value. List of valuies [%j]", datum, this.symbols);
break;
case 'array':
if (datum === null || !Array.isArray(datum))
return false;
throw new AvroErrors.DataValidationError("Data [%j] not a an array", datum, this.symbols);
break;
case 'record':
if (datum === null)
Expand All @@ -170,14 +170,25 @@ _.extend(Schema.prototype, {
var dFields = _.keys(datum);
var intersect = _.intersection(fields, dFields);
if (intersect.length < dFields.length)
return false
throw new AvroErrors.DataValidationError("Data [%j] has extra fields not in schema. data fields [%j]. schema fields [%j]", datum, dFields, fields);
break;
default:
break;
}

return true;
},

validate: function(schema, datum){
var self = this;
try {
self.validateAndThrow(schema, datum);
} catch (validateErr) {
return false;
}
return true;
},

isPrimitive: function(schema){
switch (schema) {
case 'null':
Expand Down
8 changes: 6 additions & 2 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ _.extend(Schema.prototype, {

validate: function(schema, datum){
return true;
},

},

validateAndThrow: function(schema, datum){
return true;
},

toString: function() {
return JSON.stringify({ type: this.type });
}
Expand Down

0 comments on commit 4ed52ee

Please sign in to comment.