Merge pull request #270 from letsface/fix-number-to-string

When query is number but property is string convert back to string
This commit is contained in:
Ritchie Martori
2013-10-18 09:36:15 -07:00
2 changed files with 29 additions and 1 deletions

View File

@@ -123,6 +123,8 @@ Collection.prototype.sanitize = function (body) {
sanitized[key] = val;
} else if(expected == 'number' && actual == 'string') {
sanitized[key] = parseFloat(val);
} else if(expected == 'string' && actual == 'number') {
sanitized[key] = '' + val;
}
});
@@ -147,7 +149,9 @@ Collection.prototype.sanitizeQuery = function (query) {
if (key === '$limitRecursion') return;
if (key === '$skipEvents') return;
if(expected == 'number' && actual == 'string') {
if(expected == 'string' && actual == 'number') {
sanitized[key] = '' + val;
} else if(expected == 'number' && actual == 'string') {
sanitized[key] = parseFloat(val);
} else if(expected == 'boolean' && actual != 'boolean') {
sanitized[key] = (val === 'true') ? true : false;

View File

@@ -81,8 +81,32 @@ describe('collection', function(){
var sanitized = r.sanitize({age: '22'});
expect(sanitized.age).to.equal(22);
});
it('should convert number to strings', function() {
var r = createCollection({
token: {
type: 'string'
}
});
var sanitized = r.sanitize({token: 123456});
expect(sanitized.token).to.equal('123456');
});
});
describe('.sanitizeQuery(query)', function(){
it('should convert number to strings', function() {
var r = createCollection({
token: {
type: 'string'
}
});
var sanitized = r.sanitizeQuery({token: 123456});
expect(sanitized.token).to.equal('123456');
});
});
describe('.handle(ctx)', function(){
it('should have a store', function() {
var c = new Collection('foo', { db: db.create(TEST_DB) });