Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax restrictions for Arrays #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
29 changes: 10 additions & 19 deletions lib/json/schema_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class SchemaGenerator

class << self
def generate name, data, opts = {}
generator = JSON::SchemaGenerator.new name, opts
generator.generate data
JSON::SchemaGenerator.new(name, opts).generate data
end
end

Expand Down Expand Up @@ -72,7 +71,7 @@ def create_primitive(statement_group, key, value, required_keys)
raise "Unknown Primitive Type for #{key}! #{value.class}"
end

statement_group.add "\"type\": \"#{type}\""
statement_group.add "\"oneOf\": [{\"type\": \"#{type}\"}, {\"type\": \"null\"}]"
statement_group.add "\"required\": #{required}" if @version == DRAFT3
statement_group.add "\"default\": #{value.inspect}" if @defaults
end
Expand Down Expand Up @@ -122,28 +121,20 @@ def create_hash_properties(data, required_keys)
def create_array(statement_group, data, required_keys)
statement_group.add '"type": "array"'
statement_group.add '"required": true' if @version == DRAFT3
if data.size == 0
statement_group.add '"minItems": 0'
else
statement_group.add '"minItems": 1'
end
statement_group.add '"uniqueItems": true'
# FIXME - Code assumes that all items in the array have the same structure
# Assume lowest common denominator - allow 0 items and unique not required
statement_group.add '"minItems": 0'

# TODO - consider a eq? method for StatementGroup class to evaluate LCD schema from all items in array
statement_group.add create_values("items", data.first, required_keys, true)

statement_group
end

def detect_required(collection)
begin
required_keys = @brute_search.find_required
rescue
if collection.respond_to? :keys
required_keys = collection.keys
else
required_keys = nil
end
end
required_keys
@brute_search.find_required
rescue NoMethodError
collection.keys if collection.respond_to?(:keys)
end

def result
Expand Down
20 changes: 4 additions & 16 deletions lib/json/schema_generator/brute_force_required_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ class SchemaGenerator
class BruteForceRequiredSearch
def initialize(data)
@data = data.dup
if data.is_a? Array
@json_path = ['$[*]']
else
@json_path = ['$']
end
@json_path = data.is_a?(Array) ? ['$[*]'] : ['$']
end

def push(key, value)
if value.is_a? Array
@json_path.push "#{key}[*]"
else
@json_path.push key
end
@json_path.push value.is_a?(Array) ? "#{key}[*]" : key
end

def pop
Expand All @@ -42,16 +34,12 @@ def required? child_key
end
end

def child_keys
def child_keys
JsonPath.new(current_path).on(@data).map(&:keys).flatten.uniq
end

def find_required
required = []
child_keys.each do |child_key|
required << child_key if required? child_key
end
required
child_keys.select {|k| required? k}
end
end
end
Expand Down