Class: Commands::ValidateSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/validate_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidateSchema

Returns a new instance of ValidateSchema.



14
15
16
17
18
19
20
21
# File 'lib/commands/validate_schema.rb', line 14

def initialize
  @detect = false
  @fail_fast = false
  @extra_schemas = []

  @errors = []
  @messages = []
end

Instance Attribute Details

#detectObject

Returns the value of attribute detect.



7
8
9
# File 'lib/commands/validate_schema.rb', line 7

def detect
  @detect
end

#errorsObject

Returns the value of attribute errors.



11
12
13
# File 'lib/commands/validate_schema.rb', line 11

def errors
  @errors
end

#extra_schemasObject

Returns the value of attribute extra_schemas.



9
10
11
# File 'lib/commands/validate_schema.rb', line 9

def extra_schemas
  @extra_schemas
end

#fail_fastObject

Returns the value of attribute fail_fast.



8
9
10
# File 'lib/commands/validate_schema.rb', line 8

def fail_fast
  @fail_fast
end

#messagesObject

Returns the value of attribute messages.



12
13
14
# File 'lib/commands/validate_schema.rb', line 12

def messages
  @messages
end

Instance Method Details

#run(argv) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
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
# File 'lib/commands/validate_schema.rb', line 23

def run(argv)
  return false if !initialize_store

  if !detect
    return false if !(schema_file = argv.shift)
    return false if !(schema = parse(schema_file))
  end

  # if there are no remaining files in arguments, also a problem
  return false if argv.count < 1

  argv.each do |data_file|
    if !(data = read_file(data_file))
      return false
    end

    if detect
      if !(schema_uri = data["$schema"])
        @errors = ["#{data_file}: No $schema tag for detection."]
        return false
      end

      if !(schema = @store.lookup_schema(schema_uri))
        @errors = ["#{data_file}: Unknown $schema, try specifying one with -s."]
        return false
      end
    end

    valid, errors = schema.validate(data, fail_fast: fail_fast)

    if valid
      @messages += ["#{data_file} is valid."]
    else
      @errors = map_schema_errors(data_file, errors)
    end
  end

  @errors.empty?
end