<commit_before>package io.flow.lint.linters

import io.flow.lint.Linter
import com.bryzek.apidoc.spec.v0.models.{Service, Union}

/**
  * Validates that every union type must have a discriminator with
  * value 'discriminator'
  */
case object UnionTypesHaveCommonDiscriminator extends Linter with Helpers {

  private[this] val StandardName = "discriminator"
  private[this] val ErrorName = "code"

  override def validate(service: Service): Seq[String] = {
    service.unions.flatMap(validateUnion(_))
  }

  def validateUnion(union: Union): Seq[String] = {
    val target = union.name.endsWith("_error") match {
      case true => ErrorName
      case false => StandardName
    }

    union.discriminator match {
      case None => {
        Seq(error(union, s"Must have a discriminator with value '$target'"))
      }
      case Some(actual) => {
        (actual == target) match {
          case true => Nil
          case false => Seq(error(union, s"Discriminator must have value '$target' and not '$actual'"))
        }
      }
    }
  }

}

<commit_msg>Hack for localized_price.key union type (#94)<commit_after>package io.flow.lint.linters

import io.flow.lint.Linter
import com.bryzek.apidoc.spec.v0.models.{Service, Union}

/**
  * Validates that every union type must have a discriminator with
  * value 'discriminator'
  */
case object UnionTypesHaveCommonDiscriminator extends Linter with Helpers {

  private[this] val StandardName = "discriminator"
  private[this] val ErrorName = "code"

  override def validate(service: Service): Seq[String] = {
    service.unions.flatMap(validateUnion(_))
  }

  def validateUnion(union: Union): Seq[String] = {
    val target = union.name.endsWith("_error") match {
      case true => ErrorName
      case false => union.name == "localized_price" match {
        case true => "key" // hack for now for catalog.localized_price...
        case false => StandardName
      }
    }

    union.discriminator match {
      case None => {
        Seq(error(union, s"Must have a discriminator with value '$target'"))
      }
      case Some(actual) => {
        (actual == target) match {
          case true => Nil
          case false => Seq(error(union, s"Discriminator must have value '$target' and not '$actual'"))
        }
      }
    }
  }

}
{'fraction_non_alphanumeric': 0.11628919860627178, 'fraction_numerical': 0.0017421602787456446, 'mean_word_length': 2.980935875216638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '5219798', 'n_tokens_mistral': 708, 'n_tokens_neox': 689, 'n_words': 188}