Class: GovukContentSchemaTestHelpers::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk-content-schema-test-helpers/validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_name, document) ⇒ Validator

schema_name should be a string, such as ‘finder’ document should be a JSON string of the document to validate



14
15
16
17
18
# File 'lib/govuk-content-schema-test-helpers/validator.rb', line 14

def initialize(schema_name, document)
  Util.check_govuk_content_schemas_path!
  @schema_path = GovukContentSchemaTestHelpers::Validator.schema_path(schema_name)
  @document = document
end

Class Method Details

.schema_path(schema_name) ⇒ Object

Return the first schema path that exists on the filesystem



6
7
8
9
10
# File 'lib/govuk-content-schema-test-helpers/validator.rb', line 6

def self.schema_path(schema_name)
  paths = candidate_schema_paths(schema_name)
  paths.detect { |path| File.exists?(path) } ||
    raise(ImproperlyConfiguredError, "Schema file not found at any of: #{paths.join(', ')}.")
end

Instance Method Details

#errorsObject



24
25
26
# File 'lib/govuk-content-schema-test-helpers/validator.rb', line 24

def errors
  @errors ||= JSON::Validator.fully_validate(@schema_path, @document)
end

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/govuk-content-schema-test-helpers/validator.rb', line 20

def valid?
  errors.empty?
end