Module: EasyJSONMatcher

Defined in:
lib/easy_json_matcher/validation_step.rb,
lib/easy_json_matcher.rb,
lib/easy_json_matcher/node.rb,
lib/easy_json_matcher/version.rb,
lib/easy_json_matcher/container.rb,
lib/easy_json_matcher/validator.rb,
lib/easy_json_matcher/exceptions.rb,
lib/easy_json_matcher/json_coercer.rb,
lib/easy_json_matcher/validator_set.rb,
lib/easy_json_matcher/coercion_error.rb,
lib/easy_json_matcher/node_generator.rb,
lib/easy_json_matcher/schema_library.rb,
lib/easy_json_matcher/array_generator.rb,
lib/easy_json_matcher/array_validator.rb,
lib/easy_json_matcher/content_wrapper.rb,
lib/easy_json_matcher/schema_generator.rb,
lib/easy_json_matcher/validation_rules.rb,
lib/easy_json_matcher/attribute_generator.rb,
lib/easy_json_matcher/array_content_validator.rb,
lib/easy_json_matcher/easy_json_matcher_error.rb,
lib/easy_json_matcher/validation_chain_factory.rb,
lib/easy_json_matcher/unknown_validation_step_error.rb

Overview

The ValidationStep class represents a step in the process of validating a value. Each ValidationStep instance can be chained to another ValidationStep instance in order to produce a defined process for running validations within a given Validator. A use case for such a procedure is where a value is required and of a certain type. In this case the validation for required must precede the check for the type, and validation must cease if the value is nil.

Defined Under Namespace

Modules: ContentWrapper Classes: ArrayContentValidator, ArrayGenerator, ArrayValidator, AttributeGenerator, CoercionError, Container, EasyJSONMatcherError, Error, JsonCoercer, MissingSchemaException, Node, NodeGenerator, SchemaGenerator, SchemaLibrary, UnknownValidationStepError, ValidationChainFactory, ValidationStep, Validator, ValidatorSet

Constant Summary collapse

TYPES =
[:number, :object, :value, :string, :boolean, :date, :array]
IMPORT =
Dry::AutoInject(Container)
VERSION =
"0.4.1".freeze
AutoInject =
Dry::AutoInject(Container)
VALIDATION_RULES =
{
  object: -> (value, errors) {
  unless value.is_a? Hash
    errors << "#{value} is not an Object"
    return false
  end
},
  string: -> (value, errors) {
  unless value.is_a? String
    errors << "#{value} is not a String"
    return false
  end
},
  number: -> (value, errors){
  error_message = "#{value} is not a Number"
  begin
    Kernel::Float(value)
  rescue ArgumentError, TypeError
    errors << error_message
    false
  end
},
  date: ->(value, errors){
  require "date"
  error_message = "#{value} is not a valid SQL date"
  begin
    Date.strptime(value,"%Y-%m-%d")
  rescue ArgumentError, TypeError
    errors << error_message
  end
},
  boolean: ->(value, errors){
  clazz = value.class
  unless [ TrueClass, FalseClass].include? clazz
    errors << "#{value} is not a Boolean"
    false
  end
},
  array: ->(value, errors){
  unless value.is_a? Array
    errors << "Value was not an array"
    false
  end
},
  value: ->(value, errors){
  # This is a bit of a toughie since value can be any value, including nil
},
  not_required: ->(value, errors){
  false if value.nil?
},
  required: ->(value, errors){
    errors << "no value found" and return false if value.nil?
}
}