Giter VIP home page Giter VIP logo

scala-validation's Introduction

scala-validation

A simple library for object validations

This library is under development, so, any help is welcome. ;)

Set up

Note: This library targets only Scala 2.12
resolvers += "Scala Validation Releases" at "http://dl.bintray.com/scala-validation/releases"

libraryDependencies += "org.scala.validation" %% "core" % "0.0.5"

Simple example

import org.validation.scala._
import org.validation.scala.matchers._

case class Person(name: String,
                  occupation: String,
                  age: Int,
                  address: Address,
                  phones: List[Phone],
                  favoriteNumbers: List[Int])

case class Address(address: String)

case class Phone(phoneNumber: String)


object validators {
  implicit val addressValidator = Validator { address: Address =>
    assure(address.address is notBlank) { "address cannot be empty" }
  }

  implicit val phoneValidator = Validator { phone: Phone =>
    assure(phone.phoneNumber is notBlank) { "phone number be null" } ~
    assure(phone.phoneNumber.length is higherOrEqualTo(8)) { s"phone number must have at least 8 numbers and ${phone.phoneNumber} has ${phone.phoneNumber.length}" }
  }

  implicit val personValidator = Validator { person: Person =>
    assure(person.name is notNull) { "name cannot be null" } ~
    assure(person.occupation is notBlank) { "occupation cannot be null" } ~
    assure(person.age is higherThan(0)) { "age cannot be lower than 1" } ~
    assure(person.favoriteNumbers contains 4) { "favorite numbers must contain number 4" } ~
    assureEntity { person.address } ~
    assureEntities { person.phones }
  }
}

object Main extends App {
  import validators._

  val phones = List(Phone("88888888"), Phone("999999999"), Phone("7777777"))
  val address = Address("")
  val person = Person(null, null, 1, address, phones, List(3, 2, 1))

  val result = validate(person)

  result.foreach { v =>
    println(v.message)
  }

  /** it will be printed:
    
     favorite numbers must contain number 4
     name cannot be null
     occupation cannot be null
     address cannot be empty
     phone number must have at least 8 numbers and 7777777 has 7
  */
}

Validating with paths

If you need to, you can provide "paths" to help you find where the violation occurred.

Basically, the semantics are:

//for a single value
assure("path" ~> boolean) { violationMessage }
//for another entity
assureEntity { "path" ~> entity }
//for an iterable
assureEntities { "path" ~> entities }
//base path on declaring a validator
Validator("basePath") { entity: T => ... }
//or on validating:
validate("basePath" ~> entity)

Example:

import org.validation.scala._
import org.validation.scala.matchers._

case class Person(name: String,
                  occupation: String,
                  age: Int,
                  address: Address,
                  phones: List[Phone],
                  favoriteNumbers: List[Int])

case class Address(address: String)

case class Phone(phoneNumber: String)


object validators {
  implicit val addressValidator = Validator { address: Address =>
    assure("address" ~> (address.address is notBlank)) { "address cannot be empty" }
  }

  implicit val phoneValidator = Validator { phone: Phone =>
    assure("phoneNumber" ~> (phone.phoneNumber is notBlank)) { "phone number be null" } ~
    assure("phoneNumber" ~> (phone.phoneNumber.length is higherOrEqualTo(8))) { s"phone number must have at least 8 numbers and ${phone.phoneNumber} has ${phone.phoneNumber.length}" }
  }

  implicit val personValidator = Validator { person: Person =>
    assure("name" ~> (person.name is notNull)) { "name cannot be null" } ~
    assure("occupation" ~> (person.occupation is notBlank)) { "occupation cannot be null" } ~
    assure("age" ~> (person.age is higherThan(0))) { "age cannot be lower than 1" } ~
    assure("favoriteNumbers" ~> (person.favoriteNumbers contains 4)) { "favorite numbers must contain number 4" } ~
    assureEntity { "address" ~> person.address } ~
    assureEntities { "phones" ~>  person.phones }
  }
}

object Main extends App {
  import validators._

  val phones = List(Phone("88888888"), Phone("999999999"), Phone("7777777"))
  val address = Address("")
  val person = Person(null, null, 1, address, phones, List(3, 2, 1))

  val result = validate("person" ~> person)

  result.foreach { v =>
    println(s"${v.path} - ${v.message}")
  }

  /** it will be printed:

      Some(person.favoriteNumbers) - favorite numbers must contain number 4
      Some(person.name) - name cannot be null
      Some(person.occupation) - occupation cannot be null
      Some(person.address.address) - address cannot be empty
      Some(person.phones[2].phoneNumber) - phone number must have at least 8 numbers and 7777777 has 7
  */
}

scala-validation's People

Contributors

gabfssilva avatar

Stargazers

Khemtat L. avatar Gabriel Ozeas de Oliveira avatar Bruno avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.