My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Links

Though code will be presented in this project, it is really promoting a new paradigm in data validation. The way of thought says that databases do not provide a robust enough means of describing in specific terms the data that should be stored in it. Utilizing JSON stored in each column's COMMENT attribute (a typically unused attribute), we are able to define more complex validation and allow a single class to handle all data validation.

The JSON stored in the COMMENT must be minified (no spaces, short names) due to length restrictions on the COMMENT, but here is an un-minified example:

'{	"insert_helpers": {
		"functions": {
			"func1":{
				"name":"strtotime",
				"params":{
					"param1":"+20 years"
				}
			},
			"func2":{
				"name":"str_replace",
				"params":{
					"param1":"!!",
					"param2":"!",
					"param3":"@this",
				}
			}
		}
	},
	"validators": {
		"maxlength":"10",
		"minlength":"2",
		"patterns":{
			"pattern1":{
				"pattern":"[^0-9]",
				"example":"This is data without numbers"
			}
		}
	}
}'

In practice (and for the posted code samples), here is what a real table might look like (MYSQL)

CREATE TABLE `example_table` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `event_name` varchar(255) NOT NULL COMMENT '{"funcs":{"func": {"n":"str_replace", "params":{"p1":"arch","p2":"e how cool","p3":"@this"}}},"validators":{ "maxlength":"10", "minlength":"2", "pattern":"[0-9]+"}}',
  `city` varchar(255) NOT NULL COMMENT '{"funcs":{"func": {"n":"str_replace", "params":{"p1":"arch","p2":"e how cool","p3":"@this"}}},"validators":{ "maxlength":"14", "minlength":"2", "pattern":"[0-9]+"}}',
  `zip` varchar(10) NOT NULL,
  `date_start` datetime DEFAULT '2009-12-26 00:00:00',
  `date_end` datetime NOT NULL DEFAULT '2040-01-01 00:00:00' COMMENT '{"funcs":{"func":{"n":"date", "params":{"p1":"Y-m-d H:i:s","p2":{"func":{"n":"strtotime", "params":{"p1":"+2 years"}}}}}}}',
  `entered_via` enum('web','csv', 'email') NOT NULL DEFAULT 'web',
   PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

These examples are taking it a bit far in terms of validation (as they provide transforms on the incoming data), but are meant to show the power of this method, including the ability to handle functions as parameters to functions (such as mysql datetime formatting in the date_end field).

Powered by Google Project Hosting