{"id":30186060,"url":"https://github.com/dwayne/elm-field","last_synced_at":"2025-08-12T14:10:17.207Z","repository":{"id":308448726,"uuid":"1005855178","full_name":"dwayne/elm-field","owner":"dwayne","description":"Construct valid data from unreliable string inputs.","archived":false,"fork":false,"pushed_at":"2025-08-06T02:12:54.000Z","size":314,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-08-06T02:29:07.671Z","etag":null,"topics":["elm","form","form-validation"],"latest_commit_sha":null,"homepage":"https://package.elm-lang.org/packages/dwayne/elm-field/latest/","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/dwayne.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-21T00:28:40.000Z","updated_at":"2025-08-06T02:19:13.000Z","dependencies_parsed_at":"2025-08-06T02:29:31.428Z","dependency_job_id":"6265200a-ea8b-4426-830c-d1e2b8464880","html_url":"https://github.com/dwayne/elm-field","commit_stats":null,"previous_names":["dwayne/elm-field"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/dwayne/elm-field","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-field","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-field/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-field/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-field/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dwayne","download_url":"https://codeload.github.com/dwayne/elm-field/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-field/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270073664,"owners_count":24522390,"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","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["elm","form","form-validation"],"created_at":"2025-08-12T14:10:12.022Z","updated_at":"2025-08-12T14:10:17.199Z","avatar_url":"https://github.com/dwayne.png","language":"Elm","funding_links":[],"categories":[],"sub_categories":[],"readme":"# elm-field\n\nConstruct valid data from unreliable string inputs.\n\n## Examples\n\n### Example 1\n\nYou can create fields for all of Elm's primitive types.\n\n```elm\nimport Field as F exposing (Field)\n\n\nn : Field Int\nn = F.fromString F.int \"5\"\n\n\nf : Field Float\nf = F.fromString F.float \"3.14\"\n\n\nb : Field Bool\nb = F.fromString F.bool \"true\"\n\n\nc : Field Char\nc = F.fromString F.char \"a\"\n\n\ns : Field String\ns = F.fromString F.string \"Hello, world!\"\n\n\ntype alias Record =\n    { n : Int\n    , f : Float\n    , b : Bool\n    , c : Char\n    , s : String\n    }\n\n\nrecord : Maybe Record\nrecord =\n    Just Record\n        |\u003e F.applyMaybe n\n        |\u003e F.applyMaybe f\n        |\u003e F.applyMaybe b\n        |\u003e F.applyMaybe c\n        |\u003e F.applyMaybe s\n--\n-- record == Just { n = 5, f = 3.14, b = True, c = 'a', s = \"Hello, world!\" }\n--\n```\n\n### Example 2\n\nSuppose you need to get the age of a user from an input field. The input field gives you the user's input as a `String` which you need to interpret as the user's age. Furthermore, the user's age must be between 21 years and 35 years inclusive.\n\nHere's the quick and dirty way to do it.\n\n```elm\nimport Field as F exposing (Field, Type)\n\n\nyoungAdult : Type Int\nyoungAdult = F.subsetOfInt (\\n -\u003e n \u003e= 21 \u0026\u0026 n \u003c= 35)\n\n\ngoodAge : Field Int\ngoodAge = F.fromString youngAdult \"23\"\n--\n-- F.toResult goodAge == Ok 23\n--\n\n\ntooYoung : Field Int\ntooYoung = F.fromString youngAdult \"17\"\n--\n-- F.toResult tooYoung == Err [ F.validationError \"17\" ]\n--\n\n\ntooOld : Field Int\ntooOld = F.fromString youngAdult \"40\"\n--\n-- F.toResult tooOld == Err [ F.validationError \"40\" ]\n--\n```\n\nA better approach would be to create an `Age` type with custom validation errors.\n\n```elm\nmodule Age exposing\n    ( Age\n    , CustomError(..)\n    , Error\n    , errorToString\n    , fieldType\n    , fromString\n    , toString\n    )\n\nimport Field.Advanced as F\n\n\ntype Age\n    = Age Int\n\n\ntype alias Error =\n    F.Error CustomError\n\n\ntype CustomError\n    = TooYoung { actual : Int, min : Int }\n    | TooOld { actual : Int, max : Int }\n\n\nfromString : String -\u003e Result Error Age\nfromString =\n    (F.typeToConverters F.int).fromString\n        \u003e\u003e Result.andThen\n            (\\n -\u003e\n                if n \u003c 21 then\n                    Err (F.customError \u003c| TooYoung { actual = n, min = 21 })\n\n                else if n \u003e 35 then\n                    Err (F.customError \u003c| TooOld { actual = n, max = 35 })\n\n                else\n                    Ok (Age n)\n            )\n\n\ntoString : Age -\u003e String\ntoString (Age n) =\n    String.fromInt n\n\n\nfieldType : F.Type Error Age\nfieldType =\n    F.customType\n        { fromString = fromString\n        , toString = toString\n        }\n\n\nerrorToString : Error -\u003e String\nerrorToString =\n    F.errorToString\n        { onBlank = \"An age is required\"\n        , onSyntaxError = \\s -\u003e \"The age must be an integer: \" ++ s\n        , onValidationError = always \"\"\n        , onCustomError =\n            \\e -\u003e\n                case e of\n                    TooYoung { actual, min } -\u003e\n                        \"You must be at least \" ++ String.fromInt min ++ \" years old: \" ++ String.fromInt actual\n\n                    TooOld { actual, max } -\u003e\n                        \"You must be at most \" ++ String.fromInt max ++ \" years old: \" ++ String.fromInt actual\n        }\n```\n\nAnd then, you could use it as follows:\n\n```elm\nimport Age exposing (Age, Error)\nimport Field.Advanced as F exposing (Field)\n\n\ngoodAge : Field Error Age\ngoodAge = F.fromString Age.fieldType \"23\"\n--\n-- F.toResult goodAge == Ok (Age 23)\n--\n\n\ntooYoung : Field Error Age\ntooYoung = F.fromString Age.fieldType \"17\"\n--\n-- F.toResult tooYoung == Err [ F.customError (Age.TooYoung { actual = 17, min = 21 }) ]\n--\n\n\ntooOld : Field Error Age\ntooOld = F.fromString Age.fieldType \"40\"\n--\n-- F.toResult tooOld == Err [ F.customError (Age.TooOld { actual = 40, max = 35 }) ]\n--\n```\n\nThe custom errors could be converted to user-friendly error messages, using `Age.errorToString`, at a later time.\n\nYou can find the source code for many more examples in the [`examples/`](/examples) directory. And, you can fiddle with them at [https://dwayne.github.io/elm-field/](https://dwayne.github.io/elm-field/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwayne%2Felm-field","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdwayne%2Felm-field","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwayne%2Felm-field/lists"}