不要怂,就是干,撸起袖子干!

deprecate notNull built-in validator

1 parent 5d219eaf
...@@ -5,10 +5,9 @@ var Validator = require("validator") ...@@ -5,10 +5,9 @@ var Validator = require("validator")
function noop() {} function noop() {}
// Backwards compat for people using old validation function // Deprecate this.
// We cannot use .extend, since it coerces the first arg to string Validator.notNull = function () {
Validator.notNull = function (val) { throw new Error('Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"');
return [null, undefined].indexOf(val) === -1
} }
// https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js // https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js
...@@ -137,9 +136,9 @@ DaoValidator.prototype.validate = function() { ...@@ -137,9 +136,9 @@ DaoValidator.prototype.validate = function() {
self._customValidators(), self._customValidators(),
]).then(function () { ]).then(function () {
if (Object.keys(self.errors).length) { if (Object.keys(self.errors).length) {
emitter.emit('success', self.errors) emitter.emit('success', self.errors)
} else { } else {
emitter.emit('success') emitter.emit('success')
} }
}) })
}).run() }).run()
...@@ -245,8 +244,7 @@ DaoValidator.prototype._customValidators = function() { ...@@ -245,8 +244,7 @@ DaoValidator.prototype._customValidators = function() {
DaoValidator.prototype._builtinAttrValidate = function(value, field) { DaoValidator.prototype._builtinAttrValidate = function(value, field) {
var self = this; var self = this;
// check if value is null (if null not allowed the Schema pass will capture it) // check if value is null (if null not allowed the Schema pass will capture it)
if (!('notNull' in this.modelInstance.validators[field]) && if (value === null || typeof value === 'undefined') {
(value === null || typeof value === 'undefined')) {
return Promise.resolve(); return Promise.resolve();
} }
......
...@@ -376,42 +376,42 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -376,42 +376,42 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
UserNull.create({ username: 'foo2', smth: null }).error(function(err) { UserNull.create({ username: 'foo2', smth: null }).error(function(err) {
expect(err).to.exist expect(err).to.exist
expect(err.smth[0].path).to.equal('smth'); expect(err.smth[0].path).to.equal('smth');
if (Support.dialectIsMySQL()) { if (Support.dialectIsMySQL()) {
// We need to allow two different errors for MySQL, see: // We need to allow two different errors for MySQL, see:
// http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_trans_tables // http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_trans_tables
expect(err.smth[0].message).to.match(/notNull Violation/) expect(err.smth[0].message).to.match(/notNull Violation/)
} }
else if (dialect === "sqlite") { else if (dialect === "sqlite") {
expect(err.smth[0].message).to.match(/notNull Violation/) expect(err.smth[0].message).to.match(/notNull Violation/)
} else { } else {
expect(err.smth[0].message).to.match(/notNull Violation/) expect(err.smth[0].message).to.match(/notNull Violation/)
} }
done() done()
}) })
}) })
}) })
it("raises an error if created object breaks definition contraints", function(done) { it("raises an error if created object breaks definition contraints", function(done) {
var UserNull = this.sequelize.define('UserWithNonNullSmth', { var UserNull = this.sequelize.define('UserWithNonNullSmth', {
username: { type: Sequelize.STRING, unique: true }, username: { type: Sequelize.STRING, unique: true },
smth: { type: Sequelize.STRING, allowNull: false } smth: { type: Sequelize.STRING, allowNull: false }
}) })
this.sequelize.options.omitNull = false this.sequelize.options.omitNull = false
UserNull.sync({ force: true }).success(function() { UserNull.sync({ force: true }).success(function() {
UserNull.create({ username: 'foo', smth: 'foo' }).success(function() { UserNull.create({ username: 'foo', smth: 'foo' }).success(function() {
UserNull.create({ username: 'foo', smth: 'bar' }).error(function(err) { UserNull.create({ username: 'foo', smth: 'bar' }).error(function(err) {
expect(err).to.exist expect(err).to.exist
if (dialect === "sqlite") { if (dialect === "sqlite") {
expect(err.message).to.match(/.*SQLITE_CONSTRAINT.*/) expect(err.message).to.match(/.*SQLITE_CONSTRAINT.*/)
} }
else if (Support.dialectIsMySQL()) { else if (Support.dialectIsMySQL()) {
expect(err.message).to.match(/Duplicate entry 'foo' for key 'username'/) expect(err.message).to.match(/Duplicate entry 'foo' for key 'username'/)
} else { } else {
expect(err.message).to.match(/.*duplicate key value violates unique constraint.*/) expect(err.message).to.match(/.*duplicate key value violates unique constraint.*/)
} }
done() done()
}) })
}) })
}) })
...@@ -431,12 +431,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -431,12 +431,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(str2.str).to.equal('http://sequelizejs.org') expect(str2.str).to.equal('http://sequelizejs.org')
StringIsNullOrUrl.create({ str: '' }).error(function(err) { StringIsNullOrUrl.create({ str: '' }).error(function(err) {
expect(err).to.exist expect(err).to.exist
expect(err.str[0].message).to.match(/Validation isURL failed/) expect(err.str[0].message).to.match(/Validation isURL failed/)
done() done()
}) })
}) })
}).error(done) }).error(done)
}) })
}) })
...@@ -952,9 +952,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -952,9 +952,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var Tasks = this.sequelize.define('Task', { var Tasks = this.sequelize.define('Task', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: { allowNull: false,
notNull: { args: true, msg: 'name cannot be null' }
}
}, },
code: { code: {
type: Sequelize.STRING, type: Sequelize.STRING,
...@@ -968,16 +966,16 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -968,16 +966,16 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
Tasks.bulkCreate([ Tasks.bulkCreate([
{name: 'foo', code: '123'}, {name: 'foo', code: '123'},
{code: '1234'}, {code: '1234'},
{name: 'bar', code: '1'} {name: 'bar', code: '1'}
], { validate: true }).error(function(errors) { ], { validate: true }).error(function(errors) {
expect(errors).to.not.be.null expect(errors).to.not.be.null
expect(errors).to.be.an('Array') expect(errors).to.be.an('Array')
expect(errors).to.have.length(2) expect(errors).to.have.length(2)
expect(errors[0].record.code).to.equal('1234') expect(errors[0].record.code).to.equal('1234')
expect(errors[0].errors.name[0].message).to.equal('name cannot be null') expect(errors[0].errors.name[0].message).to.equal('notNull Violation')
expect(errors[1].record.name).to.equal('bar') expect(errors[1].record.name).to.equal('bar')
expect(errors[1].record.code).to.equal('1') expect(errors[1].record.code).to.equal('1')
expect(errors[1].errors.code[0].message).to.equal('Validation len failed') expect(errors[1].errors.code[0].message).to.equal('Validation len failed')
done() done()
}) })
}) })
...@@ -988,8 +986,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -988,8 +986,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: { validate: {
notNull: { args: true, msg: 'name cannot be null' } notEmpty: true,
} },
}, },
code: { code: {
type: Sequelize.STRING, type: Sequelize.STRING,
......
...@@ -68,10 +68,6 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -68,10 +68,6 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
fail: "a", fail: "a",
pass: "9.2" pass: "9.2"
} }
, notNull : {
fail: null,
pass: 0
}
, isNull : { , isNull : {
fail: 0, fail: 0,
pass: null pass: null
...@@ -291,8 +287,8 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -291,8 +287,8 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
var Model = this.sequelize.define('model', { var Model = this.sequelize.define('model', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
allowNull: false,
validate: { validate: {
notNull: true, // won't allow null
notEmpty: true // don't allow empty strings notEmpty: true // don't allow empty strings
} }
} }
...@@ -313,8 +309,8 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -313,8 +309,8 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
var Model = this.sequelize.define('model', { var Model = this.sequelize.define('model', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
allowNull: false,
validate: { validate: {
notNull: true, // won't allow null
notEmpty: true // don't allow empty strings notEmpty: true // don't allow empty strings
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!