Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created March 9, 2011 17:31
Show Gist options
  • Save JamieMason/862601 to your computer and use it in GitHub Desktop.
Save JamieMason/862601 to your computer and use it in GitHub Desktop.
First attempt at proposal for the syntax for registration validators
// Constructor
function Validator (oParams, oProtected)
{
// optionally share private stuff between other validators
oProtected = oProtected || {};
// ...
}
// Instantiate
var registrar = new Validator ({
name: 'registrar',
fields: {
telephone : {
minlength : {
// required
isValid : function(){ return this.length > 5; },
failMsg : 'Telephone number must be a minimum of 6 characters',
// below are optional/overrides
passMsg : 'Thanks, Telephone number accepted',
required : true, // default is false
defaultValue : '123456', // default is null
// default format 'validator:<validator name>.<field name>.<requirement name>.<valid|invalid>
passEvent : 'validator:registrar.telephone.minlength.valid',
failEvent : 'validator:registrar.telephone.minlength.invalid'
},
validchars : {
isValid : function(){ return this.search(/[^0-9\-]/gi) === -1; },
failMsg : 'Telephone must only contain numbers or dashes'
},
someOtherTestCase : {
isValid : function(){ return /* Boolean */; },
failMsg : 'etc...'
}
},
someOtherField : {
someTestCase : {
isValid : function(){ return /* Boolean */; },
failMsg : 'etc...'
}
}
}
});
// get field value (returns value)
registrar.field('firstName');
// Set field value (returns boolean for valid or not, events fire in background)
registrar.field('firstName', 'Steve');
// Get all values (returns object)
registrar.field();
// Set multiple values (returns boolean for if ALL were valid or not, events fire in background)
registrar.field({
firstName: 'Steve',
surname: 'Melrose'
});
// Get array of Strings of names of unfinished fields
registrar.incomplete();
// Get array of Objects of invalid fields and their failing tests
registrar.invalid();
// Get array of Strings of names of required fields
registrar.required();
// Instantiation example with MessageBroker
var registrar = new Validator ({
name: 'registrar',
fields: {
telephone : {
validchars : {
isValid : function(){ return this.search(/[^0-9\-]/gi) === -1; },
failMsg : MessageBroker.E_WHATEVER_IT_IS
},
someOtherTestCase : {
isValid : function(){ return /* Boolean */; },
failMsg : MessageBroker.E_WHATEVER_IT_IS
}
},
someOtherField : {
someTestCase : {
isValid : function(){ return /* Boolean */; },
failMsg : MessageBroker.E_WHATEVER_IT_IS
}
}
}
});
@JamieMason
Copy link
Author

  • When all of a field's tests pass, validator:{validator name}.{field name}.valid will fire.
  • When any of a field's tests fail validator:{validator name}.{field name}.invalid will fire.
  • When any of a validator's field's tests fail, validator:{validator name}.invalid will fire.
  • When all of a validator's field's tests pass, validator:{validator name}.valid will fire.
  • When none of a validator's required field values are null or invalid AND no optional fields are invalid, validator:{validator name}.complete will fire.
  • When a previously complete validator no longer passes the above case, validator:{validator name}.incomplete will fire.

@JamieMason
Copy link
Author

To do...

  • How to define and handle asynchronous tests
  • How/if we need to handle compound fields such as date of birth here, or in the project's js
  • How to manage the utils is _isValidDate, _getStringLengthWithoutWhitespace etc.

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