module Data.JSON.Schema.Types where
import Data.Maybe
import Data.Proxy
import Data.Text (Text)
import Data.Word (Word32)
type Schema = Value
data Value =
Choice [Value]
| Object [Field]
| Array Int Int Bool Value
| Tuple [Value]
| Value Int Int
| Boolean
| Number Int Int
| Null
deriving (Eq, Show)
data Field = Field { key :: String, required :: Bool, content :: Schema } deriving (Eq, Show)
class JSONSchema a where
schema :: Proxy a -> Schema
instance JSONSchema () where
schema _ = Null
instance JSONSchema Int where
schema _ = Number 0 (1)
instance JSONSchema Integer where
schema _ = Number 0 (1)
instance JSONSchema Word32 where
schema _ = Number 0 4294967295
instance JSONSchema Bool where
schema _ = Boolean
instance JSONSchema Text where
schema _ = Value 0 (1)
instance JSONSchema a => JSONSchema (Maybe a) where
schema p = Choice [Object [Field "Just" True $ schema $ fmap fromJust p], Object [Field "Nothing" True Null]]
instance JSONSchema a => JSONSchema [a] where
schema = Array 0 (1) False . schema . fmap head