balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.81k stars 1.95k forks source link

Sails does not convert empty string to null when performing validation on models #4504

Open dev2games opened 6 years ago

dev2games commented 6 years ago

Sails version: 1.1.0-2 Node version: 9.2.1 NPM version: 6.4.0 DB adapter name: N/A DB adapter version: N/A Operating system: Ubuntu 16.04


If an empty string which initially should be a number gets passed through an action, it never converts into null.

So for example.

Someone fills out a form, in the form one of the options is "age" (which can be left empty), if they enter their age and then decide to delete it, it now gets sent as an empty string ""

Once sent to an action, the sails framework will decide to reject the request as it sees a string and not number. Regardless of whether or not allowNull = true

A workaround to this, has been to create a function that checks if a certain parameter is empty string, and then auto convert it to null.

function setNullTypes(allowedNullTypes, argins) {
  for (var i = 0; i < allowedNullTypes.length; i++) {
    if (argins[allowedNullTypes[i]] === '') {
      argins[allowedNullTypes[i]] = null;
    }
  }
  return argins;
}

I feel this is a really bad way of doing it, and it adds a ton of unnecessary bloat when the framework could easily detect itself whether or not a number is an empty string and convert that itself. Otherwise the allowNull is essentially useless for numbers, unless you convert in every form.

sailsbot commented 6 years ago

@dev2games Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

streleck commented 5 years ago

@dev2games Hi, I'm looking into this bug, but I haven't been able to get it into a state where a number field is holding an empty string. Can you give me a little more of a description of how you got that to happen?