{"id":18539078,"url":"https://github.com/simov/mysql-validator","last_synced_at":"2025-07-15T23:47:02.700Z","repository":{"id":5355076,"uuid":"6541055","full_name":"simov/mysql-validator","owner":"simov","description":"MySql data type validation","archived":false,"fork":false,"pushed_at":"2014-04-12T10:32:56.000Z","size":252,"stargazers_count":5,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-19T10:42:15.608Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-11-05T08:05:17.000Z","updated_at":"2024-02-11T04:44:00.000Z","dependencies_parsed_at":"2022-09-04T20:13:11.217Z","dependency_job_id":null,"html_url":"https://github.com/simov/mysql-validator","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simov%2Fmysql-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simov%2Fmysql-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simov%2Fmysql-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simov%2Fmysql-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simov","download_url":"https://codeload.github.com/simov/mysql-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248078602,"owners_count":21044141,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T19:46:09.058Z","updated_at":"2025-04-09T17:38:07.004Z","avatar_url":"https://github.com/simov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"#MySql Validator\n\nValidates web forms input against mysql database.\n\n##Installation\n\n    $ npm install mysql-validator\n\n##API\n\n```js\nvar validator = require('mysql-validator');\n\nvar err = validator.check('doh winning!', 'varchar(45)');\nif (err) {\n  console.log(err.message);\n}\n```\n\nThe first parameter is the posted input data and the second is the mysql data type of the field in your database.\n\n##Obtaining data types\n\nThe best way to pass the corresponding data type for your input field is to query the database for it.\n\n```sql\ndescribe `table-name`;\n```\n\nAll you need to look for is the `Type` column. This is the string that the validator expects to see as a second parameter.\n\nThis will give you an idea of what object is constructed after the data type string have been parsed. This object is used internally by the validator.\n\n    $ mocha test/data-type.js\n\nYou can type in your data types manually without querying the database, just make sure you don't mess them up.\n\n##Express 3.x example\n\nSuppose you have a form like this.\n\n```html\n\u003cform method=\"post\" action=\"/save\"\u003e\n    \u003cinput type=\"text\" name=\"name\" /\u003e\n    \u003cinput type=\"text\" name=\"cache\" /\u003e\n    \u003cinput type=\"text\" name=\"date\" /\u003e\n    \u003cinput type=\"submit\" value=\"Save\" /\u003e\n\u003c/form\u003e\n```\n\nThen your router may look like this.\n\n```js\napp.post('/save', function (req, res) {\n  // we'll store all validation errors here\n  var errors = [];\n  // field-type mapping (this may be the result of 'describe table')\n  var types = {\n    name: 'varchar(10)',\n    cache: 'decimal(6,2) unsigned',\n    date: 'datetime'\n  }\n  // loop through the submitted fields and validate them\n  for (var key in req.body) {\n    var err = validator.check(req.body[key], types[key]);\n    // store the error's message and the field name\n    if (err) errors.push({ name: key, error: err.message });\n  }\n  if (errors.length) {\n    // notify the user about the errors\n    res.render('template', { err: errors, other: 'params...' });\n  } else {\n    // safely store the user's input into the database\n  }\n});\n```\n\n##Tests\n\nBefore you can run the tests you must create the test user and give him rights to the test database.\n\n```sql\ncreate user 'liolio'@'localhost' identified by 'karamba';\ngrant all on `mysql-validator`.* to 'liolio'@'localhost';\n```\n\nThen run this test.\n\n    $ mocha test/mysql.js\n\nIf it pass then you're good to go.\n\nThere are a various tests for each data type.\n\n    $ mocha test/index.js\n\nThe output is pretty verbose. The yellow column show what the test input is. The left column show what mysql store in it's database for this input. The right column show whether the validator think it should be valid or not.\n\n![](http://i.imgur.com/rKYxW.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimov%2Fmysql-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimov%2Fmysql-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimov%2Fmysql-validator/lists"}