{"action":"opened","issue":{"url":"https://api.github.com/repos/couchbaselabs/node-ottoman/issues/139","repository_url":"https://api.github.com/repos/couchbaselabs/node-ottoman","labels_url":"https://api.github.com/repos/couchbaselabs/node-ottoman/issues/139/labels{/name}","comments_url":"https://api.github.com/repos/couchbaselabs/node-ottoman/issues/139/comments","events_url":"https://api.github.com/repos/couchbaselabs/node-ottoman/issues/139/events","html_url":"https://github.com/couchbaselabs/node-ottoman/issues/139","id":184647049,"number":139,"title":"Ability to register validation functions and/or custom types","user":{"login":"tcheever","id":1217061,"avatar_url":"https://avatars.githubusercontent.com/u/1217061?v=3","gravatar_id":"","url":"https://api.github.com/users/tcheever","html_url":"https://github.com/tcheever","followers_url":"https://api.github.com/users/tcheever/followers","following_url":"https://api.github.com/users/tcheever/following{/other_user}","gists_url":"https://api.github.com/users/tcheever/gists{/gist_id}","starred_url":"https://api.github.com/users/tcheever/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tcheever/subscriptions","organizations_url":"https://api.github.com/users/tcheever/orgs","repos_url":"https://api.github.com/users/tcheever/repos","events_url":"https://api.github.com/users/tcheever/events{/privacy}","received_events_url":"https://api.github.com/users/tcheever/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-10-22T18:46:39Z","updated_at":"2016-10-22T18:46:39Z","closed_at":null,"body":"[Enhancement] It would be nice of Ottoman allowed you to register validation functions and/or custom types which could be referenced by name in the schemas.\r\n\r\n**Validator example**\r\n\r\n```\r\nottoman.validators({\r\n  email (value) {\r\n    let regex = /^[-a-z0-9~!$%^&*_=+}{'?]+(\\.[-a-z0-9~!$%^&*_=+}{'?]+)*@([a-z0-9_][-a-z0-9_]*(\\.[-a-z0-9_]+)*\\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,5})?$/i\r\n    if (!value || !regex.test(value)) {\r\n      throw new Error('Email address is invalid.')\r\n    }\r\n  },\r\n  phone (value) {\r\n    let regex = /^\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/\r\n    if (!value || !regex.test(value)) {\r\n      throw new Error('Phone number is invalid.')\r\n    }\r\n  }\r\n});\r\n\r\nUser = ottoman.model('User', {\r\n  ...\r\n  email: {\r\n    type: 'string',\r\n    validator: 'email'\r\n    // Or ottoman.validators.email if you don't want to support function AND string\r\n  },\r\n  phone: {\r\n    type: 'string',\r\n    validator: 'phone'\r\n  }\r\n});\r\n```\r\n\r\n**Custom Type Example**\r\n\r\n```\r\nottoman.types({\r\n  email (value) {\r\n    let regex = /^[-a-z0-9~!$%^&*_=+}{'?]+(\\.[-a-z0-9~!$%^&*_=+}{'?]+)*@([a-z0-9_][-a-z0-9_]*(\\.[-a-z0-9_]+)*\\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,5})?$/i\r\n    if (!value || !regex.test(value)) {\r\n      throw new Error('Email address is invalid.')\r\n    }\r\n  },\r\n  phone (value) {\r\n    let regex = /^\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/\r\n    if (!value || !regex.test(value)) {\r\n      throw new Error('Phone number is invalid.')\r\n    }\r\n  }\r\n});\r\n\r\nUser = ottoman.model('User', {\r\n  ...\r\n  email: {\r\n    type: 'email'\r\n    // Perhaps it could also be accessible via ottoman.types.email or added to a\r\n    // validators object like ottoman.validators.email for other use cases.\r\n  },\r\n  phone: {\r\n    type: 'phone'\r\n  }\r\n});\r\n```\r\n\r\nThe approach of validators() or types() accepting the object makes it very easy for folks to make addon packages, like tv4-formats for tv4."}}
{"actor":{"display_login":"tcheever"}}