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/printer.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

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, Node, NodeGenerator, Printer, SchemaGenerator, SchemaLibrary, UnknownValidationStepError, ValidationChainFactory, ValidationRule, ValidationStep, Validator, ValidatorSet

Constant Summary collapse

TYPES =
[:number, :object, :value, :string, :boolean, :date, :array]
IMPORT =
Dry::AutoInject(Container)
VERSION =
"0.5.0".freeze
AutoInject =
Dry::AutoInject(Container)
VALIDATION_RULES =
{
  object: ValidationRule.new(:object, -> (value, errors) {
    unless value.is_a? Hash
      errors << "#{value} is not an Object"
      return false
    end
  }),

  string: ValidationRule.new(:string, -> (value, errors) {
    unless value.is_a? String
      errors << "#{value} is not a String"
      return false
    end
  }),

  number: ValidationRule.new(:number, -> (value, errors){
    error_message = "#{value} is not a Number"
    begin
      Kernel::Float(value)
    rescue ArgumentError, TypeError
      errors << error_message
      false
    end
  }),

  date: ValidationRule.new(: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: ValidationRule.new(:boolean, ->(value, errors){
    clazz = value.class
    unless [ TrueClass, FalseClass].include? clazz
      errors << "#{value} is not a Boolean"
      false
    end
  }),

  array: ValidationRule.new(:array, ->(value, errors){
    unless value.is_a? Array
      errors << "Value was not an array"
      false
    end
  }),

  value: ValidationRule.new(:value, ->(value, errors){
    # This is a bit of a toughie since value can be any value, including nil
  }),

  not_required: ValidationRule.new(:not_required, ->(value, errors){
    false if value.nil?
  }),

  required: ValidationRule.new(:required, ->(value, errors){
    errors << "no value found" and return false if value.nil?
  })
}