From cc9be807d09beb0c92ab92e3e0bba6570cb64e9a Mon Sep 17 00:00:00 2001 From: Iain Beeston Date: Wed, 29 Jun 2016 12:54:55 +0100 Subject: [PATCH] Made sure all validate methods go through the same call chain 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. --- CHANGELOG.md | 3 +++ lib/json-schema/validator.rb | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3610dc7a..1d9d6201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 9b947ff8..841751af 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -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 @@ -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 @@ -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 @@ -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!' @@ -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={})