Skip to content

Commit

Permalink
Set typeLoose to false in JSON Validator (close #122)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Oct 4, 2019
1 parent 3b37437 commit 4dae4f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,21 @@ object CirceValidator extends Validator[Json] {
private val IgluMetaschemaFactory =
JsonSchemaFactory.builder(JsonSchemaFactory.getInstance).addMetaSchema(IgluMetaschema).build()

private val SchemaValidatorsConfig: SchemaValidatorsConfig = {
val config = new SchemaValidatorsConfig()
// typeLoose is OpenAPI workaround to cast stringly typed properties
// e.g, with default true "5" string would validate against integer type
config.setTypeLoose(false)
config
};

private lazy val V4Schema =
JsonSchemaFactory.getInstance.getSchema(new ObjectMapper().readTree(V4SchemaText))

def validate(data: Json, schema: Json): Either[ValidatorError, Unit] =
Either
.catchNonFatal(IgluMetaschemaFactory.getSchema(circeToJackson(schema)))
.catchNonFatal(
IgluMetaschemaFactory.getSchema(circeToJackson(schema), SchemaValidatorsConfig))
.leftMap[ValidatorError](ValidatorError.schemaIssue) // Should never be reached in Client
.flatMap { schema =>
validateOnReadySchema(schema, data).leftMap(ValidatorError.InvalidData.apply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class RawValidationSpec extends Specification with DataTables {
additionalProperties invalidates unexpected property $e5
ipv4 format invalidates plain string $e6
validate integers bigger than java.lang.Long.MAX_VALUE $e7
validate null in [array, null] type $e8
invalidate stringly integer with integer type $e9
"""

val simpleSchemaResult: Json =
Expand Down Expand Up @@ -194,6 +196,18 @@ class RawValidationSpec extends Specification with DataTables {
def e7 = {
val schema = json"""{ "type": "integer" }"""
val input = json"""9223372036854775809"""
CirceValidator.validate(input, schema) must beRight(())
CirceValidator.validate(input, schema) must beRight
}

def e8 = {
val schema = json"""{ "type": ["array", "null"], "items": {"type": "object"} }"""
val input = json"""null"""
CirceValidator.validate(input, schema) must beRight
}

def e9 = {
val schema = json"""{ "type": "integer" }"""
val input = json""""5""""
CirceValidator.validate(input, schema) must beLeft
}
}

0 comments on commit 4dae4f1

Please sign in to comment.