how to set Sequelize defaultValue when throwing error

Viewed 17

I'm building a videogame database and I'm using the URL validator for the image, but I'm trying to set a default value when the providing link is not an URL. And I think the "defaultValue" property only works when you don't provide a value at all. So if I insert a link that is not an URL, it'll just throw an error instead of setting the default value.

  sequelize.define("videogame", {
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    released: {
      type: DataTypes.DATEONLY,
    },
    rating: {
      type: DataTypes.FLOAT,
    },
    platforms: {
      type: DataTypes.ARRAY(DataTypes.STRING),
    },
    image: {
      type: DataTypes.TEXT,
      defaultValue: "https://t3.ftcdn.net/jpg/02/80/94/76/360_F_280947621_U7mDimuN41KHdMfvt3aIqd0OgXh7RAzm.jpg",
      validate: {
        isUrl: true,
      },
    },
  });

So I'm just wondering if there's a quick and easy way to set a default value when the link is not an URL... or at least just any way to solve this.

0 Answers
Related