Class: Lyra::Schema::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/lyra/schema/validator.rb

Overview

Validates current schema against stored schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Validator

Returns a new instance of Validator.



9
10
11
12
13
14
# File 'lib/lyra/schema/validator.rb', line 9

def initialize
  @current_schema = nil
  @stored_schema = nil
  @differences = []
  @validated = false
end

Instance Attribute Details

#current_schema ⇒ Object (readonly)

Returns the value of attribute current_schema.



7
8
9
# File 'lib/lyra/schema/validator.rb', line 7

def current_schema
  @current_schema
end

#differences ⇒ Object (readonly)

Returns the value of attribute differences.



7
8
9
# File 'lib/lyra/schema/validator.rb', line 7

def differences
  @differences
end

#stored_schema ⇒ Object (readonly)

Returns the value of attribute stored_schema.



7
8
9
# File 'lib/lyra/schema/validator.rb', line 7

def stored_schema
  @stored_schema
end

Instance Method Details

#breaking_changes? ⇒ Boolean

Check if there are breaking changes

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/lyra/schema/validator.rb', line 28

def breaking_changes?
  validate! unless @validated
  @differences.any? { |d| d[:severity] == :breaking }
end

#enforce! ⇒ Object

Validate and enforce strict mode if configured



52
53
54
55
56
57
58
59
60
61
# File 'lib/lyra/schema/validator.rb', line 52

def enforce!
  return true if valid?

  if Lyra.config.strict_schema
    raise SchemaValidationError.new(report, @differences)
  else
    log_warning
    false
  end
end

#report ⇒ Object

Generate human-readable report



34
35
36
37
# File 'lib/lyra/schema/validator.rb', line 34

def report
  validate! unless @validated
  Diff.format_report(@differences)
end

#schema_exists? ⇒ Boolean

Check if stored schema exists

Returns:

  • (Boolean)


23
24
25
# File 'lib/lyra/schema/validator.rb', line 23

def schema_exists?
  Store.exists?
end

#valid? ⇒ Boolean

Check if schema is valid (no differences)

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/lyra/schema/validator.rb', line 17

def valid?
  validate! unless @validated
  @differences.empty?
end

#validate! ⇒ Object

Perform validation



40
41
42
43
44
45
46
47
48
49
# File 'lib/lyra/schema/validator.rb', line 40

def validate!
  @validated = true
  @stored_schema = Store.load_current
  @current_schema = Generator.generate

  return true if @stored_schema.nil?  # No schema yet = valid

  @differences = Diff.compare(@stored_schema, @current_schema)
  @differences.empty?
end