{"id":22409479,"url":"https://github.com/iodevs/elm-validate","last_synced_at":"2025-03-27T02:22:16.319Z","repository":{"id":52694727,"uuid":"121251598","full_name":"iodevs/elm-validate","owner":"iodevs","description":"Elm validation library","archived":false,"fork":false,"pushed_at":"2021-04-20T20:18:45.000Z","size":153,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T07:43:49.425Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"https://package.elm-lang.org/packages/iodevs/elm-validate/3.0.2","language":"Elm","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iodevs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-02-12T13:44:40.000Z","updated_at":"2022-09-13T08:12:35.000Z","dependencies_parsed_at":"2022-08-22T06:40:24.573Z","dependency_job_id":null,"html_url":"https://github.com/iodevs/elm-validate","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iodevs%2Felm-validate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iodevs%2Felm-validate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iodevs%2Felm-validate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iodevs%2Felm-validate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iodevs","download_url":"https://codeload.github.com/iodevs/elm-validate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245767588,"owners_count":20668880,"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":["hacktoberfest"],"created_at":"2024-12-05T12:07:50.652Z","updated_at":"2025-03-27T02:22:16.289Z","avatar_url":"https://github.com/iodevs.png","language":"Elm","readme":"# elm-validate\nThis library helps with a validation of input forms and it's based on Enrico Buonanno lessons on [egghead.io](https://egghead.io/courses/form-validation-in-elm/).\n\n## Install\n```\nelm install iodevs/elm-validate\n```\n\n\n## Usage:\nFor example as a validation of\n* the register / login /... form, see [example](https://github.com/iodevs/elm-validate/tree/master/example) directory in this repository or a live [demo](https://iodevs.github.io/elm-validate/)\n* the input forms for your applications, see below very simplified an example.\n\nLet say, you have an employee form by which you can add new an employee to your system. You want to save his a photo, a name, an email and an age. The form responds to record `EmployeeForm` where for each input form is defined type `Fields`.\n\n![](https://github.com/iodevs/elm-validate/raw/master/docs/employee-example.png)\n\nFor sending/fetching these data to/from server you'll need to define a record `Employee`:\n\n```elm\ntype alias Employee =\n    { name : String\n    , email : String\n    , age : Int\n    , photo : ...\n    }\n```\n\nand functions for transforming between these two records `formToEmployee`:\n\n```elm\nformToEmployee : EmployeeForm -\u003e Result String Employee\nformToEmployee form =\n    let\n        fieldOk =\n            withField Ok\n\n        valueOk =\n            withoutField Ok\n    in\n    toModel\n        Employee\n        ( valueOk ( \\item -\u003e form.photo )\n            \u003e\u003e fieldOk( \\item -\u003e form.name )\n            \u003e\u003e fieldOk( \\item -\u003e form.email )\n            \u003e\u003e fieldOk( \\item -\u003e form.age )\n        )\n        form\n```\n\nand `employeeToForm`:\n\n```elm\nemployeeToForm : Employee -\u003e EmployeeForm\nemployeeToForm employee =\n    { name = preValidatedField employee.name\n    , email = preValidatedField employee.email\n    , age = preValidatedField employee.age\n    , photo = ...\n    }\n```\n\nNow if you have the record `Employee` it's enough to write a decoder:\n\n```elm\ndecodeEmployee : Decoder Employee\ndecodeEmployee = ...\n```\n\nand an encoder for him:\n\n```elm\nencodeEmployee : Employee -\u003e Value\nencodeEmployee employee = ...\n```\n\nLast step before sending data is their validation by `OnSubmit`:\n\n```elm\nsetValidityEmployeeForm : EmployeeForm -\u003e EmployeeForm\nsetValidityEmployeeForm form =\n    let\n        name =\n            form.name\n                |\u003e validate OnSubmit nameValidation\n\n        email =\n            form.email\n                |\u003e validate OnSubmit emailValidation\n\n        age =\n            form.age\n                |\u003e validate OnSubmit ageValidation\n\n    in\n        { form\n            | name = name\n            , email = email\n            , age = age\n        }\n```\n\nwhere particular validations can be defined as:\n\n```elm\nnameValidation : Validator String String\nnameValidation =\n    isNotEmpty \"Name of employee is required.\"\n\nemailValidation : Validator String String\nemailValidation =\n    (isNotEmpty \"Email of employee is required.\")\n        |\u003e composite (isEmail \"Incorrect email.\")\n\n\nageValidation : Validator String Int\nageValidation =\n    \"You cannot employ a person younger than 10 year old or elder 100!\"\n        |\u003e isRange (isInt \"Age must be integer number!\") 10 100\n```\n\nOf course, also these validations you'll use that same a way as in previous Register form example. A relevant parts (messages `InputEmail`, `BlurEmail`, etc.) are same or similar and omitted here.\n\nFinally, somewhere inside `update` function will be:\n\n```elm\nSendEmployeeToServer employeeForm -\u003e\n    let\n        form =\n            employeeForm\n                |\u003e setValidityEmployeeForm\n    in\n        employeeForm\n            |\u003e formToEmployee\n            |\u003e Result.map\n                (\\data -\u003e\n                    ( { model\n                        | form = form\n                      }\n                    , Http.post\n                        { url = ...\n                        , body = Http.jsonBody (encodeEmployee data)\n                        , ...\n                        }\n                    )\n                )\n            |\u003e Result.withDefault\n                ( model, Cmd.none )\n\nGotEmployeeFromServer (Ok data) -\u003e\n    ( { model\n        | form = employeeToForm data\n      }\n    , Cmd.none\n    )\n\nGotEmployeeFromServer (Err err) -\u003e\n    ...\n```\n\nwhere message `SendEmployeeToServer` will be called by clicking \"check\" button at the employee form. Similarly for getting data you'll need `GotEmployeeFromServer` message. This message is binded with command:\n\n```elm\ngetEmployeeFromServer : Cmd Msg\ngetEmployeeFromServer =\n    Http.get\n        { url = ...\n        , expect = Http.expectJson GotEmployeeFromServer (Decode.decodeValue decodeEmployee)\n        }\n```\nwhich will be used in init part and somewhere else in your app.\n\n\n## ChangeLog\nsee [CHANGELOG](https://github.com/iodevs/elm-validate/blob/master/CHANGELOG.md)\n\n\n## License\n[![BSD](https://img.shields.io/badge/license-BSD-blue.svg)](https://github.com/iodevs/elm-validate/blob/master/LICENSE)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiodevs%2Felm-validate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiodevs%2Felm-validate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiodevs%2Felm-validate/lists"}