@@ -115,7 +115,6 @@ object CirceValidator extends Validator[Json] {
115115 }
116116 }
117117
118- /** Validate instance against schema and return same instance */
119118 private def validateOnReadySchema (
120119 schema : JsonSchema ,
121120 instance : Json ,
@@ -190,6 +189,26 @@ object CirceValidator extends Validator[Json] {
190189 )
191190 }
192191
192+ /** Compile circe json to networknt schema */
193+ def compileJsonSchema (
194+ schema : Json ,
195+ maxJsonDepth : Int
196+ ): Either [ValidatorError .InvalidSchema , JsonSchema ] = {
197+ for {
198+ schemaAsNode <- circeToJackson(schema, maxJsonDepth).leftMap(_.toInvalidSchema)
199+ _ <- validateSchema(schemaAsNode)
200+ evaluated <- evaluateSchema(schemaAsNode)
201+ } yield evaluated
202+ }
203+
204+ private def validateSchema (schema : JsonNode ): Either [ValidatorError .InvalidSchema , Unit ] = {
205+ val issues = validateSchemaAgainstV4(schema)
206+ issues match {
207+ case Nil => Right (())
208+ case head :: tail => Left (ValidatorError .InvalidSchema (NonEmptyList (head, tail)))
209+ }
210+ }
211+
193212 private [client] object WithCaching {
194213
195214 /**
@@ -230,42 +249,14 @@ object CirceValidator extends Validator[Json] {
230249 case Some (alreadyEvaluatedSchema) =>
231250 alreadyEvaluatedSchema.pure[F ]
232251 case None =>
233- provideNewJsonSchema (schema, maxJsonDepth)
252+ compileJsonSchema (schema, maxJsonDepth)
234253 .pure[F ]
235254 .flatTap(result => evaluationCache.put((key, timestamp), result))
236255 }
237256 case ResolverResult .NotCached (SchemaItem (schema, _)) =>
238- provideNewJsonSchema (schema, maxJsonDepth).pure[F ]
257+ compileJsonSchema (schema, maxJsonDepth).pure[F ]
239258 }
240259 }
241260
242- private def provideNewJsonSchema (
243- schema : Json ,
244- maxJsonDepth : Int
245- ): Either [ValidatorError .InvalidSchema , JsonSchema ] = {
246- for {
247- schemaAsNode <- circeToJackson(schema, maxJsonDepth).leftMap(_.toInvalidSchema)
248- _ <- validateSchema(schemaAsNode)
249- evaluated <- evaluateSchema(schemaAsNode)
250- } yield evaluated
251- }
252-
253- private def validateSchema (schema : JsonNode ): Either [ValidatorError .InvalidSchema , Unit ] = {
254- val issues = V4Schema
255- .validate(schema)
256- .asScala
257- .toList
258- .map(m =>
259- ValidatorError .SchemaIssue (
260- Option (m.getInstanceLocation()).map(_.toString).getOrElse(" " ),
261- fromValidationMessage(m).message
262- )
263- )
264-
265- issues match {
266- case Nil => Right (())
267- case head :: tail => Left (ValidatorError .InvalidSchema (NonEmptyList (head, tail)))
268- }
269- }
270261 }
271262}
0 commit comments