elasticgraph-json_schema 0.18.0.4 → 0.18.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa5e24cb6adf6b25fc3ab3e27a715bdce089285056501efec8f7bb040d94a60a
4
- data.tar.gz: b6660a98fcb18e49a167d38a4de9da3b40eee99a5ac1d95e4beef1fc03c126a0
3
+ metadata.gz: 75d827eef96aa25f4944df19b2c6b23df331104ade84a814283735ffe14a93a1
4
+ data.tar.gz: b0faf506b264d67129755d90fbf637c545cf0b681f794480e69dfb0c39768a9e
5
5
  SHA512:
6
- metadata.gz: 87b39fb428e2ae601e992953378e318d5ef058594ab9c4e688d39f34e8986190220e13ee0f11f6fb800319203f13241f2aed7979cc38d3ffcc414b34e7f2954d
7
- data.tar.gz: 1c94607203e598b563598dbc8aa7f56c2b55e4c24bc71ba2586bec33c1b4777ac522d95b4f2f161a9577e111cb79d67b05d828da7a44b5d7c24f296391d624f3
6
+ metadata.gz: b3e92ed760e08cc9c2e5b4a117110c8ff65725f711aa7a6c76920adbf5e7064e4fea04f58c106921e61abda5cf97ba50c76ce36cc051c9354233e032dee8f105
7
+ data.tar.gz: 8431eaa5c6d7080c6dfb6f7f1e8446642ea4cbcb5324911fef8377936f16bc38b0c21213abe5cd83a35d8f313b22b388ad352c1357e2fe0f73f75d872a61eea4
@@ -12,14 +12,23 @@ require "elastic_graph/support/hash_util"
12
12
  require "json"
13
13
 
14
14
  module ElasticGraph
15
+ # Provides [JSON Schema](https://json-schema.org/) validation for ElasticGraph.
15
16
  module JSONSchema
16
- # Provides a strict validator based on the JSON schema spec that a JSON schema document is valid.
17
+ # Provides a validator to validate a JSON schema definitions according to the JSON schema meta schema.
18
+ # The validator is configured to validate strictly, so that non-standard JSON schema properties are disallowed.
19
+ #
20
+ # @return [Validator]
21
+ # @see .elastic_graph_internal_meta_schema_validator
17
22
  def self.strict_meta_schema_validator
18
23
  @strict_meta_schema_validator ||= MetaSchemaLoader.load_strict_validator
19
24
  end
20
25
 
21
- # Provides a validator that allows additional ElasticGraph metadata to be embedded
22
- # in JSON schema for internal ElasticGraph use.
26
+ # Provides a validator to validate a JSON schema definition according to the JSON schema meta schema.
27
+ # The validator is configured to validate strictly, so that non-standard JSON schema properties are disallowed,
28
+ # except for internal ElasticGraph metadata properties.
29
+ #
30
+ # @return [Validator]
31
+ # @see .strict_meta_schema_validator
23
32
  def self.elastic_graph_internal_meta_schema_validator
