Class: ElasticGraph::JSONSchema::Validator
- Inherits:
-
Object
- Object
- ElasticGraph::JSONSchema::Validator
- 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
-
#sanitize_pii ⇒ Boolean
readonly
Whether to omit data that may contain PII from error messages.
-
#schema ⇒ Hash<String, Object>
readonly
A JSON schema.
Instance Method Summary collapse
-
#valid?(data) ⇒ Boolean
Validates the given data against the JSON schema, returning true if the data is valid.
-
#validate(data) ⇒ Array<Hash<String, Object>>
Validates the given data against the JSON schema, returning an array of error objects for any validation errors.
-
#validate_with_error_message(data) ⇒ nil, String
Validates the given data against the JSON schema, returning an error message string if it is invalid.
Instance Attribute Details
#sanitize_pii ⇒ Boolean (readonly)
Returns 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 (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 |
#schema ⇒ Hash<String, Object> (readonly)
Returns 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 (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.
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.
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
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.
58 59 60 61 62 63 64 65 |
# File 'lib/elastic_graph/json_schema/validator.rb', line 58 def (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 |