Class: ElasticGraph::JSONSchema::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/json_schema/validator.rb

Instance Method Summary collapse

Instance Method Details

#valid?(data) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/elastic_graph/json_schema/validator.rb', line 14

def valid?(data)
  schema.valid?(data)
end

#validate(data) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/elastic_graph/json_schema/validator.rb', line 18

def validate(data)
  schema.validate(data).map do |error|
    # The schemas can be very large and make the output very noisy, hiding what matters. So we remove them here.
    error.delete("root_schema")
    error.delete("schema")
    error
  end
end

#validate_with_error_message(data) ⇒ Object

Validates the given data against the schema, returning an error message if it is invalid. The error message is suitable for throwing or logging, as we take care to ensure it contains no PII.



30
31
32
33
34
35
36
37
# File 'lib/elastic_graph/json_schema/validator.rb', line 30

def validate_with_error_message(data)
  errors = validate(data)
  return if errors.empty?

  errors.each { |error| error.delete("data") } if sanitize_pii

  "Validation errors:\n\n#{errors.map { |e| ::JSON.pretty_generate(e) }.join("\n\n")}"
end