Skip to content

Instantly share code, notes, and snippets.

@tomaslin
Created January 10, 2020 22:21
Show Gist options
  • Save tomaslin/1b7447b71730abbea8b317d0d4bbd6f0 to your computer and use it in GitHub Desktop.
Save tomaslin/1b7447b71730abbea8b317d0d4bbd6f0 to your computer and use it in GitHub Desktop.
A spock test that validates schemas
package com.netflix.mce.schemavalidator
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.networknt.schema.JsonSchemaFactory
import com.networknt.schema.SpecVersion
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
class ValidateSchemaSpec extends Specification {
private static File YML_DIRECTORY = new File("configs")
private static File SCHEMA_FILE = new File("schema/schema.json")
@Shared
ObjectMapper mapper = new ObjectMapper(new YAMLFactory())
@Shared
JsonSchemaFactory factory = JsonSchemaFactory.builder(
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)
)
.objectMapper(mapper)
.build()
@Unroll("Validating yaml schema for #file")
void "validate all items in configs directory against schema"(){
given:
Set invalidMessages = factory.getSchema(SCHEMA_FILE.text)
.validate(mapper.readTree(file.text))
.message
if(!invalidMessages.empty){
println "Schema validation failed: ${file.name}"
invalidMessages.each{ println it }
}
expect:
invalidMessages.isEmpty()
where:
file << YML_DIRECTORY.listFiles()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment