lukejagodzinski / meteor-astronomy-validators

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

How to iterate and identify validator for field? #4

Open rclai opened 9 years ago

rclai commented 9 years ago

I'm trying to write a module to convert an Astronomy's schema into a Simple Schema (because eventually I will migrate to Astronomy), and I need to figure out what validators are used, but because they're all a bunch of functions, it doesn't seem possible to do so.

rclai commented 9 years ago

Is it possible for the validator returned here to be a class with the validation options exposed somehow?

lukejagodzinski commented 9 years ago

You are right. Right now it's not possible. However functions are also objects in JavaScript, so I can add to each validation function the name property.

At the beginning I also wanted to make each validator an object with the validate method. But we had a fierce discussion about that. We came to conclusion that validators should be functions. And I don't want to change that.

But I will add name property our some other way of taking its type. In fact I wanted to make refactoring, so maybe it's the time :-)

rclai commented 9 years ago

Is it possible to add more than just the name? Like, with the options and even nested properties?

For example:

Validators.or([
  Validators.minLength(5),
  Validators.equal('test')
])
// The custom (static) property of the function returned will give you:
{
  type: 'or',
  children: [{
    type: 'min',
    arguments: [5]
  }, {
    type: 'equal',
    arguments: ['test']
  }]
}
lukejagodzinski commented 9 years ago

Yes I think so. I will rethink how validators are constructed and come tomorrow with some solution for that. So stay tuned :)

lukejagodzinski commented 9 years ago

@rclai ok I've made a fix we've been talking about. Now when you call:

var validators = Post.getValidators();
var validator = validators.firstName[0];

You will receive object with list of validators where each validator has following properties:

validator.options; // options passed to validator
validator.message; // validator error message passed by the user
validator.definition; // definition of the validator
validator.definition._events; // `validationerror` events
validator.definition.name; // name of the validator
validator.definition.validate; // validate function

I hope, now you will be able to write your package :)

rclai commented 9 years ago

Cool thanks.

raddops commented 8 years ago

Thanks for the great work @jagi and @rclai
Any ETA on the simple-schema conversion package?

lukejagodzinski commented 8 years ago

It would be nice if someone would write such conversion package. Right now I'm busy with other stuff.

rclai commented 8 years ago

I think I may be able to work on this. How should I name the package?

lukejagodzinski commented 8 years ago

rclai:astronomy-to-simple-schema :-) or rclai:astronomy-to-simple-schema-converter long but descriptive :-)

raddops commented 8 years ago

I am a newbie at Web development, so I am not sure if I can provide any valuable help with development. Regarding the name, rclai:astronomy-autoform-schema or something similar sounds more appropriate. Triggers the idea that this package can be used to drive Autoform until native astro forms are available. Will boost popularity, IMO.

rclai commented 8 years ago

I just realized that it could just be named astronomy-simple-schema. What do you guys think?

rclai commented 8 years ago

@jagi, right now I'm using .getFields() to grab all the fields, but in order to see what kind of field constructor they're using, I've been using field.constructor.name. Is that the best way to see which field type constructor they're using?

rclai commented 8 years ago

Hey guys, here is a work in progress that you can try out at the moment.

There's a bit of hurdles because you can do so much more with Astronomy lol, so there are some limitations at the moment, at least in my brain right now, which needs sleep now.

lukejagodzinski commented 8 years ago

@rclai Oh sorry, I don't know how I could miss your comment. Thanks for this package! I would like to here comments from other developers how good it is. I will add info to the documentation about this package.

To answer your question about field type. It would be easy using the newest version of underscore of lodash, but right now you can do it in the following way:

var field = User.getField('address');

var type;
_.some(Astro.fields, function(fieldConstructor, fieldType) {
  console.log(fieldType);
  if (field instanceof fieldConstructor) {
    type = fieldType;
    return true;
  }
});

Your solution will also work but not in all old browsers, because the function's name property is the new thing. Maybe I will just add type name to the field.

lukejagodzinski commented 8 years ago

I will change it to

var field = User.getField('address');
field.type;

In the next version.