json-tools/json-schema/1.0.0

NOTE: the latest version is 1.0.2

Json.Schema.Builder

Convenience API to build a valid JSON schema

Definition

= SchemaBuilder
{ errors : List String
, schema : Maybe SubSchema
, bool : Maybe Bool
}

Builder for JSON schema providing an API like this:

buildSchema
    |> withTitle "My object"
    |> withProperties
        [ ( "foo"
          , buildSchema
                |> withType "string"
          )
        , ( "bar"
          , buildSchema
                |> withType "integer"
                |> withMaximum 10
          )
        ]

Schema builder creation

Create schema builder with blank schema

Create boolean schema

toSchema : SchemaBuilder -> Result String Schema

Extract JSON Schema from the builder

encode : Int -> Schema -> String

Encode schema into a builder code (elm)

Building up schema

Type

JSON Schema spec allows type to be string or array of strings. There are three groups of types produced: single types (e.g. "string"), nullable types (e.g. ["string", "null"]) and union types (e.g. ["string", "object"])

Set the type property of JSON schema to a specific type, accepts strings

buildSchema
    |> withType "boolean"

Set the type property of JSON schema to a nullable type.

buildSchema
    |> withNullableType "string"

Set the type property of JSON schema to an union type.

buildSchema
    |> withUnionType [ "string", "object" ]

Meta

JSON-Schema

Numeric validations

The following validations are only applicable to numeric values and will always succeed for any type other than number and integer

String validations

Array validations

Set the contains property of JSON schema to a sub-schema.

buildSchema
    |> withContains
        (buildSchema
            |> withType "string"
        )

Object validations

Generic validations

Validation

ValidationOptions
-> Value
-> Result (List Error) Value

Validate value using schema controlled by builder.