1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
require File.expand_path('../support/test_helper', __FILE__)
require 'tmpdir'
class SchemaValidationTest < Minitest::Test
def valid_schema_v3
{
"$schema" => "http://json-schema.org/draft-03/schema#",
"type" => "object",
"properties" => {
"b" => {
"required" => true
}
}
}
end
def invalid_schema_v3
{
"$schema" => "http://json-schema.org/draft-03/schema#",
"type" => "object",
"properties" => {
"b" => {
"required" => "true"
}
}
}
end
def valid_schema_v4
{
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "object",
"required" => ["b"],
"properties" => {
}
}
end
def invalid_schema_v4
{
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "object",
"required" => "b",
"properties" => {
}
}
end
def symbolized_schema
{
:type => :object,
:required => [
:id,
:name,
:real_name,
:role,
:website,
:biography,
:created_at,
:demographic
],
:properties => {
:id => {
:type => [
:integer
]
},
:name => {
:type => [
:string
]
},
:real_name => {
:type => [
:string
]
},
:role => {
:type => [
:string
]
},
:website => {
:type => [
:string,
:null
]
},
:created_at => {
:type => [
:string
]
},
:biography => {
:type => [
:string,
:null
]
}
},
:relationships => {
:demographic => {
:type => :object,
:required => [
:id,
:gender
],
:properties => {
:id => {
:type => [
:integer
]
},
:gender => {
:type => [
:string
]
}
}
}
}
}
end
def test_draft03_validation
data = {"b" => {"a" => 5}}
assert(JSON::Validator.validate(valid_schema_v3,data,:validate_schema => true, :version => :draft3))
assert(!JSON::Validator.validate(invalid_schema_v3,data,:validate_schema => true, :version => :draft3))
end
def test_validate_just_schema_draft03
errors = JSON::Validator.fully_validate_schema(valid_schema_v3, :version => :draft3)
assert_equal [], errors
errors = JSON::Validator.fully_validate_schema(invalid_schema_v3, :version => :draft3)
assert_equal 1, errors.size
assert_match(/the property .*required.*did not match/i, errors.first)
end
def test_draft04_validation
data = {"b" => {"a" => 5}}
assert(JSON::Validator.validate(valid_schema_v4,data,:validate_schema => true, :version => :draft4))
assert(!JSON::Validator.validate(invalid_schema_v4,data,:validate_schema => true, :version => :draft4))
end
def test_validate_just_schema_draft04
errors = JSON::Validator.fully_validate_schema(valid_schema_v4, :version => :draft4)
assert_equal [], errors
errors = JSON::Validator.fully_validate_schema(invalid_schema_v4, :version => :draft4)
assert_equal 1, errors.size
assert_match(/the property .*required.*did not match/i, errors.first)
end
def test_validate_schema_3_without_version_option
data = {"b" => {"a" => 5}}
assert(JSON::Validator.validate(valid_schema_v3,data,:validate_schema => true))
assert(!JSON::Validator.validate(invalid_schema_v3,data,:validate_schema => true))
end
def test_schema_validation_from_different_directory
Dir.mktmpdir do |tmpdir|
Dir.chdir(tmpdir) do
data = {"b" => {"a" => 5}}
assert(JSON::Validator.validate(valid_schema_v4,data,:validate_schema => true, :version => :draft4))
assert(!JSON::Validator.validate(invalid_schema_v4,data,:validate_schema => true, :version => :draft4))
end
end
end
def test_validate_schema_with_symbol_keys
data = {
"created_at" => "2014-01-25T00:58:33-08:00",
"id" => 8517194300913402149003,
"name" => "chelsey",
"real_name" => "Mekhi Hegmann",
"website" => nil,
"role" => "user",
"biography" => nil,
"demographic" => nil
}
assert(JSON::Validator.validate!(symbolized_schema, data, :validate_schema => true))
end
end
|