@@ -24,6 +24,12 @@ type family Repr (ty :: JType) :: * where
24 Repr 'JText = String -- because of matchRegex
24 Repr 'JText = String -- because of matchRegex
25 Repr 'JNumber = Double
25 Repr 'JNumber = Double
27+type family CRepr (jty :: JType) :: * where
28+ CRepr JText = TextConstraint
29+ CRepr JNumber = NumberConstraint
30+ CRepr JObject = (String, Schema)
31+ CRepr JArray = ArrayConstraint
27class Constrained (ty :: JType) c where
33class Constrained (ty :: JType) c where
28 cRep :: Proxy ty -> Proxy c -> CRepr ty
34 cRep :: Proxy ty -> Proxy c -> CRepr ty
@@ -39,6 +45,9 @@ data Gt nat
39instance KnownNat nat => Constrained 'JNumber (Gt nat) where
45instance KnownNat nat => Constrained 'JNumber (Gt nat) where
40 cRep _ _ = Gt $ natVal (Proxy @nat)
46 cRep _ _ = Gt $ natVal (Proxy @nat)
48+instance KnownNat nat => Constrained 'JArray (LengthEqNat nat) where
49+ cRep _ _ = LengthArrEq $ natVal (Proxy @nat)
42-- /Validators
51-- /Validators
44data Field field ty schema
53data Field field ty schema
@@ -65,6 +74,13 @@ instance
65instance Verifiable jty '[] where
74instance Verifiable jty '[] where
66 schema _ _ = []
75 schema _ _ = []
78+ ( Constrained 'JArray c
79+ , Verifiable 'JArray cs )
80+ => Verifiable 'JArray (c ': cs) where
81+ schema _ _ = cRep (Proxy @'JArray) (Proxy @c)
82+ : schema (Proxy @'JArray) (Proxy @cs)
69 ( SchemaConstructor jty
85 ( SchemaConstructor jty
70 , Verifiable 'JObject fs
86 , Verifiable 'JObject fs
@@ -76,11 +92,6 @@ instance
76 fieldName = symbolVal (Proxy @field)
92 fieldName = symbolVal (Proxy @field)
77 fieldSchema = constructor (Proxy @jty) $ schema (Proxy @jty) (Proxy @cs)
93 fieldSchema = constructor (Proxy @jty) $ schema (Proxy @jty) (Proxy @cs)
79−type family CRepr (jty :: JType) :: * where
80− CRepr JText = TextConstraint
81− CRepr JNumber = NumberConstraint
82− CRepr JObject = (String, Schema)
84data TextConstraint
95data TextConstraint
85 = LengthEq Integer
96 = LengthEq Integer
86 | LengthLe Integer
97 | LengthLe Integer
@@ -93,11 +104,15 @@ data NumberConstraint
93 | Eq Integer
104 | Eq Integer
94 deriving (Show, Eq)
105 deriving (Show, Eq)
108+ = LengthArrEq Integer
96data Schema
111data Schema
97 = SchemaText [TextConstraint]
112 = SchemaText [TextConstraint]
98 | SchemaNumber [NumberConstraint]
113 | SchemaNumber [NumberConstraint]
99 | SchemaObject [(String, Schema)]
114 | SchemaObject [(String, Schema)]
100− | SchemaArray Schema
115+ | SchemaArray [ArrayConstraint] Schema
101 deriving (Show, Eq)
116 deriving (Show, Eq)
103-- | Apply constraint for each element of list
118-- | Apply constraint for each element of list
@@ -117,7 +132,7 @@ instance SchemaConstructor 'JNumber where
117instance SchemaConstructor 'JObject where
132instance SchemaConstructor 'JObject where
118 constructor _ = SchemaObject
133 constructor _ = SchemaObject
120−class (SchemaConstructor (JT ty)) => Schematic ty where
135+class Schematic ty where
121 type JT (ty :: k) :: JType
136 type JT (ty :: k) :: JType
122 build :: Proxy ty -> Schema
137 build :: Proxy ty -> Schema
@@ -131,9 +146,13 @@ data SNumber cs
132instance (Verifiable 'JNumber cs) => Schematic (SNumber cs) where
147instance (Verifiable 'JNumber cs) => Schematic (SNumber cs) where
133 type JT (SNumber cs) = 'JNumber
148 type JT (SNumber cs) = 'JNumber
134− build _ = constructor (Proxy @(JT (SNumber cs))) $ schema (Proxy @'JNumber) (Proxy @cs)
149+ build _ = constructor (Proxy @(JT (SNumber cs)))
150+ $ schema (Proxy @'JNumber) (Proxy @cs)
153+ ( Verifiable (JT ty) cs
154+ , SchemaConstructor (JT ty) )
136−instance (Verifiable (JT ty) cs, SchemaConstructor (JT ty)) => Schematic (Field field ty cs) where
155+ => Schematic (Field field ty cs) where
137 type JT (Field field ty cs) = JT ty
156 type JT (Field field ty cs) = JT ty
138 build _ = constructor (Proxy @(JT (Field field ty cs)))
157 build _ = constructor (Proxy @(JT (Field field ty cs)))
139 $ schema (Proxy @(JT (Field field ty cs))) (Proxy @cs)
158 $ schema (Proxy @(JT (Field field ty cs))) (Proxy @cs)
@@ -146,11 +165,25 @@ instance (Verifiable 'JObject els) => Schematic (SObject els) where
146 constructor (Proxy @(JT (SObject els)))
165 constructor (Proxy @(JT (SObject els)))
147 $ schema (Proxy @(JT (SObject els))) (Proxy @els)
166 $ schema (Proxy @(JT (SObject els))) (Proxy @els)
172+ , Verifiable 'JArray cs )
173+ => Schematic (SArray cs s) where
174+ type JT (SArray cs s) = 'JArray
177+ (schema (Proxy @'JArray) (Proxy @cs))
149-- | Shows it's top-level definition according to the json-schema
180-- | Shows it's top-level definition according to the json-schema
150class TopLevel spec
181class TopLevel spec
152instance Schematic (SObject els) => TopLevel (SObject els)
183instance Schematic (SObject els) => TopLevel (SObject els)
185+instance Schematic (SArray cs s) => TopLevel (SArray cs s)
154-- | TODO: abstract over objects and arrays
187-- | TODO: abstract over objects and arrays
155-- toSchema
188-- toSchema
156-- :: forall els (e :: [k]). (e ~ '[], Verifiable (JT (SObject els)) e, Schematic (SObject els))
189-- :: forall els (e :: [k]). (e ~ '[], Verifiable (JT (SObject els)) e, Schematic (SObject els))