Class: Respect::Org3Dumper
- Inherits:
-
Object
- Object
- Respect::Org3Dumper
- Defined in:
- lib/respect/org3_dumper.rb
Overview
Dump a schema to a hash representation following the format specified on json-schema.org standard draft v3.
The current implementation covers all the Schema and Validator classes defined in this package. User-defined Schema and Validator are not guarantee to work and may never work in the future. The JSON-Schema standard is a general purpose standard and include only primitive type so it is very unlikely that it will include your custom schema and validator out of the box. However, if you can translate your schema/validator as a composition of primitive type mentioned in the standard it will work. That's why it is recommended to sub-class CompositeSchema when creating your own schema. User-defined are not properly supported yet as the API of this dumper is experimental. However, an easy way to extend both the schema and validator class hierarchies will be added in future releases.
Constant Summary
- OPTION_MAP =
Translation table mapping DSL options with json-schema.org v3 options. The associated hash is injected in the output. Values are interpreted as follow:
-
:option_value represent the option value passed to the DSL option parameter.
-
a proc is called with the option value as argument and the result is used as the value for the output key if it is not nil.
-
other value are inserted verbatim.
-
{ min_size: { 'minItems' => :option_value }, max_size: { 'maxItems' => :option_value }, uniq: { "uniqueItems" => Proc.new{|v| v if v } }, default: { "default" => Proc.new{|v| v unless v.nil? } }, required: { "required" => Proc.new{|v| true if required? } }, }.freeze
Instance Attribute Summary (collapse)
-
- (Object) output
readonly
Returns the value of attribute output.
Instance Method Summary (collapse)
- - (Object) dump(output = nil)
- - (Object) dump_options(schema, *args)
- - (Object) dump_options_for_datetime_schema(schema)
- - (Object) dump_options_for_ipv4_addr_schema(schema)
- - (Object) dump_options_for_ipv6_addr_schema(schema)
- - (Object) dump_options_for_regexp_schema(schema)
- - (Object) dump_options_for_schema(schema)
- - (Object) dump_options_for_uri_schema(schema)
- - (Object) dump_schema(schema, *args)
- - (Object) dump_schema_for_array_schema(schema, params = {})
- - (Object) dump_schema_for_composite_schema(schema, params = {})
- - (Object) dump_schema_for_hash_schema(schema, params = {})
- - (Object) dump_schema_for_schema(schema, params = {})
- - (Object) dump_statement_name(schema, *args)
- - (Object) dump_statement_name_for_hash_schema(schema)
- - (Object) dump_statement_name_for_integer_schema(schema)
- - (Object) dump_statement_name_for_numeric_schema(schema)
- - (Object) dump_statement_name_for_schema(schema)
- - (Object) dump_statement_name_for_string_schema(schema)
-
- (Org3Dumper) initialize(schema)
constructor
A new instance of Org3Dumper.
Constructor Details
- (Org3Dumper) initialize(schema)
A new instance of Org3Dumper
34 35 36 |
# File 'lib/respect/org3_dumper.rb', line 34 def initialize(schema) @schema = schema end |
Instance Attribute Details
- (Object) output (readonly)
Returns the value of attribute output
45 46 47 |
# File 'lib/respect/org3_dumper.rb', line 45 def output @output end |
Instance Method Details
- (Object) dump(output = nil)
38 39 40 41 42 43 |
# File 'lib/respect/org3_dumper.rb', line 38 def dump(output = nil) @output = output @output ||= Hash.new @output = dump_schema(@schema, ignore: [:required]) @output end |
- (Object) dump_options(schema, *args)
166 167 168 |
# File 'lib/respect/org3_dumper.rb', line 166 def (schema, *args) dispatch("dump_options", schema.class, schema, *args) end |
- (Object) dump_options_for_datetime_schema(schema)
182 183 184 |
# File 'lib/respect/org3_dumper.rb', line 182 def (schema) { "format" => "date-time" } end |
- (Object) dump_options_for_ipv4_addr_schema(schema)
186 187 188 |
# File 'lib/respect/org3_dumper.rb', line 186 def (schema) { "format" => "ip-address" } end |
- (Object) dump_options_for_ipv6_addr_schema(schema)
190 191 192 |
# File 'lib/respect/org3_dumper.rb', line 190 def (schema) { "format" => "ipv6" } end |
- (Object) dump_options_for_regexp_schema(schema)
178 179 180 |
# File 'lib/respect/org3_dumper.rb', line 178 def (schema) { "format" => "regex" } end |
- (Object) dump_options_for_schema(schema)
170 171 172 |
# File 'lib/respect/org3_dumper.rb', line 170 def (schema) {} end |
- (Object) dump_options_for_uri_schema(schema)
174 175 176 |
# File 'lib/respect/org3_dumper.rb', line 174 def (schema) { "format" => "uri" } end |
- (Object) dump_schema(schema, *args)
47 48 49 |
# File 'lib/respect/org3_dumper.rb', line 47 def dump_schema(schema, *args) dispatch("dump_schema", schema.class, schema, *args) end |
- (Object) dump_schema_for_array_schema(schema, params = {})
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/respect/org3_dumper.rb', line 118 def dump_schema_for_array_schema(schema, params = {}) h = dump_schema_for_schema(schema, params) return nil if h.nil? if schema.item h['items'] = dump_schema(schema.item, ignore: [:required]) else if schema.items && !schema.items.empty? h['items'] = schema.items.map do |x| dump_schema(x, ignore: [:required]) end end if schema.extra_items && !schema.extra_items.empty? h['additionalItems'] = schema.extra_items.map do |x| dump_schema(x, ignore: [:required]) end end end h end |
- (Object) dump_schema_for_composite_schema(schema, params = {})
138 139 140 |
# File 'lib/respect/org3_dumper.rb', line 138 def dump_schema_for_composite_schema(schema, params = {}) dump_schema(schema.schema, params) end |
- (Object) dump_schema_for_hash_schema(schema, params = {})
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/respect/org3_dumper.rb', line 80 def dump_schema_for_hash_schema(schema, params = {}) h = dump_schema_for_schema(schema, params) return nil if h.nil? props = {} pattern_props = {} additional_props = {} schema.properties.each do |prop, schema| if prop.is_a?(Regexp) if schema.optional? # FIXME(Nicolas Despres): Find a better warning reporting system. warn "pattern properties cannot be optional in json-schema.org draft v3" else # FIXME(Nicolas Despres): What do we do with regexp options such as 'i'? schema_dump = dump_schema(schema) pattern_props[prop.source] = schema_dump if schema_dump end else if schema.optional? schema_dump = dump_schema(schema) additional_props[prop.to_s] = schema_dump if schema_dump else schema_dump = dump_schema(schema) props[prop.to_s] = schema_dump if schema_dump end end end h['properties'] = props unless props.empty? h['patternProperties'] = pattern_props unless pattern_props.empty? if additional_props.empty? if schema.[:strict] h['additionalProperties'] = false end else h['additionalProperties'] = additional_props end h end |
- (Object) dump_schema_for_schema(schema, params = {})
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/respect/org3_dumper.rb', line 51 def dump_schema_for_schema(schema, params = {}) return nil if !schema.documented? h = {} h['type'] = dump_statement_name(schema) # Dump generic options. schema..each do |opt, opt_value| next if params[:ignore] && params[:ignore].include?(opt) if validator_class = Respect.validator_for(opt) h.merge!(validator_class.new(opt_value).to_h(:org3)) elsif Org3Dumper::OPTION_MAP.has_key?(opt) Org3Dumper::OPTION_MAP[opt].each do |k, v| if v == :option_value h[k] = (opt_value.is_a?(Numeric) ? opt_value : opt_value.dup) elsif v.is_a?(Proc) result = schema.instance_exec(opt_value, &v) h[k] = result unless result.nil? else h[k] = v end end end end h.merge!((schema)) # Dump documentation h["title"] = schema.title if schema.title h["description"] = schema.description if schema.description h end |
- (Object) dump_statement_name(schema, *args)
142 143 144 |
# File 'lib/respect/org3_dumper.rb', line 142 def dump_statement_name(schema, *args) dispatch("dump_statement_name", schema.class, schema, *args) end |
- (Object) dump_statement_name_for_hash_schema(schema)
150 151 152 |
# File 'lib/respect/org3_dumper.rb', line 150 def dump_statement_name_for_hash_schema(schema) "object" end |
- (Object) dump_statement_name_for_integer_schema(schema)
158 159 160 |
# File 'lib/respect/org3_dumper.rb', line 158 def dump_statement_name_for_integer_schema(schema) "integer" end |
- (Object) dump_statement_name_for_numeric_schema(schema)
154 155 156 |
# File 'lib/respect/org3_dumper.rb', line 154 def dump_statement_name_for_numeric_schema(schema) "number" end |
- (Object) dump_statement_name_for_schema(schema)
146 147 148 |
# File 'lib/respect/org3_dumper.rb', line 146 def dump_statement_name_for_schema(schema) schema.class.statement_name end |
- (Object) dump_statement_name_for_string_schema(schema)
162 163 164 |
# File 'lib/respect/org3_dumper.rb', line 162 def dump_statement_name_for_string_schema(schema) "string" end |