{"action":"closed","issue":{"url":"https://api.github.com/repos/aurelia/validatejs/issues/95","repository_url":"https://api.github.com/repos/aurelia/validatejs","labels_url":"https://api.github.com/repos/aurelia/validatejs/issues/95/labels{/name}","comments_url":"https://api.github.com/repos/aurelia/validatejs/issues/95/comments","events_url":"https://api.github.com/repos/aurelia/validatejs/issues/95/events","html_url":"https://github.com/aurelia/validatejs/issues/95","id":161268292,"number":95,"title":"Validate complex entities","user":{"login":"jods4","id":3832820,"avatar_url":"https://avatars.githubusercontent.com/u/3832820?v=3","gravatar_id":"","url":"https://api.github.com/users/jods4","html_url":"https://github.com/jods4","followers_url":"https://api.github.com/users/jods4/followers","following_url":"https://api.github.com/users/jods4/following{/other_user}","gists_url":"https://api.github.com/users/jods4/gists{/gist_id}","starred_url":"https://api.github.com/users/jods4/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jods4/subscriptions","organizations_url":"https://api.github.com/users/jods4/orgs","repos_url":"https://api.github.com/users/jods4/repos","events_url":"https://api.github.com/users/jods4/events{/privacy}","received_events_url":"https://api.github.com/users/jods4/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aurelia/validatejs/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-06-20T18:57:19Z","updated_at":"2016-08-27T14:55:23Z","closed_at":"2016-08-27T14:55:23Z","body":"This is a spin-off of aurelia/validation#252 to answer @adarshpastakia who asked how to validate a `Hotel` that has a Map of `HotelLanguage`.\r\n\r\nI haven't actually built the code, but I would use something like this: \r\n```typescript\r\n// 1. Build a custom validator for validatejs. It validates all values of a Map\r\nimport { Validator } from 'aurelia-validatejs';\r\nconst validator = new Validator() // ugly... or DI\r\n\r\nvalidate.validators.map = function(value: Map) {\r\n  const hasErrors = map.values().some(v => validator.validateObject(v).length > 0);\r\n  // Unfortunately details are lost, but Aurelia's controller will evaluate and display them on the binding as well.\r\n  // This is only really useful for your `validate()` call when saving.\r\n  return hasErrors ? \"has invalid values\" : null; \r\n}\r\n\r\n// 2. If you want to make it useable with a decorator\r\nimport { base, ValidationRule } from 'aurelia-validatejs';\r\n\r\nconst mapRule = new ValidationRule('map', true);\r\n\r\nexport function validateValues(targetOrConfig, key, descriptor) {\r\n  return base(targetOrConfig, key, descriptor, mapRule, true);\r\n}\r\n\r\n// 3. Now you should be able to do so:\r\nclass Hotel {\r\n  @validateValues\r\n  languages: Map<string, HotelLanguages>;\r\n}\r\n```\r\n\r\n@jdanyow \r\nWriting this code has shown me a few things: it is cumbersome to write your own validators.\r\nSuggestions:\r\n- export a simple function to get or set constraints/metadata `getRules(object)`.\r\n- drop the custom metadata format `ValidationRule`. In the code above I had to do a little dance around `Validator` because the stored rules are not in the native `validatejs` format. Is there any reason not to store plain constraints? It would make things easier and this module is specific to `validatejs` anyway, it's not generic.\r\n- declaring a new decorator is not very intuitive. This is rather minor, it's probably just a matter of naming `base` something else. Or maybe we could not export `base`, because with the changes suggested above I think the following code is decent:\r\n```typescript\r\nexport function validateValues(target, key) {\r\n  getRules(target)[key] = true;\r\n}\r\n```"}}
{"actor":{"display_login":"jdanyow"}}