Class: ElasticGraph::JSONSchema::Validator

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

Overview

Responsible for validating JSON data against the ElasticGraph JSON schema for a particular type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sanitize_piiBoolean (readonly)

Returns whether to omit data that may contain PII from error messages.

Returns:

  • (Boolean)

    whether to omit data that may contain PII from error messages



19
20
21
22
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
62
63
64
65
66
# File 'lib/elastic_graph/json_schema/validator.rb', line 19

class Validator < ::Data.define(:schema, :sanitize_pii)
  # Validates the given data against the JSON schema, returning true if the data is valid.
  #
  # @param data [Object] JSON data to validate
  # @return [Boolean] true if the data is valid; false if it is invalid
  #
  # @see #validate
  # @see #validate_with_error_message
  def valid?(data)
    schema.valid?(data)
  end

  # Validates the given data against the JSON schema, returning an array of error objects for
  # any validation errors.
  #
  # @param data [Object] JSON data to validate
  # @return [Array<Hash<String, Object>>] validation errors; will be empty if `data` is valid
  #
  # @see #valid?
  # @see #validate_with_error_message
  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

  # Validates the given data against the JSON schema, returning an error message string if it is invalid.
  # The error message is intended to be usable to include in a log message or a raised error.
  #
  # @param data [Object] JSON data to validate
  # @return [nil, String] a validation error message, if the data is invalid
  #
  # @note The returned error message may contain PII unless {#sanitize_pii} has not been set.
  #
  # @see #valid?
  # @see #validate
  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
end

#schemaHash<String, Object> (readonly)

Returns a JSON schema.

Returns:

  • (Hash<String, Object>)

    a JSON schema



19
20
21
22
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
62
63
64
65
66
# File 'lib/elastic_graph/json_schema/validator.rb', line 19

class Validator < ::Data.define(:schema, :sanitize_pii)
  # Validates the given data against the JSON schema, returning true if the data is valid.
  #
  # @param data [Object] JSON data to validate
  # @return [Boolean] true if the data is valid; false if it is invalid
  #
  # @see #validate
  # @see #validate_with_error_message
  def valid?(data)
    schema.valid?(data)
  end

  # Validates the given data against the JSON schema, returning an array of error objects for
  # any validation errors.
  #
  # @param data [Object] JSON data to validate
  # @return [Array<Hash<String, Object>>] validation errors; will be empty if `data` is valid
  #
  # @see #valid?
  # @see #validate_with_error_message
  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

  # Validates the given data against the JSON schema, returning an error message string if it is invalid.
  # The error message is intended to be usable to include in a log message or a raised error.
  #
  # @param data [Object] JSON data to validate
  # @return [nil, String] a validation error message, if the data is invalid
  #
  # @note The returned error message may contain PII unless {#sanitize_pii} has not been set.
  #
  # @see #valid?
  # @see #validate
  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
end

Instance Method Details

#valid?(data) ⇒ Boolean

Validates the given data against the JSON schema, returning true if the data is valid.

Parameters:

  • data (Object)

    JSON data to validate

Returns:

  • (Boolean)

    true if the data is valid; false if it is invalid

See Also:



27
28
29
# File 'lib/elastic_graph/json_schema/validator.rb', line 27

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

#validate(data) ⇒ Array<Hash<String, Object>>

Validates the given data against the JSON schema, returning an array of error objects for any validation errors.

Parameters:

  • data (Object)

    JSON data to validate

Returns:

  • (Array<Hash<String, Object>>)

    validation errors; will be empty if ‘data` is valid

See Also:



39
40
41
42
43
44
45
46
# File 'lib/elastic_graph/json_schema/validator.rb', line 39

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) ⇒ nil, String

Note:

The returned error message may contain PII unless #sanitize_pii has not been set.

Validates the given data against the JSON schema, returning an error message string if it is invalid. The error message is intended to be usable to include in a log message or a raised error.

Parameters:

  • data (Object)

    JSON data to validate

Returns:

  • (nil, String)

    a validation error message, if the data is invalid

See Also:



58
59
60
61
62
63
64
65
# File 'lib/elastic_graph/json_schema/validator.rb', line 58

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