Validator.js: Undocumented toString function

Created on 19 Feb 2019  路  8Comments  路  Source: validatorjs/validator.js

Hi there,

I just wanted to ask whether the toString function which is exported is something which we can rely on to convert values to a string before passing it to other methods exports from this library as it seems to be undocumented?

If this is simply a case of the docs being out of date, I am happy to make a PR to update the README!

馃嵖 discussion

All 8 comments

Try using assertString(str) in src/lib/utils/. It will throw an error if the passed in value isn't a string before it gets to your function.

OK I can use this for validating that the value is a string, thanks. However you didn't answer my question about toString and it's lack of documentation.

@cdimitroulas - this is just a utility used within the confines of this library, I wouldn't recommend the use of it outside, that's why it's not exposed within /lib; and also not documented.

As you can see, from the code, it is mainly relying on the already defined .toString method for whatever object is supplied: https://github.com/chriso/validator.js/blob/master/lib/util/toString.js#L11-L21

if (_typeof(input) === 'object' && input !== null) {
    if (typeof input.toString === 'function') {
      input = input.toString();
    } else {
      input = '[object Object]';
    }
  } else if (input === null || typeof input === 'undefined' || isNaN(input) && !input.length) {
    input = '';
  }

  return String(input);

@profnandaa I'm a little confused, you say that it's not exposed in /lib, however it is exported when you import the library as per normal like import validator from 'validator'. The toString utility is imported and re-exported in src/index.js inside the validator object.

The toString method is then available on validator.toString. If this utility is not supposed to be exposed then I guess it should be removed from the default export. If this is not a mistake then some documentation is needed.

I'm happy to make a PR either way, but I don't think this issue should be closed without one or the other.

Sorry, that must have been an oversight from my end, my bad :(

Sure, I agree with you. I'm of the opinion that we remove it from the list of exports. It will better remain as an internal utility function.

As for the PR, you we could start off by a deprecation warning, just in case someone has been using it in prod? What do you suggest?

cc. @chriso

Looking through the git history it looks like this utility was being exported since 3 years ago, so I think your idea of a deprecation warning is good. It is pretty likely that someone could be using toString in prod.

Going to wait for the @chriso 's opinion on this before spending time on a PR so I don't waste time doing the wrong thing.

Removed from exports in 0277eb00d245a3479af52adf7d927d4036895650. I will bump the major version since this is a breaking change.

Thanks @chriso.

Btw, it looks like you forgot to remove the key from the final exported object (this line).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rathboma picture rathboma  路  4Comments

spyshower picture spyshower  路  3Comments

galki picture galki  路  3Comments

AtomicBorg picture AtomicBorg  路  3Comments

jaxkodex picture jaxkodex  路  3Comments