{"id":16339800,"url":"https://github.com/jkachmar/purescript-validationtown","last_synced_at":"2025-11-04T03:30:35.121Z","repository":{"id":93201821,"uuid":"113923195","full_name":"jkachmar/purescript-validationtown","owner":"jkachmar","description":"Let's validate some stuff with Variant, simple-json, and Semigroups","archived":false,"fork":false,"pushed_at":"2017-12-12T02:15:14.000Z","size":41,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-27T07:11:38.763Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PureScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jkachmar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2017-12-12T00:20:41.000Z","updated_at":"2020-02-13T18:44:47.000Z","dependencies_parsed_at":"2023-04-07T22:47:16.583Z","dependency_job_id":null,"html_url":"https://github.com/jkachmar/purescript-validationtown","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkachmar%2Fpurescript-validationtown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkachmar%2Fpurescript-validationtown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkachmar%2Fpurescript-validationtown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkachmar%2Fpurescript-validationtown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jkachmar","download_url":"https://codeload.github.com/jkachmar/purescript-validationtown/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239425429,"owners_count":19636346,"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":[],"created_at":"2024-10-10T23:55:11.833Z","updated_at":"2025-11-04T03:30:35.018Z","avatar_url":"https://github.com/jkachmar.png","language":"PureScript","readme":"## PureScript Validation from JavaScript\n\nA small demo to show how you can use [purescript-validation][1] from within\nJavaScript, with a little help from [purescript-simple-json][2] and \n[purescript-variant][3].\n\n### Setup\n\nEnsure you have `node` and `npm` installed on your machine, then run the \nfollowing from the terminal:\n\n    git clone git@github.com:jkachmar/purescript-validationtown.git\n    npm install\n\nThis will install the `purescript` compiler, the `pulp` build tool, and the\n`psc-package` package manager to a local `node_modules` directory, as well as\nfetch the necessary PureScript dependencies and compile the demo.\n\n### What's Going On Here?\n\nSome basic validation logic has been encoded in the \n[`ValidateForm` module](src/ValidateForm.purs) module that we'd like to call\nfrom JavaScript with as little fuss as possible.\n\nIn order to safely call this function from JavaScript, we'll need a way to \nmarshall the untyped JavaScript record into a typed PureScript record. The\nuntyped record is represented by `Foreign`, and the typed result of our \nmarshalling is represented by the `UnvalidatedForm` type alias for a record with\nthe form structure we'll be validating.\n\nUsing [simple-json][2], this `Foreign` data can be parsed into an \n`UnvalidatedForm`, failing with an `unprocessable` error if the record is \nmalformed.\n\nThis properly typed, but not necessarily valid, form can then be passed through\na series of validation functions built up using the [purescript-validation][1]\nlibrary. Note that the errors are expressed as `Variant`s and are collected in\n`Array`s, which both have runtime representations that are relatively comfortable\nto work with in JavaScript.\n\nThe result of the validation is then unpacked with `unV` and tagged with either\n`formError` or `form` to indicate failure or success in validating the form,\nrespectively.\n\nFinally, the `validateForm` function is uncurried using `mkFn1`, so that it may\nbe more naturally called from JavaScript. \n\nThe [example JavaScript file](testValidation.js) included in this repository \nshows how the PureScript module can be imported. This example can be run with\n`npm run demo`, or directly with `node testValidation.js`, and produces the \nfollowing output:\n\n```\nThe input form failed to parse with the following errors:\n\nNonEmpty {\n  value0: \n   ErrorAtProperty {\n     value0: 'address',\n     value1: \n      ErrorAtProperty {\n        value0: 'address1',\n        value1: TypeMismatch { value0: 'String', value1: 'Undefined' } } },\n  value1: Nil {} }\n\nThe input form failed validation with the following errors:\n\n[ { type: 'badEmail',\n    value: \n     [ { type: 'emptyField', value: {} },\n       { type: 'tooShort', value: {} } ] },\n  { type: 'badUsername',\n    value: \n     [ { type: 'emptyField', value: {} },\n       { type: 'tooShort', value: {} } ] },\n  { type: 'badPassword',\n    value: \n     [ { type: 'emptyField', value: {} },\n       { type: 'tooShort', value: {} } ] },\n  { type: 'badAddress',\n    value: \n     [ { type: 'emptyField', value: {} },\n       { type: 'emptyField', value: {} },\n       { type: 'emptyField', value: {} },\n       { type: 'emptyField', value: {} },\n       { type: 'emptyField', value: {} } ] } ]\n\nThe input form was successfully processed:\n\n{ email: 'example@example.org',\n  username: 'example',\n  password: 'guest123456',\n  address: \n   { address1: 'MyStreet',\n     address2: 'MyApt',\n     city: 'MyCity',\n     zipCode: 'MyZipCode',\n     country: 'MyCountry' } }\n```\n\n### Why Variant?\n\nNormally errors encountered during Validation would be tagged with a sum type,\nso why use a `Variant` here?\n\nWell it turns out that `Variant`'s JavaScript runtime representation is a plain\nJavaScript object with a `type` key for the `SProxy` tag, and a `value` key for\nthe value.\n\nThis makes it pretty straightforward to process the result of a validation\nperformed in PureScript from JavaScript without any encoding/decoding helper\nfunctions.\n\n[1]: https://github.com/purescript/purescript-validation/\n[2]: https://github.com/justinwoo/purescript-simple-json\n[3]: http://github.com/natefaubion/purescript-variant\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjkachmar%2Fpurescript-validationtown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjkachmar%2Fpurescript-validationtown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjkachmar%2Fpurescript-validationtown/lists"}