Class: Crawlscope::SchemaRegistry
- Inherits:
-
Object
- Object
- Crawlscope::SchemaRegistry
- Defined in:
- lib/crawlscope/schema_registry.rb
Class Method Summary collapse
Instance Method Summary collapse
- #dup ⇒ Object
- #fetch(type) ⇒ Object
-
#initialize(schemas: {}) ⇒ SchemaRegistry
constructor
A new instance of SchemaRegistry.
- #register(type, schema) ⇒ Object
- #registered?(type) ⇒ Boolean
- #to_h ⇒ Object
- #validate(item) ⇒ Object
Constructor Details
#initialize(schemas: {}) ⇒ SchemaRegistry
Returns a new instance of SchemaRegistry.
9 10 11 |
# File 'lib/crawlscope/schema_registry.rb', line 9 def initialize(schemas: {}) @schemas = schemas.transform_keys(&:to_s).dup end |
Class Method Details
Instance Method Details
#dup ⇒ Object
17 18 19 |
# File 'lib/crawlscope/schema_registry.rb', line 17 def dup self.class.new(schemas: deep_copy(@schemas)) end |
#fetch(type) ⇒ Object
21 22 23 |
# File 'lib/crawlscope/schema_registry.rb', line 21 def fetch(type) @schemas.fetch(type.to_s) end |
#register(type, schema) ⇒ Object
25 26 27 28 |
# File 'lib/crawlscope/schema_registry.rb', line 25 def register(type, schema) @schemas[type.to_s] = schema self end |
#registered?(type) ⇒ Boolean
30 31 32 |
# File 'lib/crawlscope/schema_registry.rb', line 30 def registered?(type) @schemas.key?(type.to_s) end |
#to_h ⇒ Object
66 67 68 |
# File 'lib/crawlscope/schema_registry.rb', line 66 def to_h @schemas.dup end |
#validate(item) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/crawlscope/schema_registry.rb', line 34 def validate(item) if item.is_a?(Array) return item.flat_map { |entry| validate(entry) } end errors = [] if item.is_a?(Hash) && item["@graph"].is_a?(Array) item["@graph"].each do |graph_item| errors.concat(validate(graph_item)) end end type = item.is_a?(Hash) ? item["@type"] : nil return errors if type.nil? schema = @schemas[type.to_s] return errors if schema.nil? JSON::Validator.fully_validate(schema, item, errors_as_objects: true).each do |error| errors << { field: error[:fragment].to_s.sub("#/", ""), issue: error[:message], type: type } end errors rescue JSON::Schema::ValidationError => error [{field: "unknown", issue: error., type: type}] end |