Sequelize does not validate my model

0

I'm new to trying to program and started with this framework called sequelize to make a connection to a database.

with sequelize-cli create the following model called users to modify it to add some validations and stay as follows:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Usuarios', {
      username: {
        type: Sequelize.STRING,
        primaryKey:true,
        allowNull:false,
        validate:{
          max:25
        }
      },
      primerNombre: {
        type: Sequelize.STRING,
        validate:{
          max:25
        }
      },
      segundoNombre: {
        type: Sequelize.STRING,
        validate:{
          max:25
        }
      },
      primerApellido: {
        type: Sequelize.STRING,
        validate:{
          max:25
        }
      },
      segundoapellido: {
        type: Sequelize.STRING,
        validate:{
          max:25
        }
      },
      direccion: {
        type: Sequelize.STRING,
        validate:{
          max:250
        }
      },
      telefono: {
        type: Sequelize.INTEGER,
        validate:{
          max:8
        }
      },
      password: {
        type: Sequelize.STRING,
        validate:{
          min:8
        },
        allowNull:false
      },
      admin:{
        type:Sequelize.BOOLEAN,
        allowNull: false,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    },
    {
      charset:'utf8'
    }
    );
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Usuarios');
  }
};

and in the express router I have the following code:

var express = require('express');
router = express.Router();
var model = require('../models/index');

router.post('/',function(req,res,next){
    console.log("entramos a este evento");
    model.Usuario.create({
        username:req.body.username,
        primerNombre: req.body.primerNombre,
        segundoNombre: req.body.segundoNombre,
        primerApellido: req.body.primerApellido,
        segundoapellido: req.body.segundoapellido,
        direccion: req.body.direccion,
        telefono: req.body.telefono,
        password: req.body.password
    }).then(usuario => res.status(201).json({
        error: false,
        data: usuario,
        message: 'New user is been created'
    }))
    .catch(error => res.json({
        error:true,
        data:[],
        error:error
    }))
});
module.exports = router;

at the time of saving a user at the address localhost: 3000 / users / works great the problem that I see is that it does not validate me that the password can not be null and it does not validate its length I enclose a capture of the request that Make my client rest

note: neither validates that the password is less than 8 characters, I can enter passwords of any length.

    
asked by Cristian Aragon 24.09.2018 в 23:02
source

0 answers