Class: Vkit::Policy::ValidateBundle

Inherits:
Object
  • Object
show all
Defined in:
lib/vkit/policy/validate_bundle.rb

Defined Under Namespace

Classes: ValidationError

Class Method Summary collapse

Class Method Details

.call!(bundle_path:, schema_path:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vkit/policy/validate_bundle.rb', line 7

def self.call!(bundle_path:, schema_path:)
  bundle_path = File.expand_path(bundle_path)
  schema_path = File.expand_path(schema_path)

  raise "Bundle not found: #{bundle_path}" unless File.exist?(bundle_path)
  raise "Schema not found: #{schema_path}" unless File.exist?(schema_path)

  bundle = JSON.parse(File.read(bundle_path))
  schema = JSON.parse(File.read(schema_path))

  schemer = JSONSchemer.schema(schema)
  errors = schemer.validate(bundle).to_a

  if errors.any?
    raise ValidationError.new(errors)
  end

  true
end