Skip to content

Latest commit

 

History

History

baleen-json-jackson

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Baleen JSON Validation

Installation

Gradle

implementation("com.shoprunner:baleen-json-jackson:$baleen_version")

Example

import com.shoprunner.baleen.Baleen.describeAs
import com.shoprunner.baleen.DataTrace
import com.shoprunner.baleen.ValidationError
import com.shoprunner.baleen.types.StringType
import com.shoprunner.baleen.types.LongType
import com.shoprunner.baleen.json.JsonUtil
        
val employeeDescription = "Employee".describeAs {
    "id".type(LongType(min = 1, max = 6500), required = true)
    "name".type(StringType(min = 1, max = 500), required = true)
}

JsonUtil.validate(
    employeeDescription,
    DataTrace("employee object"),
    """
    {
        "id": "35",
        "name": ""
    }
    """.byteInputStream())
    .results.filterIsInstance<ValidationError>().forEach {
        println("${it.dataTrace.toList().joinToString("/")} ${it.message} ${it.dataTrace.tags}")
    }

produces

employee object/attribute "id" is not a long {line=3, column=20}
employee object/attribute "name" is not at least 1 characters {line=4, column=22}