<commit_before>package cz.kamenitxan.jakon.validation

import cz.kamenitxan.jakon.Circe.ParsedValue
import cz.kamenitxan.jakon.logging.Logger
import cz.kamenitxan.jakon.webui.entity.{Message, MessageSeverity}
import spark.Request

import java.lang.annotation.Annotation
import java.lang.reflect.Field
import scala.io.Source


object EntityValidator {

	def validate(prefix: String, validatedData: Map[Field, String]): Either[Seq[Message], Map[Field, String]] = {
		val errors = validatedData.filter(f => {
			val anns = f._1.getDeclaredAnnotations
			anns.exists(a => a.annotationType().getAnnotation(classOf[ValidatedBy]) != null)
		}).flatMap(f => {
			val anns = f._1.getDeclaredAnnotations.filter(a => a.annotationType().getAnnotation(classOf[ValidatedBy]) != null).toSeq
			validateField(prefix, f._1, anns, f._2, validatedData)
		}).toSeq

		if (errors.isEmpty) {
			Right(validatedData)
		} else {
			Left(errors)
		}
	}
	
	// TODO: change name?
	def validateJson(prefix: String, validatedData: Map[Field, ParsedValue]): Either[Seq[Message], Map[Field, ParsedValue]] = {
		val errors = validatedData.map(data => {
			val constructor = data._1.getDeclaringClass.getDeclaredConstructors.head
			val parameterIndex = constructor.getParameters.zipWithIndex.find(pi => {
				pi._1.getName == data._1.getName
			}).map(_._2).get
			val anns: Array[Annotation] = constructor.getParameterAnnotations()(parameterIndex)
			(data._1, data._2, anns)
		}).filter(data => {
			data._3.exists(a => a.annotationType().getAnnotation(classOf[ValidatedBy]) != null)
		}).flatMap(data => {
			// TODO: nested objects validation
			val oldFormatData = validatedData.map(data => (data._1, data._2.stringValue))
			val anns = data._3.filter(_.annotationType().getAnnotation(classOf[ValidatedBy]) != null)
			validateField(prefix, data._1, anns.toSeq, data._2.stringValue, oldFormatData)
		}).toSeq
<commit_msg>scala 3.2.0<commit_after>package cz.kamenitxan.jakon.validation

import cz.kamenitxan.jakon.Circe.ParsedValue
import cz.kamenitxan.jakon.logging.Logger
import cz.kamenitxan.jakon.webui.entity.{Message, MessageSeverity}
import spark.Request

import java.lang.annotation.Annotation
import java.lang.reflect.{Constructor, Field}
import scala.io.Source


object EntityValidator {

	def validate(prefix: String, validatedData: Map[Field, String]): Either[Seq[Message], Map[Field, String]] = {
		val errors = validatedData.filter(f => {
			val anns = f._1.getDeclaredAnnotations
			anns.exists(a => a.annotationType().getAnnotation(classOf[ValidatedBy]) != null)
		}).flatMap(f => {
			val anns = f._1.getDeclaredAnnotations.filter(a => a.annotationType().getAnnotation(classOf[ValidatedBy]) != null).toSeq
			validateField(prefix, f._1, anns, f._2, validatedData)
		}).toSeq

		if (errors.isEmpty) {
			Right(validatedData)
		} else {
			Left(errors)
		}
	}
	
	// TODO: change name?
	def validateJson(prefix: String, validatedData: Map[Field, ParsedValue]): Either[Seq[Message], Map[Field, ParsedValue]] = {
		val errors = validatedData.map(data => {
			val constructor: Constructor[_] = data._1.getDeclaringClass.getDeclaredConstructors.head
			val parameterIndex = constructor.getParameters.zipWithIndex.find(pi => {
				//Logger.warn(s"${pi._1.getName} - ${data._1.getName} : ${pi._1.getName == data._1.getName}")
				pi._1.getName == data._1.getName
			}).map(_._2)
			if (parameterIndex.isEmpty) {
				Logger.critical("Failed to find constructor param")
				(data._1, data._2, Array.empty[Annotation])
			} else {
				val anns: Array[Annotation] = constructor.getParameterAnnotations()(parameterIndex.get)
				(data._1, data._2, anns)
			}

		}).filter(data => {
			data._3.exists(a => a.annotationType().getAnnotation(classOf[ValidatedBy]) != null)
		}).flatMap(data => {
			// TODO: nested objects validation
			val oldFormatData = validatedData.map(data => (data._1, data._2.stringValue))
			val anns = data._3.filter(_.annotationType().getAnnotation(classOf[ValidatedBy]) != null)
			validateField(prefix, data._1, anns.toSeq, data._2.stringValue, oldFormatData)
		}).toSeq

{'fraction_non_alphanumeric': 0.14513752455795678, 'fraction_numerical': 0.01006876227897839, 'mean_word_length': 4.777304964539007, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6964722', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1341, 'n_words': 236}