sequelize: Validation of array input to array field failed for PostgreSQL
I’m using Node.js + PostgreSQL + Sequelize.js 2.0.2. I have the following schema definition for the phone field which could be array of strings, but only number is allowed:
var User = sequelize.define("User", {
....
....
/** Phone number
* Multiple numbers should be registered due to
*/
phone: {
type: Sequelize.ARRAY(Sequelize.STRING),
allowNull: false,
unique: true,
validate: {
isNumeric: true
}
},
....
....
});
The array of numeric phone numbers is not passed for validation.
Input: [ '9252522525', '9252525555' ]
Error:
{ [SequelizeValidationError: Validation error]
name: 'SequelizeValidationError',
message: 'Validation error',
errors:
[ { message: 'Validation isNumeric failed',
type: 'Validation error',
path: 'phone',
value: 'Validation isNumeric failed',
__raw: 'Validation isNumeric failed' } ] }
However, the single-value array input [ '9252522525' ]
is successful. Is it Sequelize’s bug or what am I wrong?
I changed the validator to is
, but no success.
validate: {
is: ["^[0-9]+$", "i"]
}
I got the following error.
{ [SequelizeValidationError: Validation error]
name: 'SequelizeValidationError',
message: 'Validation error',
errors:
[ { message: 'Validation is failed',
type: 'Validation error',
path: 'phone',
value: 'Validation is failed',
__raw: 'Validation is failed' } ] }
Expected: Applying the validator to each item of the array.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 18 (7 by maintainers)
How about a new syntax where the argument to
ARRAY
is another type/validation definition?For example, this defines a type that’s a list of 1 to 10 email addresses:
@janmeier I’m not sure why the single-value array
[ '9252522525' ]
is working. Along with the data typeSequelize.ARRAY(Sequelize.STRING)
, my opinion is that the validator could be enough to apply to each array element ofSequelize.STRING
because the array ofSequelize.ARRAY
itself can be validated usingArray.isArray()
and no validator is explicitly needed.What do you mean “a bit too magic if some validations apply to elements and some apply to the array”?