Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

Validate XML : error on elem disapeared with Node #76

Closed
gbersac opened this issue Feb 14, 2017 · 2 comments
Closed

Validate XML : error on elem disapeared with Node #76

gbersac opened this issue Feb 14, 2017 · 2 comments

Comments

@gbersac
Copy link

gbersac commented Feb 14, 2017

I stumbled upon a puzzling error. With a From[Elem] in this code :

case class Commentaire(
  codeMessage: Option[String],
  libelleMessage: String,
  codePriorite: Option[String]
)

object Commentaire {
  implicit val messageRule: Rule[Elem, Commentaire] = From[Elem] { __ =>
    import jto.validation.xml.Rules._

    (
      (__ \ "DossEtude"  \ "codeMessage").read[Option[String]] ~
      (__ \ "DossEtude" \ "libelleMessage").read[String] ~
      (__ \ "DossEtude" \ "prioriteMessage").read[Option[String]]
    )(Commentaire.apply _)
  }

}

I got the following error :

[error] /Users/gbe/projects/nfc-nb/commons/server/src/main/scala/models/OMDEMessage.scala:92: No implicit view available from jto.validation.Path => jto.validation.RuleLike[scala.xml.Elem,Option[String]].
[error]       (__ \ "DossEtude" \ "codeMessage").read[Option[String]] ~

This error disepeared when I replaced Elem by Node :

object Commentaire {
  implicit val fmt = Json.format[Commentaire]

  implicit val messageRule: Rule[Node, Commentaire] = From[Node] { __ =>
    (
      (__ \ "DossEtude" \ "codeMessage").read[Option[String]] ~
      (__ \ "DossEtude" \ "libelleMessage").read[String] ~
      (__ \ "DossEtude" \ "prioriteMessage").read[Option[String]]
    )(Commentaire.apply _)
  }

}

Whether this library should completely support Elem or the user should be notified that the type Elem is not supported by this library.

@OlivierBlanvillain
Copy link
Collaborator

The only thing we could do here is improve the error message. Indeed, the library does support Elem! You would just need to implement a few type class instances for this type (as the error suggests), see this section of the doc.

For better error messages, we would need to replace usages of Function1[A, B] with an equivalant custom type (ValidationView[A, B] or something), and annotate it with an @implicitNotFound.

@jto
Copy link
Owner

jto commented Feb 22, 2017

Thanks for reporting this.

Creating a new type ValidationView would break compatibility so maybe for validation v3 but not in v2. We could maybe however support Elem too.
As always, PR are welcome @gbersac :)

BTW, there's an extensive test suite here that may help if you need examples of XML validation.

@jto jto closed this as completed Feb 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants