<commit_before>package io.flow.lib.apidoc.json.validation

import com.bryzek.apidoc.spec.v0.models.Service
import com.bryzek.apidoc.spec.v0.models.json._
import io.flow.v0.models.{Address, CardForm, EventType, ItemForm, WebhookForm}
import io.flow.v0.models.json._
import play.api.libs.json._
import org.scalatest.{FunSpec, Matchers}

class ApidocServiceSpec extends FunSpec with Matchers {

  lazy val service = {
    val contents = scala.io.Source.fromFile("src/test/resources/flow-api-service.json", "UTF-8").getLines.mkString("\n")
    ApidocService(
      "test",
      Json.parse(contents).as[Service]
    )
  }

  it("fromUrl") {
    ApidocService.fromUrl("file://non-existent-tmp").left.getOrElse {
      sys.error("Expected error from invalid url")
    }

    val url = "http://apidoc.me/bryzek/apidoc-common/latest/service.json"
    val service = ApidocService.fromUrl(url) match {
      case Left(errors) => sys.error(s"Failed to load service from url[$url]: $errors")
      case Right(s) => s
    }
    service.name should be("apidoc common")
  }

  it("typeFromPath") {
    service.typeFromPath("POST", "/foo") should be(None)
    service.typeFromPath("POST", "/users") should be(Some("user_form"))
    service.typeFromPath("POST", "/:organization/webhooks") should be(Some("webhook_form"))
  }

  it("validate") {
    service.validate(
      "POST",
      "/:organization/webhooks",
      Json.obj("url" -> "https://test.flow.io")
    ) should equal(
      Left(Seq("Missing required field for type 'webhook_form': 'events'"))
    )
  }
 

}<commit_msg>Better error<commit_after>package io.flow.lib.apidoc.json.validation

import com.bryzek.apidoc.spec.v0.models.Service
import com.bryzek.apidoc.spec.v0.models.json._
import io.flow.v0.models.{Address, CardForm, EventType, ItemForm, WebhookForm}
import io.flow.v0.models.json._
import play.api.libs.json._
import org.scalatest.{FunSpec, Matchers}

class ApidocServiceSpec extends FunSpec with Matchers {

  lazy val service = {
    val contents = scala.io.Source.fromFile("src/test/resources/flow-api-service.json", "UTF-8").getLines.mkString("\n")
    ApidocService(
      "test",
      Json.parse(contents).as[Service]
    )
  }

  it("fromUrl") {
    ApidocService.fromUrl("file://non-existent-tmp").left.getOrElse {
      sys.error("Expected error from invalid url")
    }

    val url = "http://apidoc.me/bryzek/apidoc-common/latest/service.json"
    val result = ApidocService.fromUrl(url) match {
      case Left(errors) => sys.error(s"Failed to load service from url[$url]: $errors")
      case Right(s) => s
    }
    result.service.name should be("apidoc common")
  }

  it("typeFromPath") {
    service.typeFromPath("POST", "/foo") should be(None)
    service.typeFromPath("POST", "/users") should be(Some("user_form"))
    service.typeFromPath("POST", "/:organization/webhooks") should be(Some("webhook_form"))
  }

  it("validate") {
    service.validate(
      "POST",
      "/:organization/webhooks",
      Json.obj("url" -> "https://test.flow.io")
    ) should equal(
      Left(Seq("Missing required field for type 'webhook_form': 'events'"))
    )
  }
 

}
{'fraction_non_alphanumeric': 0.15769353297228417, 'fraction_numerical': 0.0031857279388340237, 'mean_word_length': 3.597364568081991, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3957466', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1058, 'n_words': 187}