Skip to content

Json validation for Circe

Notifications You must be signed in to change notification settings

tabmo/circe-validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

circe-validation

Build Status
Gitter

Overview

Circe Validation is a library which provides a rules mechanism for decoding circe Json.
This library is highly inspired by jto-validation

Getting Started

Circe Validation is currently available 2.1X
Import Circe Validation library, by adding this dependency in your build.sbt

Scala <2.13

"io.tabmo" %% "circe-validation-core" % "0.0.6"

Scala 2.13 version :

"io.tabmo" %% "circe-validation-core" % "0.1.0"

and the following resolver

"Tabmo Myget Public" at "https://www.myget.org/F/tabmo-public/maven/"

If you require some other functionality, you can pick-and-choose from amongst these modules :

  • circe-validation-extra-rules : Rules rules for Integer, String & Date
  • circe-validation-extra-rules-joda : Aditional rules for Joda

Example

Using existing rules

  
import io.circe.syntax._  
import io.tabmo.json.rules._  
  
case class Person(firstName: String, lastName: String, age: Int, email: String, dateOfBirth: Date, nickname: Option[String])  
  
val decodePerson: Decoder[Person] = Decoder.instance[Person] { (c: Hcursor) =>  
  for {
    name        <- c.downField("name").read(StringRules.maxLength(32))
    lastName    <- c.downField("lastName").as[String]
    age         <- c.downField("age").read(IntRules.positive())
    email       <- c.downField("email").read(StringRules.email)
    dateOfBirth <- c.downField("dateOfBirth").read(DateRules.date)
    nickname    <- c.downField("nickname").readOpt(StringRules.maxLength(32))
  } yield Person(name, lastName, age, email, dateOfBirth, nickname)
}  
  
val personJson = Json.obj(  
  "name" -> "Kevin".asJson,  
  "lastName" -> "Mg".asJson,  
  "age" -> 24.asJson,  
  "email" -> "email@email.com".asJson,  
  "dateOfBirth" -> "1994-02-16".asJson,
  "nickname" -> Json.Null  
)  
 println(decodePerson.decodeJson(personJson)) //Right(Person(Kevin,Mg,24,email@email.com,Wed Feb 16 00:00:00 CET 1994, None))  
  

Compose multiple rules

It's possible to compose multiple rules with |+| method.

import io.circe.syntax._  
import io.tabmo.json.rules._  
  
case class Person(firstName: String, lastName: String, age: Int)  
  
val decodePerson: Decoder[Person] = Decoder.instance[Person] { (c: Hcursor) =>  
  for {  
    name      <- c.downField("name").read(StringRules.maxLength(32) |+| StringRules.isNotEmpty())  
    lastName  <- c.downField("lastName").as[String]  
    age       <- c.downField("age").read(IntRules.positive())  
  } yield Person(name, lastName, age)  
}  
  
val personJson = Json.obj(  
  "name" -> "Kevin".asJson,  
  "lastName" -> "Mg".asJson,  
  "age" ->  24.asJson  
)  
 println(decodePerson.decodeJson(personJson)) //Right(Person(Kevin,Mg,24))  

Apply a transformation

You can use a transformation rule

import io.circe.syntax._  
import io.tabmo.json.rules._  
  
case class Person(firstName: String, lastName: String, age: Int)  
  
val decodePerson: Decoder[Person] = Decoder.instance[Person] { (c: HCursor) =>  
  for {  
    name      <- c.downField("name").read(StringRules.maxLength(32) |+| StringRules.toUpperCase())  
    lastName  <- c.downField("lastName").read(StringRules.toUpperCase)  
    age       <- c.downField("age").read(IntRules.positive())  
  } yield Person(name, lastName, age)  
}  
  
val personJson = Json.obj(  
  "name" -> "Kevin".asJson,  
  "lastName" -> "Mg".asJson,  
  "age" ->  24.asJson  
)  
 println(decodePerson.decodeJson(personJson)) //Right(Person(KEVIN,MG,24))  

Using rules for list/map/set/array

  
import io.circe.syntax._  
import io.tabmo.json.rules._  
  
case class Person(firstName: String, lastName: String, age: Int, carList: Seq[String])  
  
val decodePerson: Decoder[Person] = Decoder.instance[Person] { (c: Hcursor) =>  
  for {  
    name      <- c.downField("name").read(StringRules.maxLength(32))  
    lastName  <- c.downField("lastName").as[String]  
    age       <- c.downField("age").read(IntRules.positive())  
    cars      <- c.downField("cars").readSeq(StringRules.toUpperCase |+| StringRules.maxLength(32))  
  } yield Person(name, lastName, age, cars)  
}  
  
val personJson = Json.obj(  
  "name" -> "Kevin".asJson,  
  "lastName" -> "Mg".asJson,  
  "age" ->  24.asJson,  
  "cars" -> ("Renault", "Mercedes").asJson  
)  
 println(decodePerson.decodeJson(personJson)) //Right(Person(Kevin,Mg,24,List(RENAULT, MERCEDES)))  

Create a custom rule

It's possible to create a custom rule easily with the trait GenericRules

import cats.data.Validated.Valid  
import io.tabmo.json.rules.{GenericRules, Rule}  
  
object HelloRules extends GenericRules {  
    def replaceHiByEmoji: Rule[String, String] = Rule((str: String) => { Valid(str.replaceAll("Hi", ":wave:")) })  
  
    def sayHello(errorCode: String = "error.say.hello.:rage:"): Rule[String, String] =  
      validateWith[String](errorCode)(_.contains("Hello"))  
 }  

Maintainers

List of current maintainers :

Copyright & License

Circe Validation is licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.