Class: JSONSchemer::Result
- Inherits:
-
Struct
- Object
- Struct
- JSONSchemer::Result
- Defined in:
- lib/json_schemer/result.rb
Instance Attribute Summary collapse
-
#annotation ⇒ Object
Returns the value of attribute annotation.
-
#details ⇒ Object
Returns the value of attribute details.
-
#ignore_nested ⇒ Object
Returns the value of attribute ignore_nested.
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#instance_location ⇒ Object
Returns the value of attribute instance_location.
-
#keyword_location ⇒ Object
Returns the value of attribute keyword_location.
-
#nested ⇒ Object
Returns the value of attribute nested.
-
#nested_key ⇒ Object
Returns the value of attribute nested_key.
-
#source ⇒ Object
Returns the value of attribute source.
-
#type ⇒ Object
Returns the value of attribute type.
-
#valid ⇒ Object
Returns the value of attribute valid.
Instance Method Summary collapse
- #basic ⇒ Object
- #classic ⇒ Object
- #detailed ⇒ Object
- #error ⇒ Object
- #flag ⇒ Object
- #i18n! ⇒ Object
- #i18n? ⇒ Boolean
- #insert_property_defaults(context) ⇒ Object
- #output(output_format) ⇒ Object
- #to_classic ⇒ Object
- #to_output_unit ⇒ Object
- #verbose ⇒ Object
Instance Attribute Details
#annotation ⇒ Object
Returns the value of attribute annotation
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def annotation @annotation end |
#details ⇒ Object
Returns the value of attribute details
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def details @details end |
#ignore_nested ⇒ Object
Returns the value of attribute ignore_nested
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def ignore_nested @ignore_nested end |
#instance ⇒ Object
Returns the value of attribute instance
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def instance @instance end |
#instance_location ⇒ Object
Returns the value of attribute instance_location
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def instance_location @instance_location end |
#keyword_location ⇒ Object
Returns the value of attribute keyword_location
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def keyword_location @keyword_location end |
#nested ⇒ Object
Returns the value of attribute nested
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def nested @nested end |
#nested_key ⇒ Object
Returns the value of attribute nested_key
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def nested_key @nested_key end |
#source ⇒ Object
Returns the value of attribute source
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def source @source end |
#type ⇒ Object
Returns the value of attribute type
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def type @type end |
#valid ⇒ Object
Returns the value of attribute valid
12 13 14 |
# File 'lib/json_schemer/result.rb', line 12 def valid @valid end |
Instance Method Details
#basic ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/json_schemer/result.rb', line 126 def basic out = to_output_unit if nested&.any? out[nested_key] = Enumerator.new do |yielder| results = [self] while result = results.pop if result.ignore_nested || !result.nested&.any? yielder << result.to_output_unit else previous_results_size = results.size result.nested.reverse_each do |nested_result| results << nested_result if nested_result.valid == valid end yielder << result.to_output_unit unless (results.size - previous_results_size) == 1 end end end end out end |
#classic ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/json_schemer/result.rb', line 173 def classic Enumerator.new do |yielder| unless valid results = [self] while result = results.pop if result.ignore_nested || !result.nested&.any? yielder << result.to_classic else previous_results_size = results.size result.nested.reverse_each do |nested_result| results << nested_result if nested_result.valid == valid end yielder << result.to_classic if (results.size - previous_results_size) == 0 end end end end end |
#detailed ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/json_schemer/result.rb', line 147 def detailed return to_output_unit if ignore_nested || !nested&.any? matching_results = nested.select { |nested_result| nested_result.valid == valid } if matching_results.size == 1 matching_results.first.detailed else out = to_output_unit if matching_results.any? out[nested_key] = Enumerator.new do |yielder| matching_results.each { |nested_result| yielder << nested_result.detailed } end end out end end |
#error ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/json_schemer/result.rb', line 30 def error return @error if defined?(@error) if source.x_error # not using sprintf because it warns: "too many arguments for format string" @error = source.x_error.gsub( X_ERROR_REGEX, '%{instance}' => instance, '%{instanceLocation}' => Location.resolve(instance_location), '%{keywordLocation}' => Location.resolve(keyword_location), '%{absoluteKeywordLocation}' => source.absolute_keyword_location ) @x_error = true else resolved_instance_location = Location.resolve(instance_location) formatted_instance_location = resolved_instance_location.empty? ? 'root' : "`#{resolved_instance_location}`" @error = source.error(:formatted_instance_location => formatted_instance_location, :details => details) if i18n? begin @error = i18n! @i18n = true rescue I18n::MissingTranslationData end end end @error end |
#flag ⇒ Object
122 123 124 |
# File 'lib/json_schemer/result.rb', line 122 def flag { 'valid' => valid } end |
#i18n! ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/json_schemer/result.rb', line 62 def i18n! base_uri_str = source.schema.base_uri.to_s = source.schema..base_uri.to_s resolved_keyword_location = Location.resolve(keyword_location) error_key = source.error_key I18n.translate!( source.absolute_keyword_location, :default => [ "#{base_uri_str}#{I18N_SEPARATOR}##{resolved_keyword_location}", "##{resolved_keyword_location}", "#{base_uri_str}#{I18N_SEPARATOR}#{error_key}", "#{base_uri_str}#{I18N_SEPARATOR}#{CATCHALL}", "#{}#{I18N_SEPARATOR}#{error_key}", "#{}#{I18N_SEPARATOR}#{CATCHALL}", error_key, CATCHALL ].map!(&:to_sym), :separator => I18N_SEPARATOR, :scope => I18N_ERRORS_SCOPE, :instance => instance, :instanceLocation => Location.resolve(instance_location), :keywordLocation => resolved_keyword_location, :absoluteKeywordLocation => source.absolute_keyword_location ) end |
#i18n? ⇒ Boolean
57 58 59 60 |
# File 'lib/json_schemer/result.rb', line 57 def i18n? return @@i18n if defined?(@@i18n) @@i18n = defined?(I18n) && I18n.exists?(I18N_SCOPE) end |
#insert_property_defaults(context) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/json_schemer/result.rb', line 192 def insert_property_defaults(context) instance_locations = {} instance_locations.compare_by_identity results = [[self, true]] while (result, valid = results.pop) next if result.source.is_a?(Schema::NOT_KEYWORD_CLASS) valid &&= result.valid result.nested&.each { |nested_result| results << [nested_result, valid] } if result.source.is_a?(Schema::PROPERTIES_KEYWORD_CLASS) && result.instance.is_a?(Hash) result.source.parsed.each do |property, schema| next if result.instance.key?(property) next unless default = default_keyword_instance(schema) instance_location = Location.join(result.instance_location, property) keyword_location = Location.join(Location.join(result.keyword_location, property), default.keyword) default_result = default.validate(nil, instance_location, keyword_location, nil) instance_locations[result.instance_location] ||= {} instance_locations[result.instance_location][property] ||= [] instance_locations[result.instance_location][property] << [default_result, valid] end end end inserted = false instance_locations.each do |instance_location, properties| original_instance = context.original_instance(instance_location) properties.each do |property, results_with_tree_validity| property_inserted = yield(original_instance, property, results_with_tree_validity) inserted ||= (property_inserted != false) end end inserted end |
#output(output_format) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/json_schemer/result.rb', line 13 def output(output_format) case output_format when 'classic' classic when 'flag' flag when 'basic' basic when 'detailed' detailed when 'verbose' verbose else raise UnknownOutputFormat, output_format end end |
#to_classic ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/json_schemer/result.rb', line 105 def to_classic schema = source.schema out = { 'data' => instance, 'data_pointer' => Location.resolve(instance_location), 'schema' => schema.value, 'schema_pointer' => schema.schema_pointer, 'root_schema' => schema.root.value, 'type' => type || CLASSIC_ERROR_TYPES[source.class] } out['error'] = error out['x-error'] = true if @x_error out['i18n'] = true if @i18n out['details'] = details if details out end |
#to_output_unit ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/json_schemer/result.rb', line 88 def to_output_unit out = { 'valid' => valid, 'keywordLocation' => Location.resolve(keyword_location), 'absoluteKeywordLocation' => source.absolute_keyword_location, 'instanceLocation' => Location.resolve(instance_location) } if valid out['annotation'] = annotation if annotation else out['error'] = error out['x-error'] = true if @x_error out['i18n'] = true if @i18n end out end |
#verbose ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/json_schemer/result.rb', line 163 def verbose out = to_output_unit if nested&.any? out[nested_key] = Enumerator.new do |yielder| nested.each { |nested_result| yielder << nested_result.verbose } end end out end |