Class: ElasticGraph::JSONSchema::ValidatorFactory

Inherits:
Object
  • Object
show all
Defined in:
elasticgraph-json_schema/lib/elastic_graph/json_schema/validator_factory.rb

Overview

Factory class responsible for creating Validators for particular ElasticGraph types.

Instance Method Summary collapse

Constructor Details

#initialize(schema:, sanitize_pii:) ⇒ ValidatorFactory

Returns a new instance of ValidatorFactory.

Parameters:

  • schema (Hash<String, Object>)

    the JSON schema for an entire ElasticGraph schema

  • sanitize_pii (Boolean)

    whether to omit data that may contain PII from error messages



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
# File 'elasticgraph-json_schema/lib/elastic_graph/json_schema/validator_factory.rb', line 22

def initialize(schema:, sanitize_pii:)
  @raw_schema = schema
  @root_schema = ::JSONSchemer.schema(
    schema,
    meta_schema: schema.fetch("$schema"),
    # Here we opt to have regular expressions resolved using an ecmo-compatible resolver, instead of Ruby's.
    #
    # We do this because regexp patterns in our JSON schema are intended to be used by JSON schema libraries
    # in many languages, not just in Ruby, and we want to support the widest compatibility. For example,
    # Ruby requires that we use `\A` and `\z` to anchor the start and end of the string (`^` and `$` anchor the
    # start and end of a line instead), where as ecmo regexes treat `^` and `$` as the start and end of the string.
    # For a pattern to be usable by non-Ruby publishers, we need to use `^/`$` for our start/end anchors, and we
    # want our validator to treat it the same way here.
    #
    # Also, this was the default before json_schemer 1.0 (and we used 0.x versions for a long time...).
    # This maintains the historical behavior we've had.
    #
    # For more info:
    # https://github.com/davishmcclurg/json_schemer/blob/v1.0.0/CHANGELOG.md#breaking-changes
    regexp_resolver: "ecma"
  )

  @sanitize_pii = sanitize_pii
  @validators_by_type_name = ::Hash.new do |hash, key|
    hash[key] = Validator.new(
      schema: root_schema.ref("#/$defs/#{key}"),
      sanitize_pii: sanitize_pii
    )
  end
end

Instance Method Details

#validator_for(type_name) ⇒ Validator

Gets the ElasticGraph::JSONSchema::Validator for a particular ElasticGraph type.

Parameters:

  • type_name (String)

    name of an ElasticGraph type

Returns:



57
58
59
# File 'elasticgraph-json_schema/lib/elastic_graph/json_schema/validator_factory.rb', line 57

def validator_for(type_name)
  @validators_by_type_name[type_name]
end

#with_unknown_properties_disallowed(except: []) ⇒ ValidatorFactory

Returns a new factory configured to disallow unknown properties. By default, JSON schema allows unknown properties (they’ll simply be ignored when validating a JSON document). It can be useful to validate more strictly, so that a document with properties not defined in the JSON schema gets validation errors.

Parameters:

  • except (Array<String>) (defaults to: [])

    paths under which unknown properties should still be allowed

Returns:



68
69
70
71
72
73
74
# File 'elasticgraph-json_schema/lib/elastic_graph/json_schema/validator_factory.rb', line 68

def with_unknown_properties_disallowed(except: [])
  allow_paths = except.map { |p| p.split(".") }
  schema_copy = ::Marshal.load(::Marshal.dump(@raw_schema)) # deep copy so our mutations don't affect caller
  prevent_unknown_properties!(schema_copy, allow_paths: allow_paths)

  ValidatorFactory.new(schema: schema_copy, sanitize_pii: @sanitize_pii)
end