Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bzamecnik/af6846c882b9432fa2a791a8e21b2c64 to your computer and use it in GitHub Desktop.
Save bzamecnik/af6846c882b9432fa2a791a8e21b2c64 to your computer and use it in GitHub Desktop.
Example of using JSON-schema validator (https://github.com/java-json-tools/json-schema-validator)
package com.github.fge.jsonschema
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.github.fge.jsonschema.main.{JsonSchema, JsonSchemaFactory}
import org.scalatest.FunSuite
// Example of using JSON-schema validator
// https://github.com/java-json-tools/json-schema-validator
//
// Code Samples - Example 1 - translated to a Scala test:
// http://java-json-tools.github.io/json-schema-validator/2.2.x/index.html?com/github/fge/jsonschema/examples/package-summary.html
class JsonSchemaValidatorExplorationTest extends FunSuite {
test("foo") {
val objectMapper = new ObjectMapper
val fstabSchema: JsonNode = objectMapper.readTree(
"""{
| "$schema": "http://json-schema.org/draft-04/schema#",
| "title": "/etc/fstab",
| "description": "JSON representation of /etc/fstab",
| "type": "object",
| "properties": {
| "swap": {
| "$ref": "#/definitions/mntent"
| }
| },
| "patternProperties": {
| "^/([^/]+(/[^/]+)*)?$": {
| "$ref": "#/definitions/mntent"
| }
| },
| "required": [ "/", "swap" ],
| "additionalProperties": false,
| "definitions": {
| "mntent": {
| "title": "mntent",
| "description": "An fstab entry",
| "type": "object",
| "properties": {
| "device": {
| "type": "string"
| },
| "fstype": {
| "type": "string"
| },
| "options": {
| "type": "array",
| "minItems": 1,
| "items": { "type": "string" }
| },
| "dump": {
| "type": "integer",
| "minimum": 0
| },
| "fsck": {
| "type": "integer",
| "minimum": 0
| }
| },
| "required": [ "device", "fstype" ],
| "additionalItems": false
| }
| }
|}
""".stripMargin)
// valid
val good: JsonNode = objectMapper.readTree(
"""{
| "/": {
| "device": "/dev/sda1",
| "fstype": "btrfs",
| "options": [ "ssd" ]
| },
| "swap": {
| "device": "/dev/sda2",
| "fstype": "swap"
| },
| "/tmp": {
| "device": "tmpfs",
| "fstype": "tmpfs",
| "options": [ "size=64M" ]
| },
| "/var/lib/mysql": {
| "device": "/dev/data/mysql",
| "fstype": "btrfs"
| }
|}
""".stripMargin)
// Please note that the failure occurs at the structural level (required entry swap is missing). Validation therefore stops here, and does not attempt to validate the / member of the instance, which is itself invalid.
val bad: JsonNode = objectMapper.readTree(
"""{
| "/": {
| "fstype": "btrfs",
| "options": [ "ssd" ]
| },
| "/tmp": {
| "device": "tmpfs",
| "fstype": "tmpfs",
| "options": [ "size=64M" ]
| },
| "/var/lib/mysql": {
| "device": "/dev/data/mysql",
| "fstype": "btrfs"
| }
|}""".stripMargin)
// problem is with the member values:
//
// the options member of /tmp is a string, but an array is expected;
// the / member is missing the required fstype member.
val bad2: JsonNode = objectMapper.readTree(
"""{
| "/": {
| "device": "/dev/sda1",
| "options": [ "ssd" ]
| },
| "swap": {
| "device": "/dev/sda2",
| "fstype": "swap"
| },
| "/tmp": {
| "device": "tmpfs",
| "fstype": "tmpfs",
| "options": "size=64M"
| },
| "/var/lib/mysql": {
| "device": "/dev/data/mysql",
| "fstype": "btrfs"
| }
|}
|
""".stripMargin)
val factory: JsonSchemaFactory = JsonSchemaFactory.byDefault()
val schema: JsonSchema = factory.getJsonSchema(fstabSchema)
println(schema.validate(good))
println(schema.validate(bad))
println(schema.validate(bad2))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment