{"action":"created","issue":{"url":"https://api.github.com/repos/jagi/meteor-astronomy/issues/21","labels_url":"https://api.github.com/repos/jagi/meteor-astronomy/issues/21/labels{/name}","comments_url":"https://api.github.com/repos/jagi/meteor-astronomy/issues/21/comments","events_url":"https://api.github.com/repos/jagi/meteor-astronomy/issues/21/events","html_url":"https://github.com/jagi/meteor-astronomy/issues/21","id":83237299,"number":21,"title":"type validators and \"required\"","user":{"login":"dovrosenberg","id":4704591,"avatar_url":"https://avatars.githubusercontent.com/u/4704591?v=3","gravatar_id":"","url":"https://api.github.com/users/dovrosenberg","html_url":"https://github.com/dovrosenberg","followers_url":"https://api.github.com/users/dovrosenberg/followers","following_url":"https://api.github.com/users/dovrosenberg/following{/other_user}","gists_url":"https://api.github.com/users/dovrosenberg/gists{/gist_id}","starred_url":"https://api.github.com/users/dovrosenberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dovrosenberg/subscriptions","organizations_url":"https://api.github.com/users/dovrosenberg/orgs","repos_url":"https://api.github.com/users/dovrosenberg/repos","events_url":"https://api.github.com/users/dovrosenberg/events{/privacy}","received_events_url":"https://api.github.com/users/dovrosenberg/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/jagi/meteor-astronomy/labels/discussion","name":"discussion","color":"0000c8"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-06-01T00:25:00Z","updated_at":"2015-06-02T08:52:37Z","closed_at":null,"body":"Sorry for the flood of new issues... hopefully they're helpful.\r\n\r\nThe type validators (date, email, etc.) all appear to return false (invalid) if the field is blank.  To me, it's more intuitive that they'd return true in that case.  If I want to fail a blank field, I'd add the \"required\" validator.  But if it's not marked as \"required\", it should not be required. :)\r\n\r\nNot sure what others think, though."},"comment":{"url":"https://api.github.com/repos/jagi/meteor-astronomy/issues/comments/107872084","html_url":"https://github.com/jagi/meteor-astronomy/issues/21#issuecomment-107872084","issue_url":"https://api.github.com/repos/jagi/meteor-astronomy/issues/21","id":107872084,"user":{"login":"jagi","id":1051229,"avatar_url":"https://avatars.githubusercontent.com/u/1051229?v=3","gravatar_id":"","url":"https://api.github.com/users/jagi","html_url":"https://github.com/jagi","followers_url":"https://api.github.com/users/jagi/followers","following_url":"https://api.github.com/users/jagi/following{/other_user}","gists_url":"https://api.github.com/users/jagi/gists{/gist_id}","starred_url":"https://api.github.com/users/jagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jagi/subscriptions","organizations_url":"https://api.github.com/users/jagi/orgs","repos_url":"https://api.github.com/users/jagi/repos","events_url":"https://api.github.com/users/jagi/events{/privacy}","received_events_url":"https://api.github.com/users/jagi/received_events","type":"User","site_admin":false},"created_at":"2015-06-02T08:52:37Z","updated_at":"2015-06-02T08:52:37Z","body":"But it's very important to solve this problem at this stage :). I've solved this problem in the newest version of Astronomy core and validators. It's not published yet because I want to discuss it here.\r\n\r\nI solved it in following way (referring to your points):\r\n\r\n**0) Field type is only for casting values. It always was that way.**\r\nMaybe I haven't described it good enough in the documentation. If field has type:\r\n\r\n```js\r\nfield: {\r\n  firstName: 'string'\r\n}\r\n``` \r\nthen such assignment will cast `123` to string and we will get `\"123\"`\r\n\r\n```js\r\nvar user = new User();\r\nuser.set('firstName', 123);\r\nuser.firstName; // \"123\" not 123 number\r\n```\r\n\r\n**1) If value has type then it MUST have NOT NULL value but only when we're validating this field.**\r\nLet's say we have the following class definition:\r\n\r\n```js\r\nfield: {\r\n  firstName: 'string'\r\n},\r\nvalidators: {\r\n  firstName: [\r\n    Validators.string(),\r\n    Validators.minLength(3)\r\n  ]\r\n}\r\n```\r\n\r\nAnd we try to validate the following object:\r\n\r\n```js\r\nvar u = new User();\r\nu.set('firstName', null);\r\nu.validate();\r\n```\r\n\r\nWe will get the following error: `The \"firstName\" field's value has to be a string`\r\n\r\n**2) We can also modify our class and add `required` validator:**\r\n\r\n```js\r\nfield: {\r\n  firstName: 'string'\r\n},\r\nvalidators: {\r\n  firstName: [\r\n    Validators.required(),\r\n    Validators.string(),\r\n    Validators.minLength(3)\r\n  ]\r\n}\r\n```\r\n\r\nAnd we will try to validate:\r\n\r\n```js\r\nvar u = new User();\r\nu.set('firstName', null);\r\nu.validate();\r\n```\r\n\r\nIt will get the error: `The \"firstName\" field's value is required`. It will also get the same error with empty string `\"\"`:\r\n\r\n```js\r\nvar u = new User();\r\nu.set('firstName', '');\r\nu.validate();\r\n```\r\n\r\n**3) AS YOU WROTE: If a field doesn't have a type, then anything can be stored there, so long as it passes validation AND FIELD'S VALUE IS NOT NULL**\r\n\r\n```js\r\n{\r\n  firstName: null\r\n},\r\nvalidators: {\r\n  firstName: []\r\n}\r\n\r\n/* ... */\r\n\r\nvar u = new User();\r\nu.set('firstName', null);\r\nu.validate(); // TRUE\r\n```\r\n\r\nor\r\n\r\n```js\r\n{\r\n  firstName: null\r\n},\r\nvalidators: {\r\n  firstName: [\r\n    Validators.required(),\r\n    Validators.string()\r\n  ]\r\n}\r\n\r\n/* ... */\r\n\r\nvar u = new User();\r\nu.set('firstName', null);\r\nu.validate(); // TRUE\r\n```\r\n\r\nbut\r\n\r\n```js\r\n{\r\n  firstName: null\r\n},\r\nvalidators: {\r\n  firstName: [\r\n    Validators.required()\r\n    Validators.string()\r\n  ]\r\n}\r\n\r\n/* ... */\r\n\r\nvar u = new User();\r\nu.set('firstName', 0);\r\nu.validate(); // FALSE\r\n```\r\n\r\n**SUMMARY**\r\nMaybe it's a little bit confusing but you just have to remember that:\r\n\r\n**Having VALIDATORS DEFINED for a field**\r\n- **When field HAS TYPE, then it IS REQUIRED**\r\n  - **For NULL value it WILL VALIDATE field**\r\n  - **For NON NULL value it WILL VALIDATE field**\r\n- **When field DOES NOT HAVE TYPE, then it IS NOT REQUIRED**\r\n  - **For NULL value it WILL NOT VALIDATE field**\r\n  - **For NON NULL value it WILL VALIDATE field**\r\n\r\nSo:\r\nHAS TYPE - WILL ALWAYS VALIDATE\r\nDOES NOT HAVE TYPE - WILL VALIDATE FOR NON NULL VALUES\r\n\r\nWould you be happy with such solution?"}}