Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create method to Validator that accepts data argument. #267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 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 @@ -35,9 +49,10 @@ class Validator
@@serializer = nil
@@mutex = Mutex.new

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

validator = JSON::Validator.validator_for_name(@options[:version])
@options[:version] = validator
Expand All @@ -49,8 +64,6 @@ def initialize(schema_data, data, opts={})
@validation_options[:clear_cache] = false if @options[:clear_cache] == false

@@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
@original_data = data
@data = initialize_data(data)
@@mutex.synchronize { build_schemas(@base_schema) }

# validate the schema, if requested
Expand Down Expand Up @@ -113,18 +126,27 @@ def schema_from_fragment(base_schema, fragment)

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

# Validate the provided data against the schema this instance was created
# with.
def validate_data(data, options = {})
original_data = data
data = initialize_data(data)
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
Validator.clear_cache
end
if @validation_options[:insert_defaults]
JSON::Validator.merge_missing_values(@data, @original_data)
JSON::Validator.merge_missing_values(data, original_data)
end
end

Expand Down Expand Up @@ -231,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