partial.js logo
Latest version
v1.3.1 (RC2)

How does validation work?


Framework has simple validation logic for validating of models. You can use this logic for server side and
for the client side.


Validation example


Framework validation

Definition of validation

Delegate you can define in some definition file. How do definitions work?.

framework.onValidation = function(name, value) {
    switch (name) {
        case 'firstname':
        case 'lastname':
            return value.length > 0;
        case 'isTerms':
            return value === true || value == '1';
        case 'age':
            var age = value.toString().parseInt();
            return age > 10 && age < 90;
        case 'email':
            return value.isEmail();
    }
}

Validate some model


function someActionInController() {

	var self = this;
	var model = self.post;

	var error = self.validate(model, ['firstname', 'lastname']);

	if (error.hasError()) {

		// response error
		// Example: [{ "name": "firstname", "error": "first name is required" }]
		self.json(error);
		return;

	}

	// insert model into the DB
	self.database('users').insert(model);

	// response success
	self.json({ r: true, message: 'success' });
}

Benefits


This validation logic has several benefits: