Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit b0037d67
authored
Jul 09, 2015
by
Gustav Pursche
Committed by
Jan Aagaard Meier
Jul 13, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move validator.extend to it's own file
1 parent
1b41977d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
90 deletions
lib/instance-validator.js
lib/utils/validator-extras.js
lib/instance-validator.js
View file @
b0037d6
'use strict'
;
'use strict'
;
var
Validator
=
require
(
'validator'
)
var
validator
=
require
(
'./utils/validator-extras'
).
validator
,
extendModelValidations
=
require
(
'./utils/validator-extras'
).
extendModelValidations
,
Utils
=
require
(
'./utils'
)
,
Utils
=
require
(
'./utils'
)
,
sequelizeError
=
require
(
'./errors'
)
,
sequelizeError
=
require
(
'./errors'
)
,
Promise
=
require
(
'./promise'
)
,
Promise
=
require
(
'./promise'
)
,
DataTypes
=
require
(
'./data-types'
)
,
DataTypes
=
require
(
'./data-types'
)
,
_
=
require
(
'lodash'
);
,
_
=
require
(
'lodash'
);
function
noop
()
{}
// Deprecate this.
Validator
.
notNull
=
function
()
{
throw
new
Error
(
'Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"'
);
};
// https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js
Validator
.
extend
(
'notEmpty'
,
function
(
str
)
{
return
!
str
.
match
(
/^
[\s\t\r\n]
*$/
);
});
Validator
.
extend
(
'len'
,
function
(
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
)
{
var
number
=
parseFloat
(
str
);
return
isNaN
(
number
)
||
number
>=
val
;
});
Validator
.
extend
(
'max'
,
function
(
str
,
val
)
{
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
);
});
function
extendModelValidations
(
modelInstance
)
{
Validator
.
extend
(
'isImmutable'
,
function
(
str
,
param
,
field
)
{
return
(
modelInstance
.
isNewRecord
||
modelInstance
.
dataValues
[
field
]
===
modelInstance
.
_previousDataValues
[
field
]);
});
}
/**
/**
* The Main Instance Validator.
* The Main Instance Validator.
*
*
...
@@ -111,10 +30,10 @@ var InstanceValidator = module.exports = function(modelInstance, options) {
...
@@ -111,10 +30,10 @@ var InstanceValidator = module.exports = function(modelInstance, options) {
this
.
modelInstance
=
modelInstance
;
this
.
modelInstance
=
modelInstance
;
/**
/**
* Exposes a reference to validator.js. This allows you to add custom validations using `
V
alidator.extend`
* Exposes a reference to validator.js. This allows you to add custom validations using `
v
alidator.extend`
* @name
V
alidator
* @name
v
alidator
*/
*/
this
.
Validator
=
V
alidator
;
this
.
validator
=
v
alidator
;
/**
/**
* All errors will be stored here from the validations.
* All errors will be stored here from the validations.
...
@@ -223,7 +142,7 @@ InstanceValidator.prototype._customValidators = function() {
...
@@ -223,7 +142,7 @@ InstanceValidator.prototype._customValidators = function() {
var
valprom
=
self
.
_invokeCustomValidator
(
validator
,
validatorType
)
var
valprom
=
self
.
_invokeCustomValidator
(
validator
,
validatorType
)
// errors are handled in settling, stub this
// errors are handled in settling, stub this
.
catch
(
noop
);
.
catch
(
function
()
{}
);
validators
.
push
(
valprom
);
validators
.
push
(
valprom
);
});
});
...
@@ -270,7 +189,7 @@ InstanceValidator.prototype._builtinAttrValidate = function(value, field) {
...
@@ -270,7 +189,7 @@ InstanceValidator.prototype._builtinAttrValidate = function(value, field) {
var
validatorPromise
=
self
.
_invokeBuiltinValidator
(
value
,
test
,
validatorType
,
field
);
var
validatorPromise
=
self
.
_invokeBuiltinValidator
(
value
,
test
,
validatorType
,
field
);
// errors are handled in settling, stub this
// errors are handled in settling, stub this
validatorPromise
.
catch
(
noop
);
validatorPromise
.
catch
(
function
()
{}
);
validators
.
push
(
validatorPromise
);
validators
.
push
(
validatorPromise
);
});
});
...
@@ -329,7 +248,7 @@ InstanceValidator.prototype._invokeCustomValidator = Promise.method(function(val
...
@@ -329,7 +248,7 @@ InstanceValidator.prototype._invokeCustomValidator = Promise.method(function(val
*/
*/
InstanceValidator
.
prototype
.
_invokeBuiltinValidator
=
Promise
.
method
(
function
(
value
,
test
,
validatorType
,
field
)
{
InstanceValidator
.
prototype
.
_invokeBuiltinValidator
=
Promise
.
method
(
function
(
value
,
test
,
validatorType
,
field
)
{
// check if Validator knows that kind of validation test
// check if Validator knows that kind of validation test
if
(
typeof
V
alidator
[
validatorType
]
!==
'function'
)
{
if
(
typeof
v
alidator
[
validatorType
]
!==
'function'
)
{
throw
new
Error
(
'Invalid validator function: '
+
validatorType
);
throw
new
Error
(
'Invalid validator function: '
+
validatorType
);
}
}
...
@@ -348,7 +267,7 @@ InstanceValidator.prototype._invokeBuiltinValidator = Promise.method(function(va
...
@@ -348,7 +267,7 @@ InstanceValidator.prototype._invokeBuiltinValidator = Promise.method(function(va
validatorArgs
=
validatorArgs
.
slice
(
0
);
validatorArgs
=
validatorArgs
.
slice
(
0
);
}
}
if
(
!
Validator
[
validatorType
].
apply
(
V
alidator
,
[
value
].
concat
(
validatorArgs
)))
{
if
(
!
validator
[
validatorType
].
apply
(
v
alidator
,
[
value
].
concat
(
validatorArgs
)))
{
// extract the error msg
// extract the error msg
throw
test
.
msg
||
'Validation '
+
validatorType
+
' failed'
;
throw
test
.
msg
||
'Validation '
+
validatorType
+
' failed'
;
}
}
...
...
lib/utils/validator-extras.js
0 → 100644
View file @
b0037d6
'use strict'
;
var
validator
=
require
(
'validator'
)
,
_
=
require
(
'lodash'
);
var
extensions
=
{
notEmpty
:
function
(
str
)
{
return
!
str
.
match
(
/^
[\s\t\r\n]
*$/
);
},
len
:
function
(
str
,
min
,
max
)
{
return
this
.
isLength
(
str
,
min
,
max
);
},
isUrl
:
function
(
str
)
{
return
this
.
isURL
(
str
);
},
isIPv6
:
function
(
str
)
{
return
this
.
isIP
(
str
,
6
);
},
isIPv4
:
function
(
str
)
{
return
this
.
isIP
(
str
,
4
);
},
notIn
:
function
(
str
,
values
)
{
return
!
this
.
isIn
(
str
,
values
);
},
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
);
},
notRegex
:
function
(
str
,
pattern
,
modifiers
)
{
return
!
this
.
regex
(
str
,
pattern
,
modifiers
);
},
isDecimal
:
function
(
str
)
{
return
str
!==
''
&&
str
.
match
(
/^
(?:
-
?(?:[
0-9
]
+
))?(?:\.[
0-9
]
*
)?(?:[
eE
][\+\-]?(?:[
0-9
]
+
))?
$/
);
},
min
:
function
(
str
,
val
)
{
var
number
=
parseFloat
(
str
);
return
isNaN
(
number
)
||
number
>=
val
;
},
max
:
function
(
str
,
val
)
{
var
number
=
parseFloat
(
str
);
return
isNaN
(
number
)
||
number
<=
val
;
},
not
:
function
(
str
,
pattern
,
modifiers
)
{
return
this
.
notRegex
(
str
,
pattern
,
modifiers
);
},
contains
:
function
(
str
,
elem
)
{
return
str
.
indexOf
(
elem
)
>=
0
&&
!!
elem
;
},
notContains
:
function
(
str
,
elem
)
{
return
!
this
.
contains
(
str
,
elem
);
},
is
:
function
(
str
,
pattern
,
modifiers
)
{
return
this
.
regex
(
str
,
pattern
,
modifiers
);
},
};
var
extendModelValidations
=
function
(
modelInstance
)
{
var
extensions
=
{
isImmutable
:
function
(
str
,
param
,
field
)
{
return
(
modelInstance
.
isNewRecord
||
modelInstance
.
dataValues
[
field
]
===
modelInstance
.
_previousDataValues
[
field
]);
},
};
_
.
forEach
(
extensions
,
function
(
extend
,
key
)
{
validator
.
extend
(
key
,
extend
);
});
};
// Deprecate this.
validator
.
notNull
=
function
()
{
throw
new
Error
(
'Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"'
);
};
// https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js
_
.
forEach
(
extensions
,
function
(
extend
,
key
)
{
validator
.
extend
(
key
,
extend
);
});
module
.
exports
=
{
extensions
:
extensions
,
extendModelValidations
:
extendModelValidations
,
validator
:
validator
,
};
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment