lukejagodzinski / meteor-astronomy-validators

https://atmospherejs.com/jagi/astronomy-validators
MIT License
11 stars 13 forks source link

Make default error messages editable #1

Closed vladshcherbin closed 9 years ago

vladshcherbin commented 9 years ago

The default error messages have to be editable with a simple object / array, containing the messages, so that we can write smth like:

Astronomy.Validator.messages({
  required: 'Dude, the field is required!'
});

That way we can define our own messages in different language or just make them what we want.

It is not possible now, right? Or did I miss something?

lukejagodzinski commented 9 years ago

You shouldn't define error messages globally. As many validators as many possible error messages. You can always write your messages container or constructor. However it could be nice module that it's not integral part of validators. I will add it to my TODO list.

We could go even further. We could pass functions instead string messages and thanks to that we could compose detailed error messages in many languages using MessageFormat package.

vladshcherbin commented 9 years ago

@jagi Can't agree with you here. We absolutely need global validation messages because now we have english default messages. If we need e.g. German ones - we need to specify them directly everywhere. Not flexible.

Instead, we can define the default messages globally and use the default ones easily. Just make an object with default messages and take them from there. If there is an object with custom messages - grab them from there. This way, we can replace the messages easily.

The fieldName can be added like so:

required: "The :attribute field is required.",
date: "The :attribute is not a valid date."

I also think this should be in core validation package. And smth complex with changing message according to currently selected language, used in multilingual website, can be a separate package.

lukejagodzinski commented 9 years ago

Ok, maybe it's good idea to give developer ability to hook into the process of creating error message. It would fit nicely into the modularity of Meteor Astronomy package.

I have to figure out how to fit it nicely into current API. Maybe I will need to change some namespacing. Right now there are following objects/functions:

If I would have to give a developer some function to hook into the process of composing an error message I would have to do it in the Astronomy.Validators object and move all the validators from to global Validators object. I would also use events system to hook into the process. It would look like follows:

Let's discuss now how onMessage event could work. It needs to now few things:

Having that we could create event handler like this:

Astronomy.Validators.on('message', function(data) {
  var messages = {
    required: 'The ' + data.field + ' field is required',
    date: 'The ' + data.field + ' field is not valid date'
  };

  return messages[data.validator];
});

I think such approach would give huge flexibility.

vladshcherbin commented 9 years ago

@jagi yep, something like this would be nice

lukejagodzinski commented 9 years ago

I was thinking about that and came to conclusion that we should give developer even more flexibility. Error messages during generation process will pass through few steps.

Thanks to that a developer will be able to hook into the process of the message generation on three different stages.

Now I'm working on the new events system that will allow for easier events implementation. Stay tuned :-)

lukejagodzinski commented 9 years ago

Ok I've made an update about which you can read here: https://github.com/jagi/meteor-astronomy-validators#creating-error-message

Ability to hook into generating error message per schema. I will also add global hook (for all models/schemas) but it will need the whole system of global events on which I will be working soon.

For now I'm closing this issue. Thanks for help. With every suggestion Astronomy becomes more flexible.