Skip to content

Commit

Permalink
Tweak logical type validity check.
Browse files Browse the repository at this point in the history
This change will enable error hooks to work even when a logical type is
valid for a subset of its underlying type's values (e.g. when defining
an "even integer" type). Previously the path would have been correct but
the type would have been that of the underlying one.

Also expose `assertLogicalTypes` parsing option.
  • Loading branch information
mtth committed Dec 3, 2015
1 parent af377a1 commit 1d5d14c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doc
Submodule doc updated from cdebbb to 243b94
26 changes: 17 additions & 9 deletions lib/schemas.js
Expand Up @@ -102,7 +102,7 @@ function createType(attrs, opts) {
try {
return new DerivedType(attrs, opts);
} catch (err) {
if (opts.assertLogicalType) {
if (opts.assertLogicalTypes) {
// The spec mandates that we fall through to the underlying type if
// the logical type is invalid. We provide this option to ease
// debugging.
Expand Down Expand Up @@ -1792,23 +1792,31 @@ LogicalType.prototype._read = function (tap) {
return this._fromValue(this._underlyingType._read(tap));
};

LogicalType.prototype._write = function (tap, val) {
this._underlyingType._write(tap, this._toValue(val));
LogicalType.prototype._write = function (tap, any) {
this._underlyingType._write(tap, this._toValue(any));
};

LogicalType.prototype._check = function (val, cb) {
return this._underlyingType._check(this._toValue(val), cb);
LogicalType.prototype._check = function (any, cb) {
try {
var val = this._toValue(any);
} catch (err) {
if (cb) {
cb(PATH.slice(), any, this);
}
return false;
}
return this._underlyingType._check(val, cb);
};

LogicalType.prototype._copy = function (val, opts) {
LogicalType.prototype._copy = function (any, opts) {
var type = this._underlyingType;
switch (opts && opts.coerce) {
case 3: // To string.
return type._copy(this._toValue(val), opts);
return type._copy(this._toValue(any), opts);
case 2: // From string.
return this._fromValue(type._copy(val, opts));
return this._fromValue(type._copy(any, opts));
default: // Normal copy.
return this._fromValue(type._copy(this._toValue(val), opts));
return this._fromValue(type._copy(this._toValue(any), opts));
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "avsc",
"version": "3.1.0",
"version": "3.1.1",
"description": "Blazing fast serialization",
"homepage": "https://github.com/mtth/avsc",
"keywords": [
Expand Down
33 changes: 31 additions & 2 deletions test/test_schemas.js
Expand Up @@ -1884,7 +1884,7 @@ suite('types', function () {
assert.throws(function () {
createType(attrs, {
logicalTypes: logicalTypes,
assertLogicalType: true
assertLogicalTypes: true
});
});
});
Expand Down Expand Up @@ -2066,7 +2066,6 @@ suite('types', function () {
if (any === null) {
return null;
}

var obj = {};
obj[this._name] = any;
return obj;
Expand All @@ -2089,6 +2088,36 @@ suite('types', function () {

});

test('even integer', function () {
function EvenIntType(attrs, opts) {
types.LogicalType.call(this, attrs, opts, [types.IntType]);
}
util.inherits(EvenIntType, types.LogicalType);
EvenIntType.prototype._fromValue = function (val) {
this._assertValid(val);
return val;
};
EvenIntType.prototype._toValue = EvenIntType.prototype._fromValue;
EvenIntType.prototype._assertValid = function (any) {
if (any !== (any | 0) || any % 2) {
throw new Error('invalid');
}
};

var opts = {logicalTypes: {'even-integer': EvenIntType}};
var t = createType({type: 'int', logicalType: 'even-integer'}, opts);
assert(t.isValid(2));
assert(!t.isValid(3));
assert(!t.isValid('abc'));
assert.equal(t.fromBuffer(new Buffer([4])), 2);
assert.equal(t.clone(4), 4);
assert.equal(t.fromString('6'), 6);
assert.throws(function () { t.clone(3); });
assert.throws(function () { t.fromString('5'); });
assert.throws(function () { t.toBuffer(3); });
assert.throws(function () { t.fromBuffer(new Buffer([2])); });
});

});

suite('createType', function () {
Expand Down

0 comments on commit 1d5d14c

Please sign in to comment.