Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and Rules organisation #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions csv-validator-core/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ jarName in assembly := "meta-data-validator.jar"
mainClass in assembly := Some( "uk.gov.tna.dri.validator.MetaDataValidatorCommandLineApp" )

libraryDependencies ++= Seq(
"org.specs2" %% "specs2" % "1.12.3" % "test",
"org.scalatest" %% "scalatest" % "1.9.1" % "test"
"org.specs2" %% "specs2" % "1.12.3" % "test"
)

libraryDependencies ++= Seq(
Expand Down
129 changes: 0 additions & 129 deletions csv-validator-core/pom.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package uk.gov.tna.dri.schema

import scalaz.{Success => SuccessZ, Failure => FailureZ}

import scala.Some
import uk.gov.tna.dri.metadata.Row
import scala.util.Try

case class RegexRule(regex: String) extends Rule("regex") {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {

val regexp = if (columnDefinition.directives.contains(IgnoreCase())) "(?i)" + regex else regex
cellValue matches regexp
}

override def toError = {
s"""$ruleName("$regex")"""
}
}

case class InRule(inValue: ArgProvider) extends Rule("in", inValue) {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
val ruleValue = inValue.referenceValue(columnIndex, row, schema)

val (rv, cv) = if (columnDefinition.directives.contains(IgnoreCase())) (ruleValue.get.toLowerCase, cellValue.toLowerCase) else (ruleValue.get, cellValue)
rv contains cv
}
}

case class IsRule(isValue: ArgProvider) extends Rule("is", isValue) {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
val ruleValue = isValue.referenceValue(columnIndex, row, schema)

val (rv, cv) = if (columnDefinition.directives.contains(IgnoreCase())) (ruleValue.get.toLowerCase, cellValue.toLowerCase) else (ruleValue.get, cellValue)
cv == rv
}
}

case class IsNotRule(isNotValue: ArgProvider) extends Rule("isNot", isNotValue) {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
val ruleValue = isNotValue.referenceValue(columnIndex, row, schema)

val (rv, cv) = if (columnDefinition.directives.contains(IgnoreCase())) (ruleValue.get.toLowerCase, cellValue.toLowerCase) else (ruleValue.get, cellValue)
cv != rv
}
}

case class StartsRule(startsValue: ArgProvider) extends Rule("starts", startsValue) {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
val ruleValue = startsValue.referenceValue(columnIndex, row, schema)

val (rv, cv) = if (columnDefinition.directives.contains(IgnoreCase())) (ruleValue.get.toLowerCase, cellValue.toLowerCase) else (ruleValue.get, cellValue)
cv startsWith rv
}
}

case class EndsRule(endsValue: ArgProvider) extends Rule("ends", endsValue) {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
val ruleValue = endsValue.referenceValue(columnIndex, row, schema)

val (rv, cv) = if (columnDefinition.directives.contains(IgnoreCase())) (ruleValue.get.toLowerCase, cellValue.toLowerCase) else (ruleValue.get, cellValue)
cv endsWith rv
}
}

case class UriRule() extends PatternRule("uri", UriRegex)

case class Uuid4Rule() extends PatternRule("uuid4", Uuid4Regex)

case class PositiveIntegerRule() extends PatternRule("positiveInteger", PositiveIntegerRegex)

case class RangeRule(min: BigDecimal, max: BigDecimal) extends Rule("range") {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {

Try[BigDecimal]( BigDecimal(cellValue)) match {
case scala.util.Success(callDecimal) => if (callDecimal >= min && callDecimal <= max ) true else false
case _ => false
}
}

override def toError = s"""$ruleName($min,$max)"""
}

case class LengthRule(from: Option[String], to: String) extends Rule("length") {

def toValue: Int = if (to == "*") Int.MaxValue else to.toInt
def fromValue: Int = if (from.get == "*") 0 else from.get.toInt

def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
val cellLen = cellValue.length

from match {
case None => if ( to=="*") true else cellLen == to.toInt
case Some(_) => cellLen >= fromValue && cellLen <= toValue
}
}

override def toError = if(from.isDefined) s"""$ruleName(${from.get},$to)""" else s"""$ruleName($to)"""
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package uk.gov.tna.dri.schema
/*
* Copyright (c) 2013, The National Archives [email protected]
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2013, The National Archives [email protected]
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import scalax.file.{PathSet, Path}
import scalaz.Scalaz._
import scalaz.{Success => SuccessZ, Failure => FailureZ}
import java.io.{BufferedInputStream, FileInputStream, File}
import java.security.MessageDigest
import uk.gov.tna.dri.metadata.Row
import util.Try
import annotation.tailrec
import java.net.URI
import org.joda.time.{Interval, LocalTime, DateTime}
import org.joda.time.format.DateTimeFormat


trait DateParser {
def parse(dateStr: String): Try[DateTime]
}

object IsoDateParser extends DateParser {
def parse(dateStr: String): Try[DateTime] = Try(DateTime.parse(dateStr))
}

object UkDateParser extends DateParser {
val format = DateTimeFormat.forPattern(UkDateFormat)
def parse(dateStr: String): Try[DateTime] = Try(format.parseDateTime(dateStr))
}

object TimeParser extends DateParser {
def parse(dateStr: String) = Try(LocalTime.parse(dateStr).toDateTimeToday)
}

abstract class DateRangeRule(name: String, dateRegex: String, dateParser: DateParser) extends Rule(name) {
import dateParser.parse
val from: String
val to: String
lazy val fromDate = parse(from)
lazy val toDate = parse(to)

def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
cellValue matches dateRegex match {
case true => {
val inRange = for ( frmDt <- fromDate; toDt <- toDate; cellDt <- parse(cellValue)) yield {
val interval = new Interval(frmDt,toDt.plusMillis(1))
interval.contains(cellDt)
}

inRange.getOrElse(false)
}

case _ => false
}
}

override def toError = s"""$ruleName("$from, $to")"""
}

abstract class PatternRule(name: String, pattern: String) extends Rule(name) {
def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = cellValue matches pattern
}

abstract class DateRule(name: String, dateRegex: String, dateParser: DateParser) extends PatternRule(name, dateRegex) {
override def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema): Boolean = {
super.valid(cellValue, columnDefinition, columnIndex, row, schema) match {
case true => dateParser.parse(cellValue).isSuccess
case _ => false
}
}
}

case class XsdDateTimeRule() extends DateRule("xDateTime", XsdDateTimeRegex, IsoDateParser)

case class XsdDateTimeRangeRule(from: String, to: String) extends DateRangeRule("xDateTime", XsdDateTimeRegex, IsoDateParser)

case class XsdDateRule() extends DateRule("xDate", XsdDateRegex, IsoDateParser)

case class XsdDateRangeRule(from: String, to: String) extends DateRangeRule("xDate", XsdDateRegex, IsoDateParser)

case class UkDateRule() extends DateRule("ukDate", UkDateRegex, UkDateParser)

case class UkDateRangeRule(from: String, to: String) extends DateRangeRule("ukDate", UkDateRegex, UkDateParser)

case class XsdTimeRule() extends DateRule("xTime", XsdTimeRegex, TimeParser)

case class XsdTimeRangeRule(from: String, to: String) extends DateRangeRule("xTime", XsdTimeRegex, TimeParser)

case class PartUkDateRule() extends PatternRule("partUkDate", PartUkDateRegex)

Loading