Skip to content

Commit

Permalink
Allow enums to be nullable.
Browse files Browse the repository at this point in the history
Fixes #37.
  • Loading branch information
myronmarston committed Dec 2, 2013
1 parent b1ab0a7 commit 37f84f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/interpol/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ def conditionally_make_nullable(raw_schema)
types = Array(raw_schema['type'])
return if types.none? || types.include?('null')

raw_schema['type'] = (types << "null")
# Mark as non-nullable so it doesn't try to recurse down into it
# and make it nullable at that level (since we are taking care of it at this level)
sub_schema = raw_schema.merge('nullable' => false)
raw_schema.replace('type' => ['null', sub_schema])
end

def should_be_nullable?(raw_schema)
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/interpol/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,28 @@ def new_with(hash)
expect { subject.validate_data!('foo' => nil) }.not_to raise_error
end

it 'works with enums' do
schema['properties']['foo'] = {
'type' => 'string',
'enum' => %w[ A B C D F ]
}

expect { subject.validate_data!('foo' => nil) }.not_to raise_error
expect { subject.validate_data!('foo' => 'A') }.not_to raise_error
expect { subject.validate_data!('foo' => 'F') }.not_to raise_error
expect { subject.validate_data!('foo' => 'E') }.to raise_error(ValidationError)
end

it 'allows enums to be non-nullable' do
schema['properties']['foo'] = {
'nullable' => false,
'type' => 'string',
'enum' => %w[ A B C D F ]
}

expect { subject.validate_data!('foo' => nil) }.to raise_error(ValidationError)
end

it 'does not add an extra `null` entry to an existing nullable union type' do
schema['properties']['foo']['type'] = %w[ integer null ]

Expand Down

0 comments on commit 37f84f1

Please sign in to comment.