Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
niho committed Jan 1, 2019
0 parents commit 9a920f6
Show file tree
Hide file tree
Showing 11 changed files with 917 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
elm-stuff/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Niklas Holmgren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/html": "1.0.0",
"elm/json": "1.1.2",
"elm/regex": "1.0.0",
"etaque/elm-form": "4.0.0",
"json-tools/json-schema": "1.0.2"
},
"indirect": {
"NoRedInk/elm-json-decode-pipeline": "1.0.0",
"elm/random": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-explorations/test": "1.2.0",
"zwilias/elm-utf-tools": "2.0.1"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
19 changes: 19 additions & 0 deletions elm.package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "package",
"name": "niho/json-schema-form",
"summary": "Generate validating forms from JSON schemas.",
"license": "MIT",
"version": "1.0.0",
"exposed-modules": [],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.2 <= v < 2.0.0",
"elm/html": "1.0.0 <= v < 2.0.0",
"elm/json": "1.1.2 <= v < 2.0.0",
"etaque/elm-form": "4.0.0 <= v < 5.0.0",
"json-tools/json-schema": "1.0.2 <= v < 2.0.0"
},
"test-dependencies": {
"elm-explorations/test": "1.2.0 <= v < 2.0.0"
}
}
109 changes: 109 additions & 0 deletions src/Main.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module Main exposing (main)

import Browser
import Form exposing (Msg(..))
import Html exposing (..)
import Html.Events exposing (..)
import Json.Encode exposing (bool, string)
import Json.Schema.Builder exposing (..)
import Schema
import Schema.Encode


main =
Browser.sandbox { init = init, update = update, view = view }


init =
let
schema =
buildSchema
|> withId "http://example.com/schema#"
|> withTitle "My object"
|> withDescription "This is an example form."
|> withType "object"
|> withProperties
[ ( "name"
, buildSchema
|> withType "string"
|> withTitle "Name"
|> withDescription "Please enter your name."
|> withMaxLength 10
|> withMinLength 2
)
, ( "foo"
, buildSchema
|> withType "integer"
|> withExclusiveMinimum 5
|> withExclusiveMaximum 10
)
, ( "bar"
, buildSchema
|> withType "number"
|> withMinimum 5.5
|> withMaximum 10.1
)
, ( "color"
, buildSchema
|> withTitle "Select a color"
|> withDescription "Choose any of three colors."
|> withType "string"
|> withEnum
[ string "red"
, string "green"
, string "blue"
]
)
, ( "contact"
, buildSchema
|> withType "object"
|> withProperties
[ ( "email"
, buildSchema
|> withType "string"
|> withTitle "Email"
|> withDescription "Your email address."
|> withFormat "email"
)
, ( "phone"
, buildSchema
|> withType "string"
|> withTitle "Phone"
|> withDescription "Your phone number."
|> withFormat "phone"
)
]
)
, ( "terms"
, buildSchema
|> withType "boolean"
|> withTitle "Jag accepterar villkoren"
|> withConst (bool True)
)
]
|> toSchema
in
case schema of
Ok s ->
Schema.init s

Err error ->
Debug.todo error


update msg state =
Debug.log "state" (Schema.update msg state)


view state =
form [ onSubmit Form.Submit ]
[ Schema.view state
, button [] [ text "Submit" ]
, case Form.getOutput state.form of
Just output ->
pre []
[ text (Json.Encode.encode 4 (Schema.Encode.encode output)) ]

Nothing ->
text ""
]
Loading

0 comments on commit 9a920f6

Please sign in to comment.