Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom validation not documented completely #8905

Closed
dbettini opened this issue Jan 16, 2018 · 3 comments
Closed

Custom validation not documented completely #8905

dbettini opened this issue Jan 16, 2018 · 3 comments

Comments

@dbettini
Copy link

dbettini commented Jan 16, 2018

What are you doing?

Extending Sequelize.Validator with a custom validator for an array of urls, according to this, by just throwing an error if the format is wrong. The reason I used that is because I could not find Validator.extend documented anywhere.

function validateUrlArray(urls) {
  const urlArray = urls.split(',');

  if (urlArray.find((url) => !Validator.isUrl(url))) {
    throw new Error('Array contains invalid urls!');
  }
}

Validator.extend('isUrlArray', validateUrlArray);

What do you expect to happen?

The validation passes for an array of valid urls, otherwise it throws.

What is actually happening?

The validation throws correctly for invalid array (throws Array contains invalid urls! error). However, for an array of valid urls, it throws Validation isUrlArray on images failed.
The reason this happens is this line. If the validator function doesn't return a truthy value, the generic error with the validatorType and fieldis thrown.

It seems that extending Validator adds a new built in validator, instead of a custom one. However, built in validators seem to return falsy values instead of throwing (like custom ones do). And since Validator.extend isn't documented anywhere, nor is writing new built in validators, this creates a problem. Basically, the way I fixed this was just making my validator return true or false, but I think the docs should be updated with this use case or by making Validator.extend add a new custom validator.

Dialect: postgres
Database version: 10
Sequelize version: 4.28.1
Tested with latest release: No, but the mentioned line of code is unchanged since this version

@sushantdhiman
Copy link
Contributor

Sequelize.Validator is just a copy of https://github.com/chriso/validator.js, if you your validator to throw custom error message just define new validator extension and use that in a field's validate method

Or simply

{
    attribute: {
         validate: {
              isURLArray() {
                  const urlArray = urls.split(',');

                  if (urlArray.find((url) => !Validator.isUrl(url))) {
                       throw new Error('Array contains invalid urls!');
                  }
              }
         }
    }
}

@sushantdhiman
Copy link
Contributor

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

@dbettini
Copy link
Author

dbettini commented Feb 9, 2018

I'm aware of that. You linked the same documentation that I linked in my post. Using a custom function in the validate property doesn't allow for the reuse of that validator. On the other hand, using Validator.extend puts it in the global validator, so you could reuse it just by using validate: { isUrlArray: true }, and that's what I tried to achieve. However, I did find this - it seems they decided to drop it from validator.js, so I guess it shouldn't be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants