Skip to content

Commit

Permalink
Made sure all validate methods go through the same call chain
Browse files Browse the repository at this point in the history
Right now when a schema is validated, we perform a different call path
depending on which of the validate methods is called. Schema validation
has it's own path too.

I've changed this so everything is validated via
`JSON::Validator#validate!`, all other validate methods ultimately call
that. This means that the varied validate class methods on `Validator`
are simplified and just add or remove validation options.
  • Loading branch information
iainbeeston committed Jun 29, 2016
1 parent ab96e9a commit cc9be80
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- Made all `validate*` methods on `JSON::Validator` ultimately call `validate!`

## [2.6.2] - 2016-05-13

### Fixed
Expand Down
25 changes: 12 additions & 13 deletions lib/json-schema/validator.rb
Expand Up @@ -62,8 +62,7 @@ def initialize(schema_data, data, opts={})
end
metaschema = base_validator ? base_validator.metaschema : validator.metaschema
# Don't clear the cache during metaschema validation!
meta_validator = JSON::Validator.new(metaschema, @base_schema.schema, {:clear_cache => false})
meta_validator.validate
self.class.validate!(metaschema, @base_schema.schema, {:clear_cache => false})
end

# If the :fragment option is set, try and validate against the fragment
Expand Down Expand Up @@ -110,12 +109,17 @@ def schema_from_fragment(base_schema, fragment)
end

# Run a simple true/false validation of data against a schema
def validate()
def validate
@base_schema.validate(@data,[],self,@validation_options)
if @options[:errors_as_objects]
return @errors.map{|e| e.to_hash}

if @options[:record_errors]
if @options[:errors_as_objects]
@errors.map{|e| e.to_hash}
else
@errors.map{|e| e.to_string}
end
else
return @errors.map{|e| e.to_string}
true
end
ensure
if @validation_options[:clear_cache] == true
Expand Down Expand Up @@ -239,9 +243,7 @@ def validation_errors
class << self
def validate(schema, data,opts={})
begin
validator = JSON::Validator.new(schema, data, opts)
validator.validate
return true
validate!(schema, data, opts)
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
return false
end
Expand All @@ -258,7 +260,6 @@ def validate_uri(schema, data, opts={})
def validate!(schema, data,opts={})
validator = JSON::Validator.new(schema, data, opts)
validator.validate
return true
end
alias_method 'validate2', 'validate!'

Expand All @@ -271,9 +272,7 @@ def validate_uri!(schema, data, opts={})
end

def fully_validate(schema, data, opts={})
opts[:record_errors] = true
validator = JSON::Validator.new(schema, data, opts)
validator.validate
validate!(schema, data, opts.merge(:record_errors => true))
end

def fully_validate_schema(schema, opts={})
Expand Down

0 comments on commit cc9be80

Please sign in to comment.