Sha256: 60eda3e9df75726d83aa6c64dfbf1a4eba8a19bb292802e4b5c24e90da12a523

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

require 'test/unit'
require File.dirname(__FILE__) + '/../lib/json-schema'

class AnyOfRefSchemaTest < Test::Unit::TestCase
  def test_any_of_ref_schema
    schema = File.join(File.dirname(__FILE__),"schemas/any_of_ref_schema.json")
    data = File.join(File.dirname(__FILE__),"data/any_of_ref_data.json")
    errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
    assert(errors.empty?, errors.map{|e| e[:message] }.join("\n"))
  end

  def test_any_of_ref_subschema_errors
    schema = File.join(File.dirname(__FILE__),'schemas/any_of_ref_schema.json')
    data = %({"names": ["jack"]})
    errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
    nested_errors = errors[0][:errors]
    assert_equal([:anyof_0, :anyof_1, :anyof_2], nested_errors.keys, 'should have nested errors for each anyOf subschema')
    assert_match(/the property '#\/names\/0' value "jack" did not match the regex 'john'/i, nested_errors[:anyof_0][0][:message])
    assert_match(/the property '#\/names\/0' value "jack" did not match the regex 'jane'/i, nested_errors[:anyof_1][0][:message])
    assert_match(/the property '#\/names\/0' value "jack" did not match the regex 'jimmy'/i, nested_errors[:anyof_2][0][:message])
  end

  def test_any_of_ref_message
    schema = File.join(File.dirname(__FILE__),'schemas/any_of_ref_schema.json')
    data = %({"names": ["jack"]})
    errors = JSON::Validator.fully_validate(schema, data)
    expected_message = """The property '#/names/0' of type String did not match one or more of the required schemas. The schema specific errors were:

- anyOf #0:
    - The property '#/names/0' value \"jack\" did not match the regex 'john'
- anyOf #1:
    - The property '#/names/0' value \"jack\" did not match the regex 'jane'
- anyOf #2:
    - The property '#/names/0' value \"jack\" did not match the regex 'jimmy'"""
    assert_equal(expected_message, errors[0])
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
json-schema-2.4.1 test/test_any_of_ref_schema.rb
json-schema-2.4.0 test/test_any_of_ref_schema.rb
json-schema-2.3.0 test/test_any_of_ref_schema.rb