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

Add note about custom errors #6

Open
davidpaulhunt opened this issue Sep 19, 2017 · 8 comments
Open

Add note about custom errors #6

davidpaulhunt opened this issue Sep 19, 2017 · 8 comments

Comments

@davidpaulhunt
Copy link
Contributor

Okay, so if you pass a filterWith rule, that function should return true|false.

However, you can throw a custom error in that function instead. It's just that the customer error needs to implement toJSON and return { field, message }.

@motss
Copy link

motss commented Oct 16, 2017

I propose that we should include a way to define custom errors when filterWith, like so:

const schema = {
  first_name: { type: 'string', required: true, error: new TypeError('first_name is not defined') },
};

Thoughts, anyone?

@davidpaulhunt
Copy link
Contributor Author

@motss oh, interesting!

So, internally, fields throw errors that are caught specifically by type, so it might take more effort to permit custom error types.

Could you get what you need if it was possible to define the error message? For example:

const schema = {
  first_name: { type: 'string', required: true, message: 'first_name is not defined' },
};

@motss
Copy link

motss commented Oct 16, 2017

@davidpaulhunt Missed out that message field. Will definitely try that out. whitelister is awesome btw. Simple yet powerful enough to get the job done and done it right! 👍

@davidpaulhunt
Copy link
Contributor Author

@motss thank you!

I haven't implemented message field yet. But I could do so if it would help your use case.

😄

@motss
Copy link

motss commented Oct 17, 2017

@davidpaulhunt I see. But it'd be nice if it can be function, like so:

// message: val => { ... },
const schema = {
  first_name: {
    type: 'string',
    required: true,
    message: val => `first_name (${val}) is invalid`,
  },
  age: {
    type: 'integer',
    required: true,
    min: 0,
    max: 999,
    message: (val) => {
      if (val < 0 || val > 999) return `Invalid age (${val}). Please specify an age in between 0 and 999`;
      return 'Please specify a valid age';
    },
  },
};

In this case, I can return the invalid value along with the error message.

@motss
Copy link

motss commented Nov 3, 2017

@davidpaulhunt Any update on this?

@davidpaulhunt
Copy link
Contributor Author

@motss your example of above can already be implemented with filterWith:

const schema = {
  age: {
    type: 'integer',
    required: true,
    min: 0,
    max: 999,
    filterWith: (val) => {
      if (val < 0 || val > 999) throw new Error(`Invalid age (${val}). Please specify an age in between 0 and 999`);
      if (someOtherCondition) {
        throw new Error('Please specify a valid age');
      }
      return true
    },
  },
};

So I don't think message would need to be a function; but it could still be an option to let you choose what the default error message is.

@motss
Copy link

motss commented Nov 3, 2017

@davidpaulhunt This totally fit my purpose. 👍

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

No branches or pull requests

2 participants