How to check string field in sequelize.js file?

How can I check the database field to only accept a row?

In my database, I have two fields:

  • description: Line
  • completed: Boolean

I want the description field to accept only a string value. I mean:

  • 'description': 'text' => database accept this request
  • 'descrition': true or false => the database denies this request
  • 'descrition': 123 => the database denies this request

Currently, the field descriptioncan take a boolean value, so there is a problem in my configuration.

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('todo', {
        description: {
            type: DataTypes.STRING,
            allowNull:false,
            validate: {
                len: [1, 250],
                isBoolean:false,
                isAlpha:true
            }
        },
        completed: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: false,
            validate:{
                isBoolean:true
            }
        }
    });
};
+4
source share
1 answer

regexp, , , true false:

return sequelize.define('todo', {
    description: {
        type: DataTypes.STRING,
        allowNull:false,
        validate: {
            is: ^((?!true|false|TRUE|FALSE).){1,255}$
        }
    },
    ...

http://docs.sequelizejs.com/manual/tutorial/models-definition.html#validations

0

Source: https://habr.com/ru/post/1683358/


All Articles