Giter Club home page Giter Club logo

pariksha's Introduction

Pariksha.

Build Status

A scala library for validation.

The protagonist of our story is the Validator[T] trait which validates instances of T by using a list of Validation[T].

Consider a simple case class

case class Employee(name: String, age: Int)

We can define a list of Validation for this type

import pariksha.dsl_

implicit val validations = validator[Employee]
                    .check(_.name.nonEmpty, "name must not be empty")
                    .check(_.age > 18, "age must be above 18")
                    .check(_.name != "Bob Vance", "He owns Vance Refrigeration and is not an employee")

And then we can validate any instance of Employee type. All we need is Validator[Employee] implicitly in scope

import pareeskha.dsl_

val employee = Employee("Jim Halpert", 30)

employee.validate

validate returns a ValidationResult which can have two possible values

  • Valid
  • Invalid

Everybody knows Jenna Fischer from the office!

val beesly = Employee("Pam Beesly", 28)

beesly.validate == Valid(beesly)

And for an invalid employee

val bob = Employee("Bob Vance", 45)

bob.validate == Invalid(bob, List(ValidationError("He owns Vance Refrigeration and is not an employee")))

Nested Validations

When we have a type that contains another type, and we already have a Validator for the nested type, we can use the existing validator and delegate to that.

case class Manager(name: String, age: Int)

case class Office(manager: Manager, region: String)

We could define validations for Manager to be used in multiple places

val validations = validator[Manager]
    .check(_.name.nonEmpty, msgNameEmpty)
    .check(_.age > 25, msgAgeInvalid)

Then we we can define validations for the Office class and use the previous validator automatically for the manager field, assuming it is available as an implicit in the current scope.

Note, how we use the validate method on the contained type.

val validations = validator[Office]
                    .check(_.manager.validate)
                    .check(_.region.nonEmpty, msgRegionNonEmpty)

val validManager = Manager("Michael", 35)

val office = Office(validManager, "Scranton")

office.validate == Valid(office)

Fail Fast Validations

Sometimes it is desirable to not run all validations exhaustively but rather stop on first failed validation.

We can use the validateFailFast method on a type T . The requirements remain the same with a presence of Validator[T] needed.

val manager = Manager("", 18)

val validations = validator[Manager]
    .check(_.name.nonEmpty, msgNameEmpty)
    .check(_.age > 25, msgAgeInvalid)
    
manager.validate == Invalid(manager, List(
          ValidationError(Manager.msgNameEmpty),
          ValidationError(Manager.msgAgeInvalid)
        ))    

manager.validateFailFast == Invalid(manager, List(
                                      ValidationError(Manager.msgNameEmpty))
                                      

The second check is not even called in this case as the first one had failed. This is useful when the validations are resource/time consuming and we would like to stop at the first sign of problems.

Next goals :
  • Provide validation helpers like not null on all fields
  • ValidationRule to support intake of exceptions
  • Support for effects types like IO, ZIO etc.

pariksha's People

Contributors

ayushing avatar ayushworks 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.