Class: ElasticGraph::JSONSchema::ValidatorFactory
- Inherits:
-
Object
- Object
- ElasticGraph::JSONSchema::ValidatorFactory
- Defined in:
- lib/elastic_graph/json_schema/validator_factory.rb
Instance Attribute Summary collapse
-
#root_schema ⇒ Object
readonly
Returns the value of attribute root_schema.
Instance Method Summary collapse
-
#initialize(schema:, sanitize_pii:) ⇒ ValidatorFactory
constructor
A new instance of ValidatorFactory.
- #validator_for(type_name) ⇒ Object
-
#with_unknown_properties_disallowed(except: []) ⇒ Object
Changes the matcher to disallow additional properties.
Constructor Details
#initialize(schema:, sanitize_pii:) ⇒ ValidatorFactory
Returns a new instance of ValidatorFactory.
18 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 |
# File 'lib/elastic_graph/json_schema/validator_factory.rb', line 18 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| # Create a copy of the schema with a ref to the type merged at the root. # This ensures that any relative $ref paths will correctly resolve, while # specifying the type we want to validate against using $ref. hash[key] = Validator.new( schema: root_schema.ref("#/$defs/#{key}"), sanitize_pii: sanitize_pii ) end end |
Instance Attribute Details
#root_schema ⇒ Object (readonly)
Returns the value of attribute root_schema.
16 17 18 |
# File 'lib/elastic_graph/json_schema/validator_factory.rb', line 16 def root_schema @root_schema end |
Instance Method Details
#validator_for(type_name) ⇒ Object
52 53 54 |
# File 'lib/elastic_graph/json_schema/validator_factory.rb', line 52 def validator_for(type_name) @validators_by_type_name[type_name] end |
#with_unknown_properties_disallowed(except: []) ⇒ Object
Changes the matcher to disallow additional properties. Property paths specified in ‘except` will be allowed.
58 59 60 61 62 63 64 |
# File 'lib/elastic_graph/json_schema/validator_factory.rb', line 58 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 |