Skip to content

Commit

Permalink
Allow passing options to schema validation methods
Browse files Browse the repository at this point in the history
The default meta schemas don't inherit the provided options, because
they're initialized with their own options and cached. To override
validation-time options, they need to be passed directly to the
validation methods.

This is a little brittle because the validation-time options are listed
separately to pull them out of the options hash. I couldn't think of a
better way to split the initialization and validation options.
  • Loading branch information
davishmcclurg committed Feb 25, 2024
1 parent bbcd0ce commit 2eeef77
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/json_schemer.rb
Expand Up @@ -123,12 +123,12 @@ def schema(schema, meta_schema: draft202012, **options)

def valid_schema?(schema, **options)
schema = resolve(schema, options)
meta_schema(schema, options).valid?(schema)
meta_schema(schema, options).valid?(schema, **options.slice(:output_format, :resolve_enumerators, :access_mode))
end

def validate_schema(schema, **options)
schema = resolve(schema, options)
meta_schema(schema, options).validate(schema)
meta_schema(schema, options).validate(schema, **options.slice(:output_format, :resolve_enumerators, :access_mode))
end

def draft202012
Expand Down
8 changes: 4 additions & 4 deletions lib/json_schemer/schema.rb
Expand Up @@ -118,12 +118,12 @@ def validate(instance, output_format: @output_format, resolve_enumerators: @reso
output
end

def valid_schema?
meta_schema.valid?(value)
def valid_schema?(**options)
meta_schema.valid?(value, **options)
end

def validate_schema
meta_schema.validate(value)
def validate_schema(**options)
meta_schema.validate(value, **options)
end

def ref(value)
Expand Down
37 changes: 37 additions & 0 deletions test/json_schemer_test.rb
Expand Up @@ -557,6 +557,43 @@ def test_schema_validation_pathname
assert_empty(JSONSchemer.validate_schema(schema_items_array).to_a)
end

def test_schema_validation_options
custom_meta_schemer = JSONSchemer.schema({
'$vocabulary' => {},
'properties' => {
'yah' => {
'readOnly' => true
}
}
})
read_only_schemer = JSONSchemer.schema({ 'yah' => 1 }, meta_schema: custom_meta_schemer)
invalid_ref_schemer = JSONSchemer.schema({ '$ref' => {} })

assert(JSONSchemer.valid_schema?({ 'yah' => 1 }, meta_schema: custom_meta_schemer))
assert(JSONSchemer.valid_schema?({ 'yah' => 1 }, meta_schema: custom_meta_schemer, access_mode: 'read'))
refute(JSONSchemer.valid_schema?({ 'yah' => 1 }, meta_schema: custom_meta_schemer, access_mode: 'write'))

assert(read_only_schemer.valid_schema?)
assert(read_only_schemer.valid_schema?(access_mode: 'read'))
refute(read_only_schemer.valid_schema?(access_mode: 'write'))

assert_equal(['string'], JSONSchemer.validate_schema({ '$schema' => {} }).map { |result| result.fetch('type') })
refute(JSONSchemer.validate_schema({ '$schema' => {} }, output_format: 'basic').fetch('valid'))
assert_kind_of(Enumerator, JSONSchemer.validate_schema({ '$schema' => {} }, output_format: 'basic').fetch('errors'))
assert_kind_of(Array, JSONSchemer.validate_schema({ '$schema' => {} }, output_format: 'basic', resolve_enumerators: true).fetch('errors'))
assert_empty(JSONSchemer.validate_schema({ 'yah' => 1 }, meta_schema: custom_meta_schemer).to_a)
assert_empty(JSONSchemer.validate_schema({ 'yah' => 1 }, meta_schema: custom_meta_schemer, access_mode: 'read').to_a)
refute_empty(JSONSchemer.validate_schema({ 'yah' => 1 }, meta_schema: custom_meta_schemer, access_mode: 'write').to_a)

assert_equal(['string'], invalid_ref_schemer.validate_schema.map { |result| result.fetch('type') })
refute(invalid_ref_schemer.validate_schema(output_format: 'basic').fetch('valid'))
assert_kind_of(Enumerator, invalid_ref_schemer.validate_schema(output_format: 'basic').fetch('errors'))
assert_kind_of(Array, invalid_ref_schemer.validate_schema(output_format: 'basic', resolve_enumerators: true).fetch('errors'))
assert_empty(read_only_schemer.validate_schema.to_a)
assert_empty(read_only_schemer.validate_schema(access_mode: 'read').to_a)
refute_empty(read_only_schemer.validate_schema(access_mode: 'write').to_a)
end

def test_non_string_keys
schemer = JSONSchemer.schema({
properties: {
Expand Down

0 comments on commit 2eeef77

Please sign in to comment.