From 738fda3e34bdd4eba0335889181b849b72ec9a0c Mon Sep 17 00:00:00 2001 From: Dov Murik Date: Tue, 3 Apr 2012 10:59:09 +0300 Subject: [PATCH] accept the "required" attribute instead of "optional" According to jsonschema IETF draft-03, the "optional" attribute was replaced with the "required" attribute (default is required=false). --- lib/jsonschema.rb | 6 +++--- test/jsonschema_test.rb | 19 ++++++------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/jsonschema.rb b/lib/jsonschema.rb index 5ee800d..e49dcef 100644 --- a/lib/jsonschema.rb +++ b/lib/jsonschema.rb @@ -45,8 +45,8 @@ def check_property value, schema, key, parent end if value == Undefined - unless schema['optional'] - raise ValueError, "#{key_path}: is missing and it is not optional" + if schema['required'] + raise ValueError, "#{key_path}: is missing and it is required" end # default @@ -78,7 +78,7 @@ def check_property value, schema, key, parent if schema['items'] if schema['items'].kind_of?(Array) schema['items'].each_with_index {|val, index| - check_property(undefined_check(value, index), schema['items'][index], index, value) + check_property(undefined_check(value, index), schema['items'][index].merge("required" => true), index, value) } if schema.include?('additionalProperties') additional = schema['additionalProperties'] diff --git a/test/jsonschema_test.rb b/test/jsonschema_test.rb index 01339ae..9e0f8d5 100644 --- a/test/jsonschema_test.rb +++ b/test/jsonschema_test.rb @@ -14,7 +14,6 @@ def test_self_schema "age" => { "type"=> "integer", "maximum"=> 125, - "optional"=> true } } }, @@ -34,7 +33,6 @@ def test_self_schema "age" => { "type"=> "integer", "maximum"=> 125, - "optional"=> true } } }, @@ -54,7 +52,6 @@ def test_self_schema "age" => { "type"=> "integer", "maximum"=> 125, - "optional"=> true } } }, @@ -421,7 +418,7 @@ def test_properties }, "prop02"=>{ "type"=>"number", - "optional"=>true + "required"=>false }, "prop03"=>{ "type"=>"integer", @@ -431,14 +428,14 @@ def test_properties }, "prop05"=>{ "type"=>"object", - "optional"=>true, + "required"=>false, "properties"=>{ "subprop01"=>{ "type"=>"string", }, "subprop02"=>{ "type"=>"string", - "optional"=>false + "required"=>true } } } @@ -499,11 +496,9 @@ def test_requires "properties"=>{ "prop01"=>{ "type"=>"string", - "optional"=>true }, "prop02"=>{ "type"=>"number", - "optional"=>true, "requires"=>"prop01" } } @@ -545,7 +540,7 @@ def test_pattern } end - def test_optional + def test_required schema = { "type"=>"object", "properties"=>{ @@ -554,14 +549,14 @@ def test_optional }, "prop02"=>{ "type"=>"number", - "optional"=>true + "required"=>false }, "prop03"=>{ "type"=>"integer" }, "prop04"=>{ "type"=>"boolean", - "optional"=>false + "required"=>true } } } @@ -598,7 +593,6 @@ def test_default schema1 = { "properties"=>{ "test"=>{ - "optional"=>true, "default"=>10 }, } @@ -606,7 +600,6 @@ def test_default schema2 = { "properties"=>{ "test"=>{ - "optional"=>true, "default"=>10, "readonly"=>true }