Class: Jfuzz::SchemaFuzzer

Inherits:
Object
  • Object
show all
Defined in:
lib/jfuzz/schema_fuzzer.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_path) ⇒ SchemaFuzzer

Returns a new instance of SchemaFuzzer.



9
10
11
12
# File 'lib/jfuzz/schema_fuzzer.rb', line 9

def initialize(schema_path)
  @schema_path = schema_path
  @property_fuzzer = PropertyFuzzer.new
end

Instance Method Details

#fuzzObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jfuzz/schema_fuzzer.rb', line 14

def fuzz
  raise "Schema path cannot be nil or empty" if schema_path.to_s.nil?

  schema_contents = File.read(schema_path)
  schema = JSON.parse(schema_contents)

  fuzzed = property_fuzzer.fuzz_property(schema)

  json_schema = JsonSchema.parse!(schema)
  is_valid, validation_errors = json_schema.validate(fuzzed)

  unless is_valid
    raise "Error in the report validation. Produced invalid JSON file " \
      "according to JSON schema #{schema_path}. " \
      "Validation errors: #{validation_errors}"
  end

  fuzzed
end