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

Commit 46bd87cc by Jan Aagaard Meier

Make validator tests run again, and add a lot of methods from validator@1.5 for backwards compat

1 parent 9f1d4c8f
...@@ -2,10 +2,12 @@ var Validator = require("validator") ...@@ -2,10 +2,12 @@ var Validator = require("validator")
, Utils = require("./utils") , Utils = require("./utils")
// Backwards compat for people using old validation function // Backwards compat for people using old validation function
// We cannot use .extend, since it coerces the first arg to string
Validator.notNull = function (val) {
return val !== null
}
// 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
Validator.extend('notNull', function(str) {
return str !== '';
})
Validator.extend('notEmpty', function(str) { Validator.extend('notEmpty', function(str) {
return !str.match(/^[\s\t\r\n]*$/); return !str.match(/^[\s\t\r\n]*$/);
...@@ -15,13 +17,62 @@ Validator.extend('len', function(str, min, max) { ...@@ -15,13 +17,62 @@ Validator.extend('len', function(str, min, max) {
return this.isLength(str, min, max) return this.isLength(str, min, max)
}) })
Validator.extend('isUrl', function(str) {
return this.isURL(str)
})
Validator.extend('isIPv6', function(str) {
return this.isIP(str, 6)
})
Validator.extend('isIPv4', function(str) {
return this.isIP(str, 4)
})
Validator.extend('notIn', function(str, values) {
return !this.isIn(str, values)
})
Validator.extend('regex', function(str, pattern, modifiers) {
str += '';
if (Object.prototype.toString.call(pattern).slice(8, -1) !== 'RegExp') {
pattern = new RegExp(pattern, modifiers);
}
return str.match(pattern);
})
Validator.extend('notRegex', function(str, pattern, modifiers) {
return !this.regex(str, pattern, modifiers);
})
Validator.extend('isDecimal', function(str) {
return str !== '' && str.match(/^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/);
})
Validator.extend('min', function(str, val) { Validator.extend('min', function(str, val) {
var number = parseFloat(str); var number = parseFloat(str);
return isNaN(number) || number >= val; return isNaN(number) || number >= val;
}) })
Validator.extend('isUrl', function(str) { Validator.extend('max', function(str, val) {
return this.isURL(str) var number = parseFloat(str);
return isNaN(number) || number <= val;
})
Validator.extend('not', function(str, pattern, modifiers) {
return this.notRegex(str, pattern, modifiers);
})
Validator.extend('contains', function(str, elem) {
return str.indexOf(elem) >= 0 && !!elem;
})
Validator.extend('notContains', function(str, elem) {
return !this.contains(str, elem);
})
Validator.extend('is', function(str, pattern, modifiers) {
return this.regex(str, pattern, modifiers);
}) })
var DaoValidator = module.exports = function(model, options) { var DaoValidator = module.exports = function(model, options) {
......
...@@ -951,7 +951,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -951,7 +951,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
code: { code: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: { validate: {
isLength: [3, 10] len: [3, 10]
} }
} }
}) })
...@@ -969,7 +969,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -969,7 +969,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(errors[0].errors.name[0]).to.equal('name cannot be null') expect(errors[0].errors.name[0]).to.equal('name cannot be null')
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]).to.equal('Validation isLength failed: code') expect(errors[1].errors.code[0]).to.equal('Validation len failed: code')
done() done()
}) })
}) })
...@@ -986,7 +986,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -986,7 +986,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
code: { code: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: { validate: {
isLength: [3, 10] len: [3, 10]
} }
} }
}) })
......
...@@ -69,7 +69,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -69,7 +69,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
} }
, notNull : { , notNull : {
fail: null, fail: null,
pass: 0 pass: ''
} }
, isNull : { , isNull : {
fail: 0, fail: 0,
...@@ -104,6 +104,12 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -104,6 +104,12 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
fail: "a", fail: "a",
pass: "0" pass: "0"
} }
, isLength: {
spec: { args: [2,4] },
fail: ["1", "12345"],
pass: ["12", "123", "1234"],
raw: true
}
, len: { , len: {
spec: { args: [2,4] }, spec: { args: [2,4] },
fail: ["1", "12345"], fail: ["1", "12345"],
...@@ -128,12 +134,12 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -128,12 +134,12 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
, isAfter: { , isAfter: {
spec: { args: "2011-11-05" }, spec: { args: "2011-11-05" },
fail: "2011-11-04", fail: "2011-11-04",
pass: "2011-11-05" pass: "2011-11-06"
} }
, isBefore: { , isBefore: {
spec: { args: "2011-11-05" }, spec: { args: "2011-11-05" },
fail: "2011-11-06", fail: "2011-11-06",
pass: "2011-11-05" pass: "2011-11-04"
} }
, isIn: { , isIn: {
spec: { args: "abcdefghijk" }, spec: { args: "abcdefghijk" },
...@@ -150,37 +156,20 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -150,37 +156,20 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
fail: "24", fail: "24",
pass: "23" pass: "23"
} }
, max$: {
spec: 23,
fail: "24",
pass: "23"
}
, min: { , min: {
spec: { args: 23 }, spec: { args: 23 },
fail: "22", fail: "22",
pass: "23" pass: "23"
} }
, min$: {
spec: 23,
fail: "22",
pass: "23"
}
, isArray: {
fail: 22,
pass: [22]
}
, isCreditCard: { , isCreditCard: {
fail: "401288888888188f", fail: "401288888888188f",
pass: "4012888888881881" pass: "4012888888881881"
} }
} }
for (var validator in checks) { var checkValidator = function (validator, validatorDetails) {
if (checks.hasOwnProperty(validator)) {
validator = validator.replace(/\$$/, '') validator = validator.replace(/\$$/, '')
var validatorDetails = checks[validator]
if (!validatorDetails.hasOwnProperty("raw")) { if (!validatorDetails.hasOwnProperty("raw")) {
validatorDetails.fail = [ validatorDetails.fail ] validatorDetails.fail = [ validatorDetails.fail ]
validatorDetails.pass = [ validatorDetails.pass ] validatorDetails.pass = [ validatorDetails.pass ]
...@@ -251,6 +240,11 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -251,6 +240,11 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
} }
} }
for (var validator in checks) {
if (checks.hasOwnProperty(validator)) {
checkValidator(validator, checks[validator])
}
} }
describe('#update', function() { describe('#update', function() {
...@@ -297,7 +291,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -297,7 +291,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
Model.sync({ force: true }).success(function() { Model.sync({ force: true }).success(function() {
Model.create({name: 'World'}).success(function(model) { Model.create({name: 'World'}).success(function(model) {
model.updateAttributes({name: ''}).error(function(err) { model.updateAttributes({name: ''}).error(function(err) {
expect(err).to.deep.equal({ name: [ 'Validation notNull failed: name', 'Validation notEmpty failed: name' ] }) expect(err).to.deep.equal({ name: [ 'Validation notEmpty failed: name' ] })
done() done()
}) })
}) })
...@@ -318,7 +312,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -318,7 +312,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
Model.sync({ force: true }).success(function() { Model.sync({ force: true }).success(function() {
Model.create({name: 'World'}).success(function(model) { Model.create({name: 'World'}).success(function(model) {
Model.update({name: ''}, {id: 1}).error(function(err) { Model.update({name: ''}, {id: 1}).error(function(err) {
expect(err).to.deep.equal({ name: [ 'Validation notNull failed: name', 'Validation notEmpty failed: name' ] }) expect(err).to.deep.equal({ name: [ 'Validation notEmpty failed: name' ] })
done() done()
}) })
}) })
......
...@@ -23,7 +23,7 @@ describe(Support.getTestDialectTeaser("Promise"), function () { ...@@ -23,7 +23,7 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
validateCustom: { validateCustom: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true, allowNull: true,
validate: {isLength: {msg: 'Length failed.', args: [1, 20]}} validate: {len: {msg: 'Length failed.', args: [1, 20]}}
}, },
dateAllowNullTrue: { dateAllowNullTrue: {
......
...@@ -37,7 +37,7 @@ describe(Support.getTestDialectTeaser("Executable"), function() { ...@@ -37,7 +37,7 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
flags.forEach(function(flag) { flags.forEach(function(flag) {
describe(flag, function() { describe(flag, function() {
it("prints the help", function(done) { it("prints the help", function(done) {
exec("node bin/sequelize " + flag, function(err, stdout, stderr) { exec("bin/sequelize " + flag, function(err, stdout, stderr) {
expect(version).to.not.be.empty expect(version).to.not.be.empty
expect(stdout).to.include(version) expect(stdout).to.include(version)
done() done()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!