Skip to content

Commit

Permalink
include dynamicPath in toJSON() output
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Dec 13, 2024
1 parent 16ac338 commit 085350a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class ValidationFailure(
val json = mutableMapOf<JsonString, JsonValue>(
JsonString("instanceRef") to instanceRef,
JsonString("schemaRef") to JsonString(schema.location.pointer.toString()),
JsonString("dynamicPath") to JsonString(dynamicPath.toString()),
JsonString("message") to JsonString(message)
)
keyword?.let { json[JsonString("keyword")] = JsonString(it.value) }
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/erosb/jsonsKema/Validator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private class DefaultValidator(
return rval
}

override fun visitAdditionalPropertiesSchema(schema: AdditionalPropertiesSchema): ValidationFailure? =
override fun visitAdditionalPropertiesSchema(schema: AdditionalPropertiesSchema): ValidationFailure? = inPathSegment(Keyword.ADDITIONAL_PROPERTIES) {
instance.maybeObject { obj ->
var endResult: ValidationFailure? = null
obj.properties
Expand All @@ -395,6 +395,7 @@ private class DefaultValidator(
}
endResult
}
}

override fun visitMaxLengthSchema(schema: MaxLengthSchema): ValidationFailure? = inPathSegment(Keyword.MAX_LENGTH) {
instance.maybeString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,40 @@ class PropertiesValidationTest {
val expected = JsonParser(
"""
{
"instanceRef": "#",
"schemaRef": "#",
"message": "multiple validation failures",
"causes": [
{
"message": "expected type: array, actual: boolean",
"schemaRef": "#/properties/arrProp/type",
"instanceRef": "#/arrProp",
"keyword": "type"
},
{
"message": "expected type: object, actual: null",
"schemaRef": "#/properties/objProp/type",
"instanceRef": "#/objProp",
"keyword": "type"
},
{
"instanceRef": "#/strProp",
"schemaRef": "#/properties/strProp/type",
"message": "expected type: string, actual: integer",
"keyword": "type"
},
{
"instanceRef": "#/realObjProp/nestedProp",
"schemaRef": "#/properties/realObjProp/properties/nestedProp/type",
"message": "expected type: string, actual: integer",
"keyword": "type"
}
]
"instanceRef": "#",
"schemaRef": "#",
"dynamicPath": "#",
"message": "multiple validation failures",
"causes": [
{
"instanceRef": "#/arrProp",
"schemaRef": "#/properties/arrProp/type",
"dynamicPath": "#/properties/arrProp/type",
"message": "expected type: array, actual: boolean",
"keyword": "type"
},
{
"instanceRef": "#/objProp",
"schemaRef": "#/properties/objProp/type",
"dynamicPath": "#/properties/objProp/type",
"message": "expected type: object, actual: null",
"keyword": "type"
},
{
"instanceRef": "#/strProp",
"schemaRef": "#/properties/strProp/type",
"dynamicPath": "#/properties/strProp/type",
"message": "expected type: string, actual: integer",
"keyword": "type"
},
{
"instanceRef": "#/realObjProp/nestedProp",
"schemaRef": "#/properties/realObjProp/properties/nestedProp/type",
"dynamicPath": "#/properties/realObjProp/properties/nestedProp/type",
"message": "expected type: string, actual: integer",
"keyword": "type"
}
]
}
"""
)()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TypeValidationTest {
{
"instanceRef": "#",
"schemaRef": "#/type",
"dynamicPath": "#/type",
"message": "expected type: $typeKeywordValue, actual: $actualType",
"keyword": "type"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,26 @@ class ValidationFailureTest {
"instanceRef": "#",
"schemaRef": "#/additionalProperties",
"message": "multiple validation failures",
"dynamicPath": "#/additionalProperties",
"causes": [
{
"instanceRef": "#/customerName",
"schemaRef": "#/additionalProperties",
"dynamicPath": "#/additionalProperties/false",
"message": "false schema always fails",
"keyword": "false"
},
{
"instanceRef": "#/acquireDate",
"schemaRef": "#/additionalProperties",
"dynamicPath": "#/additionalProperties/false",
"message": "false schema always fails",
"keyword": "false"
},
{
"instanceRef": "#",
"schemaRef": "#/required",
"dynamicPath": "#/required",
"message": "required properties are missing: age",
"keyword": "required"
}
Expand Down
95 changes: 54 additions & 41 deletions src/test/kotlin/com/github/erosb/jsonsKema/ValidatorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import org.junit.jupiter.api.Assertions.assertSame
import org.junit.jupiter.api.Test

class ValidatorTest {

@Test
fun `ValidationFailure props`() {
val schema = CompositeSchema(
subschemas = setOf(
MinLengthSchema(5, SourceLocation(5, 5, JsonPointer(listOf("parent", "minLength"))))
),
UnknownSource
)
val schema =
CompositeSchema(
subschemas =
setOf(
MinLengthSchema(5, SourceLocation(5, 5, JsonPointer(listOf("parent", "minLength")))),
),
UnknownSource,
)
val instance = JsonParser(" \"hey\"")()
val actual = Validator.forSchema(schema).validate(instance)!!

assertEquals("actual string length 3 is lower than minLength 5", actual.message)
assertSame(schema.subschemas.stream().findFirst().get(), actual.schema)
assertSame(
schema.subschemas
.stream()
.findFirst()
.get(),
actual.schema,
)
assertSame(instance, actual.instance)
assertSame(Keyword.MIN_LENGTH, actual.keyword)
}
Expand All @@ -28,14 +35,16 @@ class ValidatorTest {
val minLengthSchema = MinLengthSchema(5, SourceLocation(5, 5, JsonPointer(listOf())))
val maxLengthSchema = MaxLengthSchema(3, UnknownSource)
val falseSchema = FalseSchema(UnknownSource)
val schema = CompositeSchema(
subschemas = setOf(
minLengthSchema,
maxLengthSchema,
falseSchema
),
location = UnknownSource
)
val schema =
CompositeSchema(
subschemas =
setOf(
minLengthSchema,
maxLengthSchema,
falseSchema,
),
location = UnknownSource,
)
val instance = JsonParser(" \"heyy\"")()
val actual = Validator.forSchema(schema).validate(instance)!!
assertEquals("multiple validation failures", actual.message)
Expand All @@ -44,33 +53,37 @@ class ValidatorTest {
JsonParser(
"""
{
"instanceRef": "#",
"schemaRef": "#",
"message": "multiple validation failures",
"causes": [
{
"instanceRef": "#",
"schemaRef": "#",
"message": "actual string length 4 is lower than minLength 5",
"keyword": "minLength"
},
{
"instanceRef": "#",
"schemaRef": "#",
"message": "actual string length 4 exceeds maxLength 3",
"keyword": "maxLength"
},
{
"instanceRef": "#",
"schemaRef": "#",
"message": "false schema always fails",
"keyword": "false"
}
]
"instanceRef": "#",
"schemaRef": "#",
"dynamicPath": "#",
"message": "multiple validation failures",
"causes": [
{
"instanceRef": "#",
"schemaRef": "#",
"dynamicPath": "#/minLength",
"message": "actual string length 4 is lower than minLength 5",
"keyword": "minLength"
},
{
"instanceRef": "#",
"schemaRef": "#",
"dynamicPath": "#/maxLength",
"message": "actual string length 4 exceeds maxLength 3",
"keyword": "maxLength"
},
{
"instanceRef": "#",
"schemaRef": "#",
"dynamicPath": "#/false",
"message": "false schema always fails",
"keyword": "false"
}
]
}
"""
""",
)(),
actual.toJSON()
actual.toJSON(),
)
}
}

0 comments on commit 085350a

Please sign in to comment.