bamboo
bamboo copied to clipboard
Validations
Here's what I was very generally thinking with validations:
Declare validations and defaults:
var is_email = function(email) {
// custom check that an email is valid
};
var Post = Model({
title: {
type: 'string',
required: true,
max_length: 40,
default: 'New Post'
},
author: {
name: 'string',
email: {
validate: is_email,
message: 'Please enter a valid email'
}
}
});
Errors:
var post = Post();
post.errors; // {}
post.title; // 'New Post'
post.title = '';
post.errors; // {'title': 'title required'}
post.set_default('title');
post.author.email = 'hi';
post.errors; // {'author': {'email': 'Please enter a valid email'}};
Error events could be emitted in the form of 'error property' or 'error object.property', and some kind of templating or form library could take those events and render them.
It's probably too much ambiguity without prefixing the validations with something or doing some other interface:
var Post = Model({
title: {
_type: 'string',
_required: true,
_max_length: 40,
_default: 'New Post'
},
author: {
name: 'string',
email: {
_validate: is_email,
_message: 'Please enter a valid email'
}
}
});
So I don't think that validation should happen after every item set. I also don't think that there should be an errors property that lists the validation errors. What i think would be more appropriate is to have a validate() method or similar which would possibly return a Validator object or some such that would contain any errors. This might be more flexible. Or maybe the .use(validator) would monkey patch a validate() method or whatnot. Imagine if some validation isn't sync? Maybe we don't care about that currently.
I don't think messages or things like max_length should be baked into the schema. Maybe plugins could provide additional things but we should keep core bamboo lean.
Here are some more things to inspire schema layouts and validation:
- http://mongoosejs.com/docs/guide.html
- https://github.com/modella/modella
I don't like _ prefixing everything.
How about the validator as its own independent lib that takes any object with properties (or a custom getter) and change events?
var post_validator = Validator(model).attr('thing', {validation: schema}).attr('other_thing', {other: validations});
post_validator.is_valid(); // bool for all props
post_validator.is_valid('thing'); // bool for just thing
model.thing = 'lol hi'; // emits validation event for post_validator such as "invalid thing"
Would bamboo still even need a schema?
http://www.reactiongifs.com/wp-content/gallery/dance-party/mr_t_dance.gif
Bamboo needs a schema because of the property change events. Unless you meant a specific schema for validation. Separating the validation object from the model is interesting but I think will make it harder for using before a save unless we add a way to prevent save. On Jan 17, 2014 1:54 PM, "Jay R. Bolton" [email protected] wrote:
How about the validator as its own independent lib that takes any object with properties (or a custom getter) and change events?
var post_validator = Validator(model).attr('thing', {validation: schema}).attr('other_thing', {other: validations}); post_validator.is_valid(); // bool for all props post_validator.is_valid('thing'); // bool for just thing model.thing = 'lol hi'; // emits validation event for post_validator such as "invalid thing"
Would bamboo still even need a schema?
http://www.reactiongifs.com/wp-content/gallery/dance-party/mr_t_dance.gif
— Reply to this email directly or view it on GitHubhttps://github.com/defunctzombie/bamboo/issues/2#issuecomment-32635694 .