class JSchema::SimpleValidator
Attributes
keywords[RW]
parent[R]
Public Class Methods
build(schema, parent)
click to toggle source
# File lib/jschema/simple_validator.rb, line 4 def build(schema, parent) args = schema.values_at(*keywords) new(*args, parent) unless args.compact.empty? end
new(*args, parent)
click to toggle source
# File lib/jschema/simple_validator.rb, line 16 def initialize(*args, parent) @parent = parent if validate_args(*args) post_initialize(*args) else fail InvalidSchema end end
Public Instance Methods
valid?(instance)
click to toggle source
# File lib/jschema/simple_validator.rb, line 26 def valid?(instance) validate(instance).nil? end
validate(instance)
click to toggle source
# File lib/jschema/simple_validator.rb, line 30 def validate(instance) if !applicable_type || instance.is_a?(applicable_type) validate_instance(instance) end end
Private Instance Methods
applicable_type()
click to toggle source
Hook method
# File lib/jschema/simple_validator.rb, line 39 def applicable_type; end
boolean?(value)
click to toggle source
Argument validation helpers
# File lib/jschema/simple_validator.rb, line 47 def boolean?(value) value.is_a?(TrueClass) || value.is_a?(FalseClass) end
greater_or_equal_to?(value, limit)
click to toggle source
# File lib/jschema/simple_validator.rb, line 55 def greater_or_equal_to?(value, limit) integer?(value) && value >= limit end
integer?(value)
click to toggle source
# File lib/jschema/simple_validator.rb, line 51 def integer?(value) value.is_a?(Integer) end
invalid_schema(keyword, value)
click to toggle source
# File lib/jschema/simple_validator.rb, line 41 def invalid_schema(keyword, value) fail InvalidSchema, "Invalid `#{keyword}` value: #{value.inspect}" end
non_empty_array?(value, uniqueness_check = true)
click to toggle source
# File lib/jschema/simple_validator.rb, line 63 def non_empty_array?(value, uniqueness_check = true) result = value.is_a?(Array) && !value.empty? if uniqueness_check result && value.size == value.uniq.size else result end end
number?(value)
click to toggle source
# File lib/jschema/simple_validator.rb, line 59 def number?(value) value.is_a?(Numeric) end
schema_array?(value, id, uniqueness_check = true)
click to toggle source
# File lib/jschema/simple_validator.rb, line 72 def schema_array?(value, id, uniqueness_check = true) non_empty_array?(value, uniqueness_check) && value.to_enum.with_index.all? do |schema, index| full_id = [id, index].join('/') valid_schema? schema, full_id end end
valid_schema?(schema, id)
click to toggle source
# File lib/jschema/simple_validator.rb, line 80 def valid_schema?(schema, id) schema.is_a?(Hash) && Schema.build(schema, parent, id) rescue InvalidSchema false end