Skip to content

Commit 3e4fbbc

Browse files
committed
Expose more modules and improve documentation.
1 parent 39a9155 commit 3e4fbbc

File tree

8 files changed

+63
-15
lines changed

8 files changed

+63
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Generate validating forms from JSON schemas.
1616

1717
1. The way form fields are generated and presented is very opinionated and thus not always suitable for general case usage. This library is intended to be used for cases where you have control over how the schema is structured.
1818
2. The HTML that the library outputs is intended to be used together with [Bootstrap](https://getbootstrap.com/) to style the form. It can of course be used without Bootstrap but some field types might need some custom styling to look ok.
19+
3. There is currently no support for linked schemas using `$ref`.
1920

2021

2122
## Example usage

elm.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
"version": "1.0.0",
77
"exposed-modules": [
88
"Json.Schema.Form",
9+
"Json.Schema.Form.Encode",
910
"Json.Schema.Form.Error",
10-
"Json.Schema.Form.Encode"
11+
"Json.Schema.Form.Format",
12+
"Json.Schema.Form.Value"
1113
],
1214
"elm-version": "0.19.0 <= v < 0.20.0",
1315
"dependencies": {

src/Json/Schema/Form.elm

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Json.Schema.Form exposing
22
( Options, State, Msg
33
, init, update
44
, view, onSubmit
5+
, getOutput
56
)
67

78
{-| Generate validating forms from JSON schemas.
@@ -63,7 +64,7 @@ type alias Msg =
6364
F.Msg
6465

6566

66-
{-| Initialize a form state with options and a schema.
67+
{-| Initialize a form state with options and a schema. Use [json-tools/json-schema](https://package.elm-lang.org/packages/json-tools/json-schema/1.0.2/) to parse or build a schema.
6768
-}
6869
init : Options -> Schema -> State
6970
init options schema =
@@ -96,10 +97,28 @@ view state =
9697
9798
form [ Json.Schema.Form.onSubmit ]
9899
[ Json.Schema.Form.view state
99-
, button [] [ test "Submit" ]
100+
, button [] [ text "Submit" ]
100101
]
101102
102103
-}
103104
onSubmit : Attribute Msg
104105
onSubmit =
105106
Html.Events.onSubmit F.Submit
107+
108+
109+
{-| Get the output value of the form if it validates.
110+
111+
case Json.Schema.Form.getOutput state of
112+
Just value ->
113+
-- The `value` is a tree of all the fields in the form (see the
114+
-- `Json.Schema.Form.Value` module).
115+
-- Use the `Json.Schema.Form.Encode.encode` function to turn
116+
-- it into a JSON value.
117+
Nothing ->
118+
-- If any field in the form is not valid `getOutput` will
119+
-- return `Nothing`.
120+
121+
-}
122+
getOutput : State -> Maybe Value
123+
getOutput state =
124+
F.getOutput state.form

src/Json/Schema/Form/Fields.elm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Json.Schema.Definitions
2121
, blankSchema
2222
)
2323
import Json.Schema.Form.Error exposing (ErrorValue, Errors)
24-
import Json.Schema.Form.Format exposing (Formats, getFormat)
24+
import Json.Schema.Form.Format exposing (CustomFormat, Formats)
2525
import Json.Schema.Form.Validation exposing (validation)
2626
import Json.Schema.Form.Value exposing (Value(..))
2727

@@ -485,3 +485,8 @@ conditional f conditions =
485485
Nothing
486486
in
487487
Html.Keyed.node "div" [] <| List.filterMap cond conditions
488+
489+
490+
getFormat : String -> Formats -> Maybe CustomFormat
491+
getFormat format formats =
492+
formats |> Dict.fromList |> Dict.get format

src/Json/Schema/Form/Format.elm

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
module Json.Schema.Form.Format exposing (CustomFormat, Formats, getFormat)
1+
module Json.Schema.Form.Format exposing (Formats, CustomFormat)
2+
3+
{-| In a JSON schema the `format` keyword has a number of pre-defined formats (`date`, `email`, etc.) but can also be any custom format (see [7. Semantic Validation with "format"](https://json-schema.org/latest/json-schema-validation.html#rfc.section.7)). If you simply need a to match a field against a regular expression you should use the `pattern` keyword instead. Custom formats are intended for annotation and more complex validation that is not possible to accomplish with a regex.
4+
5+
@docs Formats, CustomFormat
6+
7+
-}
28

3-
import Dict
49
import Form.Validate exposing (Validation)
510
import Json.Schema.Form.Error exposing (ErrorValue)
611

712

13+
{-| A list of custom formats with a format identifier for each.
14+
-}
815
type alias Formats =
916
List ( String, CustomFormat )
1017

1118

19+
{-| A custom format has a title and a validation function that validates the string value of the field according to the rules of the format.
20+
21+
- `title` - A user readable description of the format that is used to annotate the field.
22+
- `validation` - A validation function (see [etaque/elm-form](https://package.elm-lang.org/packages/etaque/elm-form/4.0.0/Form-Validate) for details on how to write a validation function).
23+
24+
-}
1225
type alias CustomFormat =
1326
{ title : Maybe String
1427
, validation : String -> Validation ErrorValue String
1528
}
16-
17-
18-
getFormat : String -> Formats -> Maybe CustomFormat
19-
getFormat format formats =
20-
formats |> Dict.fromList |> Dict.get format

src/Json/Schema/Form/Validation.elm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Json.Schema.Definitions
1818
)
1919
import Json.Schema.Form.Encode
2020
import Json.Schema.Form.Error exposing (ErrorValue(..))
21-
import Json.Schema.Form.Format exposing (Formats)
21+
import Json.Schema.Form.Format exposing (CustomFormat, Formats)
2222
import Json.Schema.Form.Regex
2323
import Json.Schema.Form.Value exposing (Value(..))
2424
import Regex
@@ -422,3 +422,8 @@ isType types schema_ =
422422
lazy : (() -> Validation e o) -> Validation e o
423423
lazy thunk =
424424
andThen thunk (succeed ())
425+
426+
427+
getFormat : String -> Formats -> Maybe CustomFormat
428+
getFormat format formats =
429+
formats |> Dict.fromList |> Dict.get format

src/Json/Schema/Form/Value.elm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
module Json.Schema.Form.Value exposing (Value(..))
22

3+
{-| When a form is valid the output is a tree of `Value` items that represent the type and value of each field or group/list of fields in the form.
34
5+
@docs Value
6+
7+
-}
8+
9+
10+
{-| A form value.
11+
-}
412
type Value
513
= IntValue Int
614
| FloatValue Float

tests/ValidationTest.elm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import Expect exposing (Expectation)
44
import Form.Field exposing (..)
55
import Json.Schema.Builder exposing (..)
66
import Json.Schema.Definitions exposing (..)
7-
import Schema.Validation exposing (validation)
8-
import Schema.Value exposing (Value(..))
7+
import Json.Schema.Form.Validation exposing (validation)
8+
import Json.Schema.Form.Value exposing (Value(..))
99
import Test exposing (..)
1010

1111

1212
suite : Test
1313
suite =
14-
describe "Schema.Validation"
14+
describe "Json.Schema.Form.Validation"
1515
[ describe "validation"
1616
[ describe "with a blank schema"
1717
[ describe "any type of input"

0 commit comments

Comments
 (0)