waterline
Advanced tools
Comparing version 0.9.3 to 0.9.4
@@ -74,4 +74,4 @@ /** | ||
* afterValidation | ||
* beforeSave | ||
* afterSave | ||
* beforeUpdate | ||
* afterUpdate | ||
* beforeCreate | ||
@@ -78,0 +78,0 @@ * afterCreate |
@@ -89,3 +89,3 @@ /** | ||
// Just a double check to exit if hasOwnProperty fails | ||
if(!obj.hasOwnProperty(property)) return; | ||
if(!Object.prototype.hasOwnProperty.call(obj, property)) return; | ||
@@ -92,0 +92,0 @@ // If Nested Object call function again passing the property as obj |
@@ -46,19 +46,21 @@ /** | ||
//add custom type definitions to anchor | ||
// add custom type definitions to anchor | ||
types = types || {}; | ||
anchor.define(types); | ||
var validations = this.validations; | ||
for(var attr in attrs) { | ||
this.validations[attr] = {}; | ||
var validation = validations[attr] = {}; | ||
var attrsVal = attrs[attr]; | ||
for(var prop in attrs[attr]) { | ||
if(['defaultsTo', 'primaryKey', 'autoIncrement', 'unique', 'index', 'columnName'].indexOf(prop) > -1) continue; | ||
for(var prop in attrsVal) { | ||
if(/^(defaultsTo|primaryKey|autoIncrement|unique|index|columnName)$/.test(prop)) continue; | ||
// use the Anchor `in` method for enums | ||
if(prop === 'enum') { | ||
this.validations[attr]['in'] = attrs[attr][prop]; | ||
continue; | ||
validation['in'] = attrsVal[prop]; | ||
} | ||
this.validations[attr][prop] = attrs[attr][prop]; | ||
else { | ||
validation[prop] = attrsVal[prop]; | ||
} | ||
} | ||
@@ -88,7 +90,5 @@ } | ||
cb = presentOnly; | ||
presentOnly = false; | ||
} | ||
// Use present values only or all validations | ||
if(presentOnly) { | ||
else if(presentOnly) { | ||
validations = _.intersection(validations, Object.keys(values)); | ||
@@ -98,5 +98,6 @@ } | ||
function validate(validation, cb) { | ||
var curValidation = self.validations[validation]; | ||
// Build Requirements | ||
var requirements = anchor(self.validations[validation]); | ||
var requirements = anchor(curValidation); | ||
@@ -109,3 +110,3 @@ // Grab value and set to null if undefined | ||
// try and validate it | ||
if(!self.validations[validation].required) { | ||
if(!curValidation.required) { | ||
if(value === null || value === '') return cb(); | ||
@@ -115,6 +116,6 @@ } | ||
// Ignore Text type validation | ||
if(self.validations[validation].type === 'text') return cb(); | ||
if(curValidation.type === 'text') return cb(); | ||
// If Boolean and required manually check | ||
if(self.validations[validation].required && self.validations[validation].type === 'boolean') { | ||
if(curValidation.required && curValidation.type === 'boolean') { | ||
if(value.toString() == 'true' || value.toString() == 'false') return cb(); | ||
@@ -129,10 +130,3 @@ } | ||
// Build an Error Object | ||
errors[validation] = []; | ||
err.forEach(function(obj) { | ||
if(obj.property) delete obj.property; | ||
errors[validation].push({ rule: obj.rule, message: obj.message }); | ||
}); | ||
errors[validation] = err; | ||
return cb(); | ||
@@ -144,5 +138,5 @@ } | ||
if(Object.keys(errors).length === 0) return cb(); | ||
cb(errors); | ||
cb({ 'ValidationError': errors }); | ||
}); | ||
}; |
@@ -9,3 +9,4 @@ /** | ||
Deferred = require('./deferred'), | ||
async = require('async'); | ||
async = require('async'), | ||
_ = require('underscore'); | ||
@@ -56,3 +57,3 @@ module.exports = { | ||
if(!record[key] && record[key] !== false && self.attributes[key].hasOwnProperty('defaultsTo')) { | ||
record[key] = self.attributes[key].defaultsTo; | ||
record[key] = _.clone(self.attributes[key].defaultsTo); | ||
} | ||
@@ -244,3 +245,3 @@ } | ||
if(!record[key] && record[key] !== false && self.attributes[key].hasOwnProperty('defaultsTo')) { | ||
record[key] = self.attributes[key].defaultsTo; | ||
record[key] = _.clone(self.attributes[key].defaultsTo); | ||
} | ||
@@ -247,0 +248,0 @@ } |
@@ -9,3 +9,4 @@ /** | ||
Deferred = require('./deferred'), | ||
async = require('async'); | ||
async = require('async'), | ||
_ = require('underscore'); | ||
@@ -51,3 +52,3 @@ module.exports = { | ||
if(!values[key] && values[key] !== false && this.attributes[key].hasOwnProperty('defaultsTo')) { | ||
values[key] = this.attributes[key].defaultsTo; | ||
values[key] = _.clone(this.attributes[key].defaultsTo); | ||
} | ||
@@ -54,0 +55,0 @@ } |
@@ -10,3 +10,4 @@ /** | ||
Deferred = require('./deferred'), | ||
async = require('async'); | ||
async = require('async'), | ||
_ = require('underscore'); | ||
@@ -56,4 +57,4 @@ module.exports = { | ||
for(var key in this.attributes) { | ||
if(!values[key] && values[key] !== false && this.attributes[key].hasOwnProperty('defaultsTo')) { | ||
values[key] = this.attributes[key].defaultsTo; | ||
if(values[key] === undefined && this.attributes[key].hasOwnProperty('defaultsTo')) { | ||
values[key] = _.clone(this.attributes[key].defaultsTo); | ||
} | ||
@@ -60,0 +61,0 @@ } |
@@ -65,8 +65,13 @@ var _ = require('underscore'); | ||
// make criteria an object | ||
criteria.sort = {}; | ||
criteria.sort[parts[0]] = parts[1] ? parts[1].toLowerCase() : 'asc'; | ||
if(parts.length !== 2 || (parts[1].toLowerCase() !== 'asc' && parts[1].toLowerCase() !== 'desc')) { | ||
// Set default sort to asc | ||
parts[1] = parts[1] ? parts[1].toLowerCase() : 'asc'; | ||
// Throw error on invalid sort order | ||
if(parts[1] !== 'asc' && parts[1] !== 'desc') { | ||
throw new Error('Invalid sort criteria :: ' + criteria.sort); | ||
} | ||
// Expand criteria.sort into object | ||
criteria.sort = {}; | ||
criteria.sort[parts[0]] = parts[1]; | ||
} | ||
@@ -73,0 +78,0 @@ |
{ | ||
"name": "waterline", | ||
"description": "An ORM for Node.js and the Sails framework", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"contributors": [ | ||
@@ -22,3 +22,3 @@ { | ||
"async": "0.2.9", | ||
"anchor": "~0.9.1", | ||
"anchor": "~0.9.4", | ||
"q": "0.9.4" | ||
@@ -25,0 +25,0 @@ }, |
@@ -78,4 +78,13 @@ ![image_squidhome@2x.png](http://i.imgur.com/7rMxw.png) | ||
Available options are: `tableName`, `adapter`, `schema`, `attributes`, along with any class methods and lifecycle callbacks you define. | ||
#### options | ||
Available options are | ||
- `tableName` Define a custom table name to store the models | ||
- `adapters` the name of the adapter you would like to use for this collection | ||
- `schema` Set schema true/false to only allow fields defined in `attributes` to be saved. Only for schemaless adapters. | ||
- `attributes` A hash of attributes to be defined for a model | ||
- [lifecyle callbacks](#lifecycle-callbacks) | ||
- anyother class method you define! | ||
#### Attributes | ||
@@ -154,4 +163,4 @@ | ||
* afterValidation | ||
* beforeSave | ||
* afterSave | ||
* beforeUpdate | ||
* afterUpdate | ||
* beforeCreate | ||
@@ -264,4 +273,7 @@ * afterCreate | ||
}); | ||
``` | ||
**Promises** | ||
```javascript | ||
User.findOne() | ||
@@ -349,3 +361,30 @@ .where({ id: 2 }) | ||
``` | ||
## Custom Types | ||
You can define your own types and their validation with the `types` hash | ||
```javascript | ||
var User = Waterline.Collection.extend({ | ||
types: { | ||
point: function(latlng){ | ||
return latlng.x && latlng.y | ||
} | ||
}, | ||
attributes: { | ||
firstName: { | ||
type: 'string', | ||
required: true, | ||
minLength: 5, | ||
maxLength: 15 | ||
}, | ||
location: { | ||
//note, that the base type (json) still has to be define | ||
type: 'json', | ||
point: true | ||
} | ||
} | ||
}); | ||
``` | ||
## Indexing | ||
@@ -352,0 +391,0 @@ |
@@ -51,3 +51,4 @@ var Waterline = require('../../lib/waterline'), | ||
assert(!user); | ||
assert(err.name[0].rule === 'required'); | ||
assert(err.ValidationError); | ||
assert(err.ValidationError.name[0].rule === 'required'); | ||
done(); | ||
@@ -68,3 +69,4 @@ }); | ||
assert(!user); | ||
assert(err.sex[0].rule === 'in'); | ||
assert(err.ValidationError); | ||
assert(err.ValidationError.sex[0].rule === 'in'); | ||
done(); | ||
@@ -71,0 +73,0 @@ }); |
@@ -22,2 +22,6 @@ var Collection = require('../../../lib/waterline/collection'), | ||
}, | ||
arr: { | ||
type: 'array', | ||
defaultsTo: [] | ||
}, | ||
doSomething: function() {} | ||
@@ -60,2 +64,13 @@ } | ||
it('should clone default values for each record', function(done) { | ||
query.createEach([{},{}], function(err, values) { | ||
assert(Array.isArray(values)); | ||
assert(values[0].arr !== values[1].arr); | ||
values[1].arr.push('another'); | ||
assert(values[0].arr.length === 0); | ||
assert(values[1].arr.length === 1); | ||
done(); | ||
}); | ||
}); | ||
it('should strip values that don\'t belong to the schema', function(done) { | ||
@@ -62,0 +77,0 @@ query.createEach([{ foo: 'bar' }], function(err, values) { |
@@ -24,4 +24,5 @@ var Validator = require('../../../lib/waterline/core/validations'), | ||
validator.validate({ sex: 'other' }, function(errors) { | ||
assert(errors.sex); | ||
assert(errors.sex[0].rule === 'in'); | ||
assert(errors.ValidationError); | ||
assert(errors.ValidationError.sex); | ||
assert(errors.ValidationError.sex[0].rule === 'in'); | ||
done(); | ||
@@ -28,0 +29,0 @@ }); |
@@ -37,3 +37,4 @@ var Validator = require('../../../lib/waterline/core/validations'), | ||
validator.validate({ firstName: 'f' }, function(errors) { | ||
assert(errors.firstName); | ||
assert(errors.ValidationError); | ||
assert(errors.ValidationError.firstName); | ||
done(); | ||
@@ -55,3 +56,4 @@ }); | ||
validator.validate({ lastName: 'foobar' }, function(errors) { | ||
assert(errors.lastName); | ||
assert(errors.ValidationError); | ||
assert(errors.ValidationError.lastName); | ||
done(); | ||
@@ -58,0 +60,0 @@ }); |
@@ -25,4 +25,5 @@ var Validator = require('../../../lib/waterline/core/validations'), | ||
validator.validate({ name: ' ', age: 27 }, function(errors) { | ||
assert(errors.name); | ||
assert(errors.name[0].rule === 'required'); | ||
assert(errors.ValidationError); | ||
assert(errors.ValidationError.name); | ||
assert(errors.ValidationError.name[0].rule === 'required'); | ||
done(); | ||
@@ -29,0 +30,0 @@ }); |
@@ -30,3 +30,4 @@ var Validator = require('../../../lib/waterline/core/validations'), | ||
validator.validate({ email: 'foobar' }, function(errors) { | ||
assert(errors.email); | ||
assert(errors.ValidationError); | ||
assert(errors.ValidationError.email); | ||
done(); | ||
@@ -33,0 +34,0 @@ }); |
@@ -36,3 +36,4 @@ var Validator = require('../../../lib/waterline/core/validations'), | ||
validator.validate({ age: 'foo bar' }, function(errors) { | ||
assert(errors.age); | ||
assert(errors.ValidationError); | ||
assert(errors.ValidationError.age); | ||
done(); | ||
@@ -44,2 +45,2 @@ }); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
265425
121
8050
440
0
Updatedanchor@~0.9.4