Skip to content

Commit

Permalink
Merge branch 'master' of github.com:neumino/thinky
Browse files Browse the repository at this point in the history
  • Loading branch information
neumino committed Jul 12, 2015
2 parents 9b31ce8 + 03790d8 commit a4c1cca
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,11 @@ Document.prototype.__makeSavableCopy = function(doc, schema, options, model, r)

// model is an instance of a Model (for the top level fields), or undefined
var result, key, keys, nextSchema, copyFlag;
if (type.isDate(schema) && (typeof doc === 'string')) {
if (type.isDate(schema) && (typeof doc === 'string' || typeof doc === 'number')) {
var numericDate = parseInt(doc, 10);
if(!isNaN(numericDate)) {
doc = numericDate;
}
return new Date(doc); // Use r.ISO8601 and not `new Date()` to keep timezone
}
else if (type.isPoint(schema)) {
Expand All @@ -455,6 +459,14 @@ Document.prototype.__makeSavableCopy = function(doc, schema, options, model, r)
return doc;
}
}
else if (type.isNumber(schema) && (typeof doc === 'string')) {
var numericString = parseFloat(doc);
if(!isNaN(numericString)){
return numericString;
}else{
return doc;
}
}

if (util.isPlainObject(doc) && (doc instanceof Buffer === false)) {
result = {};
Expand Down
6 changes: 5 additions & 1 deletion lib/type/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ TypeDate.prototype.validate = function(date, prefix, options) {
// TOIMPROVE -- we currently just check if it's a term from the driver
// We suppose for now that this is enough and we don't throw an error
}
else if (typeof date === 'string') {
else if (typeof date === 'string' || typeof date === 'number') {
var numericDate = parseInt(date, 10);
if(!isNaN(numericDate)){
date = numericDate;
}
jsDate = new Date(date);
if (jsDate.getTime() !== jsDate.getTime()) {
if (options.enforce_type === "strict") {
Expand Down
7 changes: 7 additions & 0 deletions lib/type/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ TypeNumber.prototype.validate = function(number, prefix, options) {
throw new Errors.ValidationError("Validator for the field "+prefix+" returned `false`.");
}

if(typeof number === 'string'){
var numericString = parseFloat(number);
if(!isNaN(numericString)){
number = numericString;
}
}

if ((typeof number === 'function') && (number._query !== undefined)) {
// We do not check ReQL terms
}
Expand Down
38 changes: 38 additions & 0 deletions test/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ describe('save', function() {
done()
}).error(done)
});
it('Date as number should be coerced to ReQL dates', function(done){
var Model = thinky.createModel(modelNames[0], {
id: String,
date: Date
})
var t = new Model({
id: util.s8(),
date: Date.now()
});

t.save().then(function(result) {
assert(t.date instanceof Date)

return Model.get(t.id).execute({timeFormat: "raw"})
}).then(function(result) {
assert.equal(Object.prototype.toString.call(result.date), "[object Object]");
assert.equal(result.date.$reql_type$, "TIME");
done()
}).error(done)
});
it('Points as array should be coerced to ReQL points', function(done){
var Model = thinky.createModel(modelNames[0], {
id: String,
Expand Down Expand Up @@ -384,6 +404,24 @@ describe('save', function() {
done()
}).error(done)
});
it('Number as string should be coerced to number', function(done){
var Model = thinky.createModel(modelNames[0], {
id: String,
number: Number
})
var t = new Model({
id: util.s8(),
number: "123456"
});

t.save().then(function(result) {
return Model.get(t.id).execute()
}).then(function(result) {
assert.equal(typeof t.number, "number")
assert.equal(t.number, 123456)
done()
}).error(done)
});
});
describe("Joins - hasOne", function() {
afterEach(cleanTables);
Expand Down
24 changes: 24 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,14 @@ describe('Chainable types', function(){
var doc = new Model({ id: null })
doc.validate();
});
it('Date - number', function(){
var name = util.s8();
var Model = thinky.createModel(name,
{id: type.date()},
{init: false})
var doc = new Model({ id: Date.now() })
doc.validate();
});
it('Date - basic - wrong type', function(){
var name = util.s8();
var Model = thinky.createModel(name,
Expand Down Expand Up @@ -1869,6 +1877,22 @@ describe('validate', function(){

doc.validate();
});
it('Number - not wrong type - numeric string', function(){
var name = util.s8();
var str = util.s8();

var Model = thinky.createModel(name, {
id: String,
field: Number
}, {init: false})

doc = new Model({
id: str,
field: "123456"
})

doc.validate();
});
it('Boolean - wrong type - type: "strict"', function(){
var name = util.s8();
var str = util.s8();
Expand Down

0 comments on commit a4c1cca

Please sign in to comment.