Class: ElasticGraph::Support::JSONSchema::ValidatorFactory
- Inherits:
-
Object
- Object
- ElasticGraph::Support::JSONSchema::ValidatorFactory
- Defined in:
- elasticgraph-support/lib/elastic_graph/support/json_schema/validator_factory.rb
Overview
Factory class responsible for creating Validators for particular ElasticGraph types.
Instance Method Summary collapse
-
#initialize(schema:, sanitize_pii:) ⇒ ValidatorFactory
constructor
A new instance of ValidatorFactory.
-
#validator_for(type_name) ⇒ Validator
Gets the Validator for a particular ElasticGraph type.
-
#with_unknown_properties_disallowed(except: []) ⇒ ValidatorFactory
Returns a new factory configured to disallow unknown properties.
Constructor Details
#initialize(schema:, sanitize_pii:) ⇒ ValidatorFactory
Returns a new instance of ValidatorFactory.
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 |
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator_factory.rb', line 24 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 ecma-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 ecma 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 = ThreadSafeCache.new # : ThreadSafeCache[::String, Validator] end |
Instance Method Details
#validator_for(type_name) ⇒ Validator
Gets the ElasticGraph::Support::JSONSchema::Validator for a particular ElasticGraph type.
54 55 56 57 58 59 60 61 |
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator_factory.rb', line 54 def validator_for(type_name) @validators_by_type_name.fetch_or_store(type_name) do Validator.new( schema: root_schema.ref("#/$defs/#{type_name}"), sanitize_pii: @sanitize_pii ) end 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.
70 71 72 73 74 75 76 |
# File 'elasticgraph-support/lib/elastic_graph/support/json_schema/validator_factory.rb', line 70 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 |