import form. { type FieldError , type Form , type Schema , CustomError , MustBeAccepted , MustBeColour , MustBeEmail , MustBeFloatLessThan , MustBeInt , MustBeIntLessThan , MustBeIntMoreThan , MustBePhoneNumber , MustBeStringLengthLessThan , MustBeStringLengthMoreThan , MustConfirm } import std/list import std/test.{assert, assert_equal} @derive Eq Show Debug type SignUp { SignUp(email: String, password: String) } @derive Eq Show Debug type Visitor { Guest(email: String), Member(name: String) } type Starter { Squirtle, Bulbasaur, Charmander } /// The README's example: an email, a password of 8+ characters, and its /// confirmation. fn signup_schema :: -> Schema { use email <- form.field "email" (form.parse_email ()) use password <- form.field "password" ({ form.parse_string () |> form.check_string_length_more_than 7 }) use _ <- form.field "confirm" ({ form.parse_string () |> form.check_confirms password }) form.success SignUp(email: email, password: password) } fn signup_form :: values: List<(String, String)> -> Result> { form.new (signup_schema ()) |> form.add_values values |> form.run } fn expect_ok :: result: Result> -> a { match result { Ok(value) -> value, Error(_) -> panic "expected the form to be valid" } } fn expect_error :: result: Result> -> Form { match result { Ok(_) -> panic "expected the form to be invalid", Error(f) -> f } } @test fn a_valid_form_decodes :: { let signup = expect_ok ( signup_form [ ("email", "ada@example.com") , ("password", "correct horse") , ("confirm", "correct horse") ] ) assert_equal signup SignUp(email: "ada@example.com", password: "correct horse") } @test fn every_bad_field_reports_its_errors :: { let f = expect_error ( signup_form [ ("email", "not an email") , ("password", "short") , ("confirm", "different") ] ) assert_equal (form.field_errors f "email") [MustBeEmail] assert_equal (form.field_errors f "password") [MustBeStringLengthMoreThan(7)] assert_equal (form.field_errors f "confirm") [MustConfirm] // The values stay on the form, for re-rendering. assert_equal (form.field_value f "email") "not an email" } @test fn missing_fields_parse_as_absent :: { let f = expect_error (signup_form []) assert_equal (form.field_errors f "email") [MustBeEmail] assert_equal (form.field_errors f "password") [MustBeStringLengthMoreThan(7)] } @test fn a_parse_failure_skips_the_checks_after_it :: { let schema = { use age <- form.field "age" ( { form.parse_int () |> form.check_int_more_than 0 |> form.check_int_less_than 150 } ) form.success age } let f = expect_error (form.new schema |> form.add_values [("age", "old")] |> form.run) assert_equal (form.field_errors f "age") [MustBeInt] } @test fn checks_do_not_short_circuit_each_other :: { let schema = { use name <- form.field "name" ( { form.parse_string () |> form.check_not_empty |> form.check_string_length_less_than 4 } ) form.success name } let f = expect_error (form.new schema |> form.add_values [("name", "longname")] |> form.run) assert_equal (form.field_errors f "name") [MustBeStringLengthLessThan(4)] } @test fn int_and_float_ranges :: { let schema = { use age <- form.field "age" ( { form.parse_int () |> form.check_int_more_than 0 |> form.check_int_less_than 150 } ) use price <- form.field "price" ( { form.parse_float () |> form.check_float_more_than 0.0 |> form.check_float_less_than 100.0 } ) form.success (age, price) } assert_equal ( expect_ok ( form.new schema |> form.add_values [("age", "42"), ("price", "9.99")] |> form.run ) ) (42, 9.99) // An int is a valid float. assert_equal ( expect_ok ( form.new schema |> form.add_values [("age", "42"), ("price", "9")] |> form.run ) ) (42, 9.0) let f = expect_error ( form.new schema |> form.add_values [("age", "0"), ("price", "100.0")] |> form.run ) assert_equal (form.field_errors f "age") [MustBeIntMoreThan(0)] // 100.0 is not less than 100.0: the limit is strict. assert_equal (form.field_errors f "price") [MustBeFloatLessThan(100.0)] } @test fn optional_fields :: { let schema = { use age <- form.field "age" (form.parse_optional (form.parse_int ())) form.success age } assert_equal (expect_ok (form.new schema |> form.run)) None assert_equal (expect_ok (form.new schema |> form.add_values [("age", "")] |> form.run)) None assert_equal (expect_ok (form.new schema |> form.add_values [("age", "42")] |> form.run)) Some(42) let f = expect_error (form.new schema |> form.add_values [("age", "old")] |> form.run) assert_equal (form.field_errors f "age") [MustBeInt] } @test fn repeated_fields_parse_as_a_list :: { let schema = { use tags <- form.field "tag" (form.parse_list (form.parse_int ())) form.success tags } assert_equal ( expect_ok ( form.new schema |> form.add_values [("tag", "1"), ("tag", "2"), ("tag", "3")] |> form.run ) ) [1, 2, 3] // A bad element fails the field; the errors deduplicate. let f = expect_error ( form.new schema |> form.add_values [("tag", "1"), ("tag", "x"), ("tag", "x")] |> form.run ) assert_equal (form.field_errors f "tag") [MustBeInt] } @test fn checkboxes :: { let schema = { use agreed <- form.field "terms" ({ form.parse_checkbox () |> form.check_accepted }) form.success agreed } assert_equal ( expect_ok (form.new schema |> form.add_values [("terms", "on")] |> form.run) ) True // An unchecked checkbox sends no value at all. let f = expect_error (form.new schema |> form.run) assert_equal (form.field_errors f "terms") [MustBeAccepted] } @test fn phone_numbers :: { let schema = { use phone <- form.field "phone" (form.parse_phone_number ()) form.success phone } assert_equal ( expect_ok ( form.new schema |> form.add_values [("phone", "+1 (555) 123-4567")] |> form.run ) ) "15551234567" let f = expect_error (form.new schema |> form.add_values [("phone", "12")] |> form.run) assert_equal (form.field_errors f "phone") [MustBePhoneNumber] } @test fn colours :: { let schema = { use colour <- form.field "colour" (form.parse_colour ()) form.success colour } assert_equal ( expect_ok (form.new schema |> form.add_values [("colour", "#a0FF00")] |> form.run) ) "#a0FF00" list.each ["a0FF00", "#12345", "#1234567", "#12345g"] fn value { let f = expect_error (form.new schema |> form.add_values [("colour", value)] |> form.run) assert_equal (form.field_errors f "colour") [MustBeColour] } } @test fn custom_parsers :: { let parse_starter = form.parse fn input { match input { ["Squirtle", ..] -> Ok(Squirtle) , ["Bulbasaur", ..] -> Ok(Bulbasaur) , ["Charmander", ..] -> Ok(Charmander) , _ -> Error((Squirtle, "must be a starter Pokémon")) } } let schema = { use starter <- form.field "starter" parse_starter form.success starter } let f = expect_error (form.new schema |> form.add_values [("starter", "Pikachu")] |> form.run) assert_equal (form.field_error_messages f "starter") ["must be a starter Pokémon"] } @test fn custom_checks :: { let schema = { use name <- form.field "name" ( { form.parse_string () |> form.check fn text { if text == "bother" { Error("must not be rude") } else { Ok(text) } } } ) form.success name } let f = expect_error (form.new schema |> form.add_values [("name", "bother")] |> form.run) assert_equal (form.field_errors f "name") [CustomError("must not be rude")] } @test fn prefilled_values :: { let f = form.new (signup_schema ()) |> form.add_int "one" 100 |> form.add_string "one" "Hello" |> form.add_string "two" "Hi!" assert_equal (form.field_values f "one") ["Hello", "100"] assert_equal (form.field_value f "one") "Hello" assert_equal (form.field_value f "missing") "" // set_values replaces rather than appends. let replaced = form.set_values f [("three", "3")] assert_equal (form.all_values replaced) [("three", "3")] } @test fn added_errors_surface_with_the_rest :: { let f = form.new (signup_schema ()) |> form.add_values [ ("email", "taken@example.com") , ("password", "correct horse") , ("confirm", "correct horse") ] // e.g. the address is already registered: checked outside the schema. let f = form.add_error f "email" form.MustBeUnique assert_equal (form.field_errors f "email") [form.MustBeUnique] assert_equal (form.field_error_messages f "email") ["is already in use"] } @test fn translations :: { assert_equal (form.en_gb MustBeColour) "must be a hex colour code" assert_equal (form.en_us MustBeColour) "must be a hex color code" assert_equal (form.en_gb form.MustBePresent) "must not be blank" assert_equal (form.en_gb MustBeIntMoreThan(0)) "must be more than 0" assert_equal (form.en_gb MustBeStringLengthLessThan(20)) "must be less than 20 characters" assert_equal (form.en_gb CustomError("custom")) "custom" // A custom translator via `language`. let bad = expect_error ( form.new (signup_schema ()) |> form.language fn _error { "ist ein Pflichtfeld" } |> form.add_values [] |> form.run ) assert_equal (form.field_error_messages bad "email") ["ist ein Pflichtfeld"] } @test fn variants_through_a_kind_field :: { // A hidden input says which variant the form is; the rest of the schema // follows from it. let schema = { use kind <- form.field "kind" (form.parse_string ()) match kind { "guest" -> { use email <- form.field "email" (form.parse_email ()) form.success Guest(email) } , _ -> { use name <- form.field "username" ({ form.parse_string () |> form.check_not_empty }) form.success Member(name) } } } assert_equal ( expect_ok ( form.new schema |> form.add_values [("kind", "guest"), ("email", "g@example.com")] |> form.run ) ) Guest("g@example.com") assert_equal ( expect_ok ( form.new schema |> form.add_values [("kind", "user"), ("username", "ada")] |> form.run ) ) Member("ada") } @test fn a_new_form_is_clean :: { let f = form.new (signup_schema ()) assert_equal (form.all_errors f) [] assert_equal (form.all_values f) [] }