Skip to content

Commit

Permalink
Merge pull request #217 from Teamwork/fix-op-validation
Browse files Browse the repository at this point in the history
Allow op to be a non-object
  • Loading branch information
nateps authored Jul 23, 2018
2 parents 632fad3 + 90b46d6 commit be3f161
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ot.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ exports.apply = function(snapshot, op) {
function applyOpEdit(snapshot, edit) {
if (!snapshot.type) return {code: 4015, message: 'Document does not exist'};

if (typeof edit !== 'object') return {code: 5004, message: 'Missing op'};
if (edit == null) return {code: 5004, message: 'Missing op'};
var type = types[snapshot.type];
if (!type) return {code: 4008, message: 'Unknown type'};

Expand Down
23 changes: 23 additions & 0 deletions test/client/number-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// A simple number type, where:
//
// - snapshot is an integer
// - operation is an integer
exports.type = {
name: 'number-type',
uri: 'http://sharejs.org/types/number-type',
create: create,
apply: apply,
transform: transform
};

function create(data) {
return data | 0;
}

function apply(snapshot, op) {
return snapshot + op;
}

function transform(op1, op2, side) {
return op1;
}
15 changes: 15 additions & 0 deletions test/client/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ var async = require('async');
var expect = require('expect.js');
var types = require('../../lib/types');
var deserializedType = require('./deserialized-type');
var numberType = require('./number-type');
types.register(deserializedType.type);
types.register(deserializedType.type2);
types.register(numberType.type);

module.exports = function() {
describe('client submit', function() {
Expand Down Expand Up @@ -1073,6 +1075,19 @@ describe('client submit', function() {
});
});

it('allows snapshot and op to be a non-object', function(done) {
var doc = this.backend.connect().get('dogs', 'fido');
doc.create(5, numberType.type.uri, function (err) {
if (err) return done(err);
expect(doc.data).to.equal(5);
doc.submitOp(2, function(err) {
if (err) return done(err);
expect(doc.data).to.equal(7);
done();
});
});
});

describe('type.deserialize', function() {
it('can create a new doc', function(done) {
var doc = this.backend.connect().get('dogs', 'fido');
Expand Down

0 comments on commit be3f161

Please sign in to comment.