Class: ElasticGraph::Support::JSONSchema::Validator

Inherits:
Object
  • Object
show all
Defined in:
elasticgraph-support/lib/elastic_graph/support/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



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator.rb', line 21

class Validator < MemoizableData.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

  # Merges default values defined in the JSON schema into the given `data` without mutating it.
  #
  # @param data [Object] JSON data to populate with defaults from the schema
  # @return [Object] data updated with defaults for missing properties
  def merge_defaults(data)
    ::Marshal.load(::Marshal.dump(data)).tap do |deep_duped_data|
      defaulting_schema.valid?(deep_duped_data)
    end
  end

  private

  def defaulting_schema
    @defaulting_schema ||= begin
      config = schema.configuration.dup
      config.insert_property_defaults = true
      ::JSONSchemer.schema(schema.value, configuration: config)
    end
  end
end

#schemaHash<String, Object> (readonly)

Returns a JSON schema.

Returns:

  • (Hash<String, Object>)

    a JSON schema



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator.rb', line 21

class Validator < MemoizableData.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

  # Merges default values defined in the JSON schema into the given `data` without mutating it.
  #
  # @param data [Object] JSON data to populate with defaults from the schema
  # @return [Object] data updated with defaults for missing properties
  def merge_defaults(data)
    ::Marshal.load(::Marshal.dump(data)).tap do |deep_duped_data|
      defaulting_schema.valid?(deep_duped_data)
    end
  end

  private

  def defaulting_schema
    @defaulting_schema ||= begin
      config = schema.configuration.dup
      config.insert_property_defaults = true
      ::JSONSchemer.schema(schema.value, configuration: config)
    end
  end
end

Instance Method Details

#merge_defaults(data) ⇒ Object

Merges default values defined in the JSON schema into the given data without mutating it.

Parameters:

  • data (Object)

    JSON data to populate with defaults from the schema

Returns:

  • (Object)

    data updated with defaults for missing properties



73
74
75
76
77
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator.rb', line 73

def merge_defaults(data)
  ::Marshal.load(::Marshal.dump(data)).tap do |deep_duped_data|
    defaulting_schema.valid?(deep_duped_data)
  end
end

#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:



29
30
31
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator.rb', line 29

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:



41
42
43
44
45
46
47
48
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator.rb', line 41

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:



60
61
62
63
64
65
66
67
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator.rb', line 60

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