Class: AvroGen::SchemaValidator

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, namespace:, path: nil, store: nil) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.

Parameters:

  • schema (String)
  • namespace (String)
  • path (String) (defaults to: nil)

    location of .avsc files; defaults to the configured schema_path

  • store (SchemaRegistry::AvroSchemaStore) (defaults to: nil)

    an existing store to reuse; when omitted a fresh, isolated store is created (the generator relies on this).



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

#namespaceString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/avro_gen/schema_validator.rb', line 15

def namespace
  @namespace
end

#schemaString (readonly)

Returns:

  • (String)


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.

Parameters:

  • path (String)

Returns:

  • (SchemaRegistry::AvroSchemaStore)


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

Returns:

  • (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_schemaAvro::Schema

Forcefully loads the schema into memory.

Returns:

  • (Avro::Schema)


55
56
57
# File 'lib/avro_gen/schema_validator.rb', line 55

def load_schema
  avro_schema
end

#schema_fieldsArray<AvroGen::SchemaField>

Returns:



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_storeSchemaRegistry::AvroSchemaStore

Returns:

  • (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