diff --git a/lib/jsonschema.rb b/lib/jsonschema.rb index 5ee800d..2e9d33e 100644 --- a/lib/jsonschema.rb +++ b/lib/jsonschema.rb @@ -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) diff --git a/test/jsonschema_test.rb b/test/jsonschema_test.rb index 01339ae..a904281 100644 --- a/test/jsonschema_test.rb +++ b/test/jsonschema_test.rb @@ -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"]}