Class: AvroGen::SchemaValidator
- Inherits:
-
Object
- Object
- AvroGen::SchemaValidator
- Defined in:
- lib/avro_gen/schema_validator.rb
Overview
Loads an Avro schema from the configured schema path and exposes its fields. Used at runtime by generated Record classes (for #schema_fields) and as the schema loader for the generator.
Instance Attribute Summary collapse
- #namespace ⇒ String readonly
- #schema ⇒ String readonly
Class Method Summary collapse
-
.clear_store_cache! ⇒ void
Drop cached stores; call when the schemas on disk may have changed.
-
.store_for(path) ⇒ SchemaRegistry::AvroSchemaStore
A schema store is an in-memory cache of parsed Avro schemas.
Instance Method Summary collapse
- #avro_schema(schema = nil) ⇒ Avro::Schema::NamedSchema
-
#initialize(schema:, namespace:, path: nil, store: nil) ⇒ SchemaValidator
constructor
A new instance of SchemaValidator.
-
#load_schema ⇒ Avro::Schema
Forcefully loads the schema into memory.
- #schema_fields ⇒ Array<AvroGen::SchemaField>
- #schema_store ⇒ SchemaRegistry::AvroSchemaStore
- #validate(payload, schema: nil) ⇒ void
Constructor Details
#initialize(schema:, namespace:, path: nil, store: nil) ⇒ SchemaValidator
Returns a new instance of SchemaValidator.
41 42 43 44 45 46 |
# File 'lib/avro_gen/schema_validator.rb', line 41 def initialize(schema:, namespace:, path: nil, store: nil) @schema = schema @namespace = namespace @path = path || AvroGen.config.schema_path @schema_store = store end |
Instance Attribute Details
#namespace ⇒ String (readonly)
15 16 17 |
# File 'lib/avro_gen/schema_validator.rb', line 15 def namespace @namespace end |
#schema ⇒ String (readonly)
13 14 15 |
# File 'lib/avro_gen/schema_validator.rb', line 13 def schema @schema end |
Class Method Details
.clear_store_cache! ⇒ void
This method returns an undefined value.
Drop cached stores; call when the schemas on disk may have changed.
31 32 33 |
# File 'lib/avro_gen/schema_validator.rb', line 31 def clear_store_cache! @stores = {} end |
.store_for(path) ⇒ SchemaRegistry::AvroSchemaStore
A schema store is an in-memory cache of parsed Avro schemas. Reuse one per path so repeated runtime lookups (e.g. Record#schema_fields across many records) don't re-read and re-parse the .avsc files each time.
25 26 27 |
# File 'lib/avro_gen/schema_validator.rb', line 25 def store_for(path) (@stores ||= {})[path] ||= SchemaRegistry::AvroSchemaStore.new(path: path) end |
Instance Method Details
#avro_schema(schema = nil) ⇒ Avro::Schema::NamedSchema
60 61 62 63 |
# File 'lib/avro_gen/schema_validator.rb', line 60 def avro_schema(schema=nil) schema ||= @schema schema_store.find("#{@namespace}.#{schema}") end |
#load_schema ⇒ Avro::Schema
Forcefully loads the schema into memory.
55 56 57 |
# File 'lib/avro_gen/schema_validator.rb', line 55 def load_schema avro_schema end |
#schema_fields ⇒ Array<AvroGen::SchemaField>
66 67 68 69 70 71 |
# File 'lib/avro_gen/schema_validator.rb', line 66 def schema_fields avro_schema.fields.map do |field| enum_values = field.type.type == 'enum' ? field.type.symbols : [] AvroGen::SchemaField.new(field.name, field.type, enum_values, field.default) end end |
#schema_store ⇒ SchemaRegistry::AvroSchemaStore
49 50 51 |
# File 'lib/avro_gen/schema_validator.rb', line 49 def schema_store @schema_store ||= SchemaRegistry::AvroSchemaStore.new(path: @path) end |
#validate(payload, schema: nil) ⇒ void
This method returns an undefined value.
74 75 76 77 78 |
# File 'lib/avro_gen/schema_validator.rb', line 74 def validate(payload, schema: nil) Avro::SchemaValidator.validate!(avro_schema(schema), payload, recursive: true, fail_on_extra_fields: true) end |