24
33
  @elastic_graph_internal_meta_schema_validator ||= MetaSchemaLoader.load_strict_validator({
25
34
  "properties" => {
@@ -35,7 +44,11 @@ module ElasticGraph
35
44
  })
36
45
  end
37
46
 
47
+ # Responsible for building {Validator}s that can validate JSON schema definitions.
38
48
  module MetaSchemaLoader
49
+ # Builds a validator to validate a JSON schema definition according to the JSON schema meta schema.
50
+ #
51
+ # @param overrides [Hash<String, Object>] meta schema overrides
39
52
  def self.load_strict_validator(overrides = {})
40
53
  # Downloaded from: https://json-schema.org/draft-07/schema
41
54
  schema = ::JSON.parse(::File.read(::File.expand_path("../json_schema_draft_7_schema.json", __FILE__)))
@@ -10,11 +10,32 @@ require "json"
10
10
 
11
11
  module ElasticGraph
12
12
  module JSONSchema
13
+ # Responsible for validating JSON data against the ElasticGraph JSON schema for a particular type.
14
+ #
15
+ # @!attribute [r] schema
16
+ # @return [Hash<String, Object>] a JSON schema
17
+ # @!attribute [r] sanitize_pii
18
+ # @return [Boolean] whether to omit data that may contain PII from error messages
13
19
  class Validator < ::Data.define(:schema, :sanitize_pii)
20
+ # Validates the given data against the JSON schema, returning true if the data is valid.
21
+ #
22
+ # @param data [Object] JSON data to validate
23
+ # @return [Boolean] true if the data is valid; false if it is invalid
24
+ #
25
+ # @see #validate
26
+ # @see #validate_with_error_message
14
27
  def valid?(data)
15
28
  schema.valid?(data)
16
29
  end
17
30
 
31
+ # Validates the given data against the JSON schema, returning an array of error objects for
32
+ # any validation errors.
33
+ #
34
+ # @param data [Object] JSON data to validate
35
+ # @return [Array<Hash<String, Object>>] validation errors; will be empty if `data` is valid
36
+ #
37
+ # @see #valid?
38
+ # @see #validate_with_error_message
18
39
  def validate(data)
19
40
  schema.validate(data).map do |error|
20
41
  # The schemas can be very large and make the output very noisy, hiding what matters. So we remove them here.
@@ -24,9 +45,16 @@ module ElasticGraph
24
45
  end
25
46
  end
26
47
 
27
- # Validates the given data against the schema, returning an error message if it is invalid.
28
- # The error message is suitable for throwing or logging, as we take care to ensure it contains
29
- # no PII.
48
+ # Validates the given data against the JSON schema, returning an error message string if it is invalid.
49
+ # The error message is intended to be usable to include in a log message or a raised error.
50
+ #
51
+ # @param data [Object] JSON data to validate
52
+ # @return [nil, String] a validation error message, if the data is invalid
53
+ #
54
+ # @note The returned error message may contain PII unless {#sanitize_pii} has not been set.
55
+ #
56
+ # @see #valid?
57
+ # @see #validate
30
58
  def validate_with_error_message(data)
31
59
  errors = validate(data)
32
60
  return if errors.empty?
@@ -11,10 +11,14 @@ require "json_schemer"
11
11
 
12
12
  module ElasticGraph
13
13
  module JSONSchema
14
+ # Factory class responsible for creating {Validator}s for particular ElasticGraph types.
14
15
  class ValidatorFactory
15
16
  # @dynamic root_schema
17
+ # @private
16
18
  attr_reader :root_schema
17
19
 
20
+ # @param schema [Hash<String, Object>] the JSON schema for an entire ElasticGraph schema
21
+ # @param sanitize_pii [Boolean] whether to omit data that may contain PII from error messages
18
22
  def initialize(schema:, sanitize_pii:)
19
23
  @raw_schema = schema
20
24
  @root_schema = ::JSONSchemer.schema(
@@ -39,9 +43,6 @@ module ElasticGraph
39
43
 
40
44
  @sanitize_pii = sanitize_pii
41
45
  @validators_by_type_name = ::Hash.new do |hash, key|
42
- # Create a copy of the schema with a ref to the type merged at the root.
43
- # This ensures that any relative $ref paths will correctly resolve, while
44
- # specifying the type we want to validate against using $ref.
45
46
  hash[key] = Validator.new(
46
47
  schema: root_schema.ref("#/$defs/#{key}"),
47
48
  sanitize_pii: sanitize_pii
@@ -49,12 +50,21 @@ module ElasticGraph
49
50
  end
50
51
  end
51
52
 
53
+ # Gets the {Validator} for a particular ElasticGraph type.
54
+ #
55
+ # @param type_name [String] name of an ElasticGraph type
56
+ # @return [Validator]
52
57
  def validator_for(type_name)
53
58
  @validators_by_type_name[type_name]
54
59
  end
55
60
 
56
- # Changes the matcher to disallow additional properties. Property paths specified in
57
- # `except` will be allowed.
61
+ # Returns a new factory configured to disallow unknown properties. By default, JSON schema
62
+ # allows unknown properties (they'll simply be ignored when validating a JSON document). It
63
+ # can be useful to validate more strictly, so that a document with properties not defined in
64
+ # the JSON schema gets validation errors.
65
+ #
66
+ # @param except [Array<String>] paths under which unknown properties should still be allowed
67
+ # @return [ValidatorFactory]
58
68
  def with_unknown_properties_disallowed(except: [])
59
69
  allow_paths = except.map { |p| p.split(".") }
60
70
  schema_copy = ::Marshal.load(::Marshal.dump(@raw_schema)) # deep copy so our mutations don't affect caller
metadata CHANGED
@@ -1,14 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticgraph-json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0.4
4
+ version: 0.18.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myron Marston
8
+ - Ben VandenBos
9
+ - Square Engineering
8
10
  autorequire:
9
11
  bindir: exe
10
12
  cert_chain: []
11
- date: 2024-09-06 00:00:00.000000000 Z
13
+ date: 2024-09-20 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: rubocop-factory_bot
@@ -176,14 +178,14 @@ dependencies:
176
178
  requirements:
177
179
  - - '='
178
180
  - !ruby/object:Gem::Version
179
- version: 0.18.0.4
181
+ version: 0.18.0.5
180
182
  type: :runtime
181
183
  prerelease: false
182
184
  version_requirements: !ruby/object:Gem::Requirement
183
185
  requirements:
184
186
  - - '='
185
187
  - !ruby/object:Gem::Version
186
- version: 0.18.0.4
188
+ version: 0.18.0.5
187
189
  - !ruby/object:Gem::Dependency
188
190
  name: json_schemer
189
191
  requirement: !ruby/object:Gem::Requirement