{"action":"created","issue":{"url":"https://api.github.com/repos/javaslang/javaslang.github.io/issues/37","labels_url":"https://api.github.com/repos/javaslang/javaslang.github.io/issues/37/labels{/name}","comments_url":"https://api.github.com/repos/javaslang/javaslang.github.io/issues/37/comments","events_url":"https://api.github.com/repos/javaslang/javaslang.github.io/issues/37/events","html_url":"https://github.com/javaslang/javaslang.github.io/pull/37","id":127032406,"number":37,"title":"adding documentation for 'Validation' type","user":{"login":"enelson","id":124756,"avatar_url":"https://avatars.githubusercontent.com/u/124756?v=3","gravatar_id":"","url":"https://api.github.com/users/enelson","html_url":"https://github.com/enelson","followers_url":"https://api.github.com/users/enelson/followers","following_url":"https://api.github.com/users/enelson/following{/other_user}","gists_url":"https://api.github.com/users/enelson/gists{/gist_id}","starred_url":"https://api.github.com/users/enelson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/enelson/subscriptions","organizations_url":"https://api.github.com/users/enelson/orgs","repos_url":"https://api.github.com/users/enelson/repos","events_url":"https://api.github.com/users/enelson/events{/privacy}","received_events_url":"https://api.github.com/users/enelson/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-16T15:40:33Z","updated_at":"2016-01-16T19:36:30Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/javaslang/javaslang.github.io/pulls/37","html_url":"https://github.com/javaslang/javaslang.github.io/pull/37","diff_url":"https://github.com/javaslang/javaslang.github.io/pull/37.diff","patch_url":"https://github.com/javaslang/javaslang.github.io/pull/37.patch"},"body":"Let me know if this is something you'd like to have on the Javaslang site. Thanks Daniel."},"comment":{"url":"https://api.github.com/repos/javaslang/javaslang.github.io/issues/comments/172248793","html_url":"https://github.com/javaslang/javaslang.github.io/pull/37#issuecomment-172248793","issue_url":"https://api.github.com/repos/javaslang/javaslang.github.io/issues/37","id":172248793,"user":{"login":"danieldietrich","id":743833,"avatar_url":"https://avatars.githubusercontent.com/u/743833?v=3","gravatar_id":"","url":"https://api.github.com/users/danieldietrich","html_url":"https://github.com/danieldietrich","followers_url":"https://api.github.com/users/danieldietrich/followers","following_url":"https://api.github.com/users/danieldietrich/following{/other_user}","gists_url":"https://api.github.com/users/danieldietrich/gists{/gist_id}","starred_url":"https://api.github.com/users/danieldietrich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danieldietrich/subscriptions","organizations_url":"https://api.github.com/users/danieldietrich/orgs","repos_url":"https://api.github.com/users/danieldietrich/repos","events_url":"https://api.github.com/users/danieldietrich/events{/privacy}","received_events_url":"https://api.github.com/users/danieldietrich/received_events","type":"User","site_admin":false},"created_at":"2016-01-16T19:36:30Z","updated_at":"2016-01-16T19:36:30Z","body":"Hi Eric,\r\n\r\nthis is a good idea! I've played around a bit with it and think that Validation deserves a complete example which devs can copy & paste to be used as base to build upon. Something like this:\r\n\r\nI'm not a native english speaker. Feel free to edit / delete / add as much text as you want :-)\r\n\r\n--\r\n\r\n### VALIDATION CONTROL\r\n\r\nValidation is an applicative functor, and facilitates accumulating errors. When trying to compose Monads, the combination process will short circuit at the first encountered error. But 'Validation' will continue processing the combining functions, accumulating all errors. This is especially useful when doing validation of multiple fields, say a web form, and you want to know all errors encountered, instead of one at a time.\r\n\r\n**Example:** We retrieve the fields 'name' and 'age' of a web form and want to create either a valid Person or return the list of validation error.\r\n\r\n```java\r\nPersonValidator validator = new PersonValidator();\r\n\r\n// Valid(Person(Eric, 39))\r\nvalidator.validatePerson(\"Eric\", 39);\r\n\r\n// Invalid(List(Validation contains invalid characters: '!4?', Not born yet.))\r\nvalidator.validatePerson(\"Wh4t!?\", -1);\r\n```\r\n\r\nFor that purpose we create a PersonValidator.\r\n\r\n```java\r\nclass PersonValidator {\r\n\r\n    private final String validNameChars = \"[a-zA-Z ]\";\r\n    private final int minAge = 0;\r\n\r\n    public Validation<List<String>, Person> validatePerson(String name, int age) {\r\n        return Validation.combine(validName(name), validAge(age)).ap(Person::new);\r\n    }\r\n\r\n    private Validation<String, String> validName(String name) {\r\n        return CharSeq.of(name).replaceAll(validNameChars, \"\").transform(seq ->\r\n                seq.isEmpty() ? Validation.valid(name)\r\n                              : Validation.invalid(\"Invalid name characters: '\" + seq.distinct().sort() + \"'\"));\r\n    }\r\n\r\n    private Validation<String, Integer> validAge(int age) {\r\n        return (age < minAge) ? Validation.invalid(\"Too young.\")\r\n                              : Validation.valid(age);\r\n    }\r\n}\r\n```\r\n\r\nOur Person is represented by a simple value object \r\n\r\n```java\r\npublic class Person {\r\n\r\n    public final String name;\r\n    public final int age;\r\n\r\n    public Person(String name, int age) {\r\n        this.name = name;\r\n        this.age = age;\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Person(\" + name + \", \" + age + \")\";\r\n    }\r\n}\r\n```\r\n"}}