Skip to content

Commit

Permalink
Create Processor class for storing errors (and anything else) that ch…
Browse files Browse the repository at this point in the history
…anges during validation.
  • Loading branch information
glennpratt committed Oct 5, 2015
1 parent 7d2f38b commit bf8e3bf
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ module JSON

class Validator

class Processor
def initialize
@errors = []
end

def validation_error(error)
@errors.push(error)
end

def validation_errors
@errors
end
end

@@schemas = {}
@@cache_schemas = false
@@default_opts = {
Expand All @@ -37,8 +51,8 @@ class Validator

def initialize(schema_data, data=nil, opts={})
@options = @@default_opts.clone.merge(opts)
@errors = []
@data = data
# This instance var is deprecated, please don't use it.
@_data = data

validator = JSON::Validator.validator_for_name(@options[:version])
@options[:version] = validator
Expand Down Expand Up @@ -112,19 +126,20 @@ def schema_from_fragment(base_schema, fragment)

# Run a simple true/false validation of data against a schema
def validate()
validate_data(@data)
validate_data(@_data, @options)
end

# Validate the provided data against the schema this instance was created
# with.
def validate_data(data)
def validate_data(data, options = {})
original_data = data
data = initialize_data(data)
@base_schema.validate(data,[],self,@validation_options)
if @options[:errors_as_objects]
return @errors.map{|e| e.to_hash}
processor = Processor.new
@base_schema.validate(data, [], processor, @validation_options)
if options[:errors_as_objects]
return processor.validation_errors.map{|e| e.to_hash}
else
return @errors.map{|e| e.to_string}
return processor.validation_errors.map{|e| e.to_string}
end
ensure
if @validation_options[:clear_cache] == true
Expand Down Expand Up @@ -238,15 +253,6 @@ def handle_schema(parent_schema, obj)
end
end

def validation_error(error)
@errors.push(error)
end

def validation_errors
@errors
end


class << self
def validate(schema, data,opts={})
begin
Expand Down

0 comments on commit bf8e3bf

Please sign in to comment.