Skip to content

Instantly share code, notes, and snippets.

@techbelly
Created July 19, 2018 09:06
Show Gist options
  • Save techbelly/86c364135730cc8a9bce598d9f692f1c to your computer and use it in GitHub Desktop.
Save techbelly/86c364135730cc8a9bce598d9f692f1c to your computer and use it in GitHub Desktop.
Transform json schema v3 required to v4
class Stack < Array
def add_frame
Stack.new(self + [[]])
end
def frames_ago n
return self[-n]
end
end
def remap(schema, parent_key = nil, stack = Stack.new)
schema.each_with_object({}) do |(k, v), copy|
if v.is_a?(Hash)
new_stack = stack.add_frame
copy[k] = remap(v, k, new_stack)
copy["required"] = new_stack.last unless new_stack.last.empty?
elsif k == "required"
stack.frames_ago(2) << parent_key if v
else
copy[k] = v
end
end
end
require 'json'
file = ARGF.read
schema = JSON.parse(file)
begin
new_schema = remap(schema)
puts new_schema.to_json
rescue Exception => err
$stderr.puts "#{ARGF.filename} #{err}"
puts file
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment