@@ -7,9 +7,12 @@ module Data.Schematic
77 , module Data.Schematic.Utils
88 , decodeAndValidateJson
99 , parseAndValidateJson
10+ , parseAndValidateJsonBy
1011 , parseAndValidateVersionedJson
1112 , parseAndValidateTopVersionJson
1213 , decodeAndValidateVersionedJson
14+ , parseAndValidateWithMList
15+ , decodeAndValidateVersionedWithMList
1316 , isValid
1417 , isDecodingError
1518 , isValidationError
@@ -46,6 +49,13 @@ parseAndValidateJson v =
4649 Left em -> ValidationError em
4750 Right () -> Valid jsonRepr
4851
52+parseAndValidateJsonBy
53+ :: (J.FromJSON (JsonRepr schema), TopLevel schema, SingI schema)
54+ => proxy schema
55+ -> J.Value
56+ -> ParseResult (JsonRepr schema)
57+parseAndValidateJsonBy _ = parseAndValidateJson
58+
4959parseAndValidateTopVersionJson
5060 :: forall proxy (v :: Versioned)
5161 . (SingI (TopVersion (AllVersions v)))
@@ -93,6 +103,16 @@ parseAndValidateVersionedJson
93103 -> ParseResult (JsonRepr (Snd (Head (AllVersions v))))
94104parseAndValidateVersionedJson _ v = mparse (sing :: Sing (AllVersions v)) v
95105
106+parseAndValidateWithMList
107+ :: MList revisions
108+ -> J.Value
109+ -> ParseResult (JsonRepr (Head revisions))
110+parseAndValidateWithMList MNil v = parseAndValidateJson v
111+parseAndValidateWithMList ((:&&) p f tl) v = case parseAndValidateJsonBy p v of
112+ Valid a -> Valid a
113+ DecodingError _ -> f <$> parseAndValidateWithMList tl v
114+ ValidationError _ -> f <$> parseAndValidateWithMList tl v
115+
96116decodeAndValidateJson
97117 :: forall schema
98118 . (J.FromJSON (JsonRepr schema), TopLevel schema, SingI schema)
@@ -110,3 +130,16 @@ decodeAndValidateVersionedJson
110130decodeAndValidateVersionedJson vp bs = case decode bs of
111131 Nothing -> DecodingError "malformed json"
112132 Just x -> parseAndValidateVersionedJson vp x
133+
134+type family MapSnd (l :: [(a,k)]) = (r :: [k]) where
135+ MapSnd '[] = '[]
136+ MapSnd ( '(a, b) ': tl) = b ': MapSnd tl
137+
138+decodeAndValidateVersionedWithMList
139+ :: proxy versioned
140+ -> MList (MapSnd (AllVersions versioned))
141+ -> BL.ByteString
142+ -> ParseResult (JsonRepr (Head (MapSnd (AllVersions versioned))))
143+decodeAndValidateVersionedWithMList _ mlist bs = case decode bs of
144+ Nothing -> DecodingError "malformed json"
145+ Just x -> parseAndValidateWithMList mlist x