JSONSchema.jl

JSON Schema validation package for Julia
Author fredo-dedup
Popularity
8 Stars
Updated Last
5 Months Ago
Started In
June 2018

JSONSchema

JSON instance validation using JSON Schemas

Build Status Build status Coverage Status codecov

Overview

JSONSchema.jl is a JSON validation package for the julia programming language. Given a validation schema this package can verify if any JSON instance meets all the assertions defining a valid document.

This package has been validated with the JSON Schema Test Suite for draft v4 and v6.

API

Create a Schema object by passing a string:

julia> my_schema = Schema("""{
            "properties": {
                "foo": {},
                "bar": {}
            },
            "required": ["foo"]
        }""")

passing a dictionary with the same structure as a schema:

julia> my_schema = Schema(
            Dict(
                "properties" => Dict(
                    "foo" => Dict(),
                    "bar" => Dict()
                ),
                "required" => ["foo"]
            )
        )

or by passing a parsed JSON file containing the schema:

julia> my_schema = Schema(JSON.parsefile(filename))

Check the validity of a given JSON instance by calling validate with the JSON instance x to be tested and the schema. If the validation succeeds, validate returns nothing:

julia> data_pass = Dict("foo" => true)
Dict{String,Bool} with 1 entry:
  "foo" => true

julia> validate(data_pass, my_schema)

If the validation fails, a struct is returned that, when printed, explains the reason for the failure:

julia> data_fail = Dict("bar" => 12.5)
Dict{String,Float64} with 1 entry:
  "bar" => 12.5

julia> validate(data_fail, my_schema)
Validation failed:
path:         top-level
instance:     Dict("bar"=>12.5)
schema key:   required
schema value: ["foo"]

As a short-hand for validate(x, schema) === nothing, use Base.isvalid(x, schema).