Class: Marty::JsonSchema
- Inherits:
-
JSON::Schema::Draft4
- Object
- JSON::Schema::Draft4
- Marty::JsonSchema
- Defined in:
- lib/marty/json_schema.rb
Constant Summary collapse
- RAW_URI =
'http://json-schema.org/marty-draft/schema#'
Class Method Summary collapse
- .fix_numbers(json, numbers) ⇒ Object
- .get_numbers(schema) ⇒ Object
- .get_schema(tag, sname, node, attr) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ JsonSchema
constructor
A new instance of JsonSchema.
Constructor Details
#initialize ⇒ JsonSchema
Returns a new instance of JsonSchema.
28 29 30 31 32 33 34 35 |
# File 'lib/marty/json_schema.rb', line 28 def initialize super @attributes['pg_enum'] = PgEnumAttribute @formats['date-time'] = JSON::Schema::DateTimeFormat @formats['date'] = JSON::Schema::DateFormat @uri = JSON::Util::URI.parse(RAW_URI) @names = ['marty-draft', RAW_URI] end |
Class Method Details
.fix_numbers(json, numbers) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/marty/json_schema.rb', line 58 def self.fix_numbers(json, numbers) # follow path to drill into json drill = lambda { |tree, path| return unless tree key = path.first val = val = tree.send(:[], key) unless key == :array if key == :array # if we are at an array of numbers, fix them if path.length == 1 tree.each_with_index do |v, i| tree[i] = v.to_f if v.is_a?(Numeric) end else # this is an array of object so continue to drill down tree.each { |sub| drill.call(sub, path[1..-1]) } end elsif path.length == 1 # fix a non array field tree.send(:[]=, key, val.to_f) if val.is_a?(Numeric) else # continue drilling drill.call(val, path[1..-1]) end } numbers.each { |number| drill.call(json, number) } end |
.get_numbers(schema) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/marty/json_schema.rb', line 39 def self.get_numbers(schema) numbers = [] # traverse the schema, if we find a type: number, add to numbers [] trav = lambda { |tree, key, path = []| return tree.each do |k, v| trav.call(v, k, path + [k]) end if tree.is_a?(Hash) numbers << path[0..-2] if key == 'type' && tree == 'number' } trav.call(schema, nil, []) # convert the array stuff [ie. "items", "properties"] to :array numbers.map do |num| num.delete('properties') num.map { |n| n == 'items' ? :array : n } end end |
.get_schema(tag, sname, node, attr) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/marty/json_schema.rb', line 86 def self.get_schema(tag, sname, node, attr) Marty::ScriptSet.new(tag).get_engine(sname + 'Schemas'). evaluate(node, attr, {}) rescue StandardError => e id = "#{sname}/#{node} attrs=#{attr}" # the schema DL might not exist at all, or might not define the attr # being requested sch_not_found = ['No such script', "undefined method `#{attr}__D'", "node #{node} is undefined"] msg = sch_not_found.detect { |msg| e..starts_with?(msg) } ? 'Schema not defined' : "Problem with schema: #{e.}" "Schema error for #{id}: #{msg}" end |