Module: Hdo::StortingImporter::HasJsonSchema::ClassMethods
- Defined in:
- lib/hdo/storting_importer/has_json_schema.rb
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #description ⇒ Object
- #from_json(str) ⇒ Object
- #json_example ⇒ Object
- #kind ⇒ Object
- #properties ⇒ Object (also: #fields)
- #schema_path(path) ⇒ Object
- #valid?(e) ⇒ Boolean
- #validate!(e) ⇒ Object
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
41 42 43 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 41 def schema @schema end |
Instance Method Details
#description ⇒ Object
59 60 61 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 59 def description @description ||= schema['description'] end |
#from_json(str) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 74 def from_json(str) data = MultiJson.decode(str) case data when Array data.map { |e| from_hash validate!(e) } when Hash from_hash validate!(data) else raise TypeError, "expected Array or Hash, got #{data.inspect}:#{data.class}" end end |
#json_example ⇒ Object
51 52 53 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 51 def json_example @json_example ||= Util.json_pretty example end |
#kind ⇒ Object
55 56 57 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 55 def kind @kind ||= schema['properties']['kind']['default'] end |
#properties ⇒ Object Also known as: fields
63 64 65 66 67 68 69 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 63 def properties @properties ||= ( schema['properties'].map do |name, data| Field.new(name, !!data['required'], data['type'], data['description'] || 'unknown') end ) end |
#schema_path(path) ⇒ Object
43 44 45 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 43 def schema_path(path) @schema_path = path end |
#valid?(e) ⇒ Boolean
87 88 89 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 87 def valid?(e) Jschematic.validate(e, schema, :context => HasJsonSchema.schemas, :debug => true) end |
#validate!(e) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 91 def validate!(e) Jschematic.validate!(e, schema, :context => HasJsonSchema.schemas, :debug => true) e rescue Jschematic::ValidationError => ex raise ValidationError, "#{ex.}: #{e.inspect}" end |