• Stars
    star
    144
  • Rank 248,062 (Top 6 %)
  • Language
    Elm
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convenience functions for validating Elm data.

elm-validate

elm-validate provides convenience functions for validating data.

It is based around the idea of a Validator, which runs checks on a subject and returns a Result which can be be either Ok (Valid originalSubject) if there were no validation errors or Err validationErrors if the validation failed.

case validate someValidator someSubject of
  Ok validSubject -> ...--> (Valid someSubject)
  Err validationErrors -> ...--> List of validation errors

For example:

import Validate exposing (ifBlank, ifNotInt, validate)


type alias Model =
    { name : String, email : String, age : String, selections : List String }


modelValidator : Validator String Model
modelValidator =
    Validate.all
        [ ifBlank .name "Please enter a name."
        , ifBlank .email "Please enter an email address."
        , ifNotInt .age "Age must be a whole number."
        , ifEmptyList .selections "Please select at least one."
        ]


validate modelValidator
    { name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
    --> Err [ "Please enter an email address.", "Age must be a whole number." ]

validate modelValidator
    { name = "Sam", email = "[email protected]", age = "27", selections = [ "cats" ] }
    --> Ok (Valid { name = "Sam", email = "[email protected]", age = "27", selections = [ "cats" ] })

You can represent your errors however you like. One nice approach is to use tuple of the error message and the field responsible for the error:

type Field =
    Name | Email | Age | Selections


modelValidator : Validator ( Field, String ) Model
modelValidator =
    Validate.all
        [ ifBlank .name ( Name, "Please enter a name." )
        , ifBlank .email ( Email, "Please enter an email address." )
        , ifNotInt .age ( Age, "Age must be a whole number." )
        , ifEmptyList .selections ( Selections, "Please select at least one." )
        ]


type alias Model =
    { name : String, email : String, age : String }


validate modelValidator
    { name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
    --> Err [ ( Email, "Please enter an email address." )
    -->   , ( Age, "Age must be a whole number." )
    -->   ]

Functions that detect the absence of a value, such as ifBlank and ifEmptyList, accept an error as the second argument:

modelValidator : Validator ( Field, String ) Model
modelValidator =
    Validate.all
        [ ifBlank .name ( Name, "Please enter a name." )
        , ifEmptyList .selections ( Selections, "Please select at least one." )
        ]

Functions the detect something wrong with a value, such as isNotInt and isInvalidEmail, accept a function as the second argument so you can include the incorrect value in the error message:

modelValidator : Validator ( Field, String ) Model
modelValidator =
    Validate.all
        [ ifNotInt .count (\value -> ( Count, value ++ " is not an integer." ))
        , ifInvalidEmail .email (\value -> ( Email, value ++ " is not a valid email address." ))
        ]

More Repositories

1

seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
JavaScript
5,363
star
2

elm-spa-example

A Single Page Application written in Elm
Elm
3,251
star
3

elm-css

Typed CSS in Elm.
Elm
1,214
star
4

dreamwriter

(NOTE! This has not been updated since Elm 0.15.1.) Streamlined long-form writing software. Written in Elm.
JavaScript
459
star
5

elm-workshop

An introductory Elm workshop. Video available on Frontend Masters!
Elm
438
star
6

elm-0.19-workshop

Elm 0.19 workshops. Includes Intro and Advanced courses.
Elm
210
star
7

rust-1.51-workshop

Richard Feldman's Rust workshop
Rust
186
star
8

node-elm-compiler

A Node.js interface to the Elm compiler binaries.
TypeScript
170
star
9

elm-in-action

Resources for the Elm In Action book.
JavaScript
160
star
10

node-test-runner

Runs elm-test suites from Node.js. Get it with npm install -g elm-test
JavaScript
132
star
11

elm-google-maps

Using Google Maps in Elm by way of Polymer
Elm
56
star
12

elm-use

Switch between Elm versions
JavaScript
38
star
13

elm-iso8601-date-strings

Convert between ISO-8601 date strings and Time.Posix
Elm
30
star
14

elm-web-workers

Write Elm code that talks to Web Workers.
Elm
28
star
15

selectlist

A nonempty list in which exactly one element is always selected.
Elm
27
star
16

elm-json-experiment

Experimental API for building JSON decoders
Elm
20
star
17

lambdaconf-2015-elm-workshop

Materials for the Elm Workshop at LambdaConf 2015
Elm
20
star
18

grunt-elm

Grunt plugin that compiles Elm files to JavaScript.
JavaScript
19
star
19

elm-codify

A tool to generate Elm decoders, encoders, and type aliases from JSON.
Elm
16
star
20

elm-0.18-workshop

An introductory Elm 0.18 workshop.
Elm
14
star
21

elm-node-app

Communication layer so Elm code can talk to Node.js code.
Elm
13
star
22

elm-css-helpers

Helpers for using elm-css with elm-html
Elm
11
star
23

dotfiles

Configuration files I use.
Shell
11
star
24

style-elements-demo

style-elements demo for ReactiveConf 2017 talk, "CSS as Bytecode"
Elm
8
star
25

elm-sorter-experiment

Experimental Sorter/Dict/Set API
Elm
8
star
26

elm-hex

Work with Hexadecimal numbers in Elm.
Elm
6
star
27

dreamwriter-coffee

Streamlined writing software. Written in React and CoffeeScript.
CoffeeScript
6
star
28

vector

List where head & tail don't return Maybe and still can't crash
Elm
6
star
29

count

Call record constructors with increasing integers. Surprisingly useful!
Elm
5
star
30

elm-signup-form

JavaScript
5
star
31

test-update

Convenience functions for testing update functions
Elm
5
star
32

mloc-2015-elm-workshop

Materials for the Elm Workshop at mloc.js 2015
Elm
5
star
33

hashed-class

Generate CSS classnames by hashing their styles.
Elm
5
star
34

hex

Work with Hexadecimal numbers in Elm.
Elm
4
star
35

roc-decimal-draft

Rust
3
star
36

quicksort-benchmarks

quicksort-benchmarks
Swift
3
star
37

csrf-xhr

Automatically add Rails CSRF tokens into XMLHttpRequest headers.
JavaScript
3
star
38

elm-in-action.com

Website for the book Elm in Action, from Manning Publications
3
star
39

console-print

Print special characters to the console, in Elm
Elm
3
star
40

ziplist

DEPRECATED in favor of rtfeldman/selectlist (better name!)
Elm
3
star
41

path

Elm
2
star
42

operation-pterodactyl

🦉
Elm
2
star
43

elm-css-util

Utility functions shared by elm-css and elm-css-helpers
Elm
2
star
44

elm-dropbox

Elm API for integrating with Dropbox
JavaScript
2
star
45

legacy-elm-test

An implementation of the legacy ElmTest module for backwards compatibility.
Elm
2
star
46

openresty-postgres-example

RealWorld example backend using OpenResty, Postgres, and nothing else
JavaScript
1
star
47

binstall

JavaScript
1
star
48

sweetvalidation

CoffeeScript
1
star
49

promises-done-polyfill

Polyfills Promise.done()
JavaScript
1
star