Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/jsonschema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ def check_property value, schema, key, parent
end
elsif schema['properties']
check_object(value, schema['properties'], schema['additionalProperties'])
elsif schema['patternProperties']
value.each { |k, val|
schema['patternProperties'].each { |pattern_string, pattern_schema|
if k =~ Regexp.new(pattern_string)
check_property(val, schema['patternProperties'][pattern_string], k, value)
end
}
}
elsif schema.include?('additionalProperties')
additional = schema['additionalProperties']
unless additional.kind_of?(TrueClass)
Expand Down
33 changes: 33 additions & 0 deletions test/jsonschema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,39 @@ def test_additionalProperties
end
end

def test_patternProperties
schema1 = {
"patternProperties" => {
"^i.*$" => {
"type" => "integer"
},
"^s.*$" => {
"type" => "string"
},
"^b.*$" => {
"type" => "boolean"
}
}
}
data1 = {
"iabc" => 123,
"s456" => "val",
"another" => { "hash" => 1234 }
}
assert_nothing_raised{
JSON::Schema.validate(data1, schema1)
}
data2 = {
"iabc" => 123,
"s456" => "val",
"another" => { "hash" => 1234 },
"b567" => "string_instead_of_boolean"
}
assert_raise(JSON::Schema::ValueError){
JSON::Schema.validate(data2, schema1)
}
end

def test_disallow
# multi phase
schema = {"disallow"=>["null","integer","string"]}
Expand Down