Class: Data::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/data/validator.rb,
lib/data/validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule = {}) ⇒ Validator

(Maybe recursively) constructs a validator.

Parameters:

  • rule (Hash) (defaults to: {})

    Describes validation rule.

Options Hash (rule):

  • isa (String)

    Class to match.

  • :xor (<String>)

    ??? (please ask gfx).

  • :default (Object)

    Default value.

  • :optional (true, false)

    Omitable or not.

  • :allow_extra (true, false)

    Can have others or not.

  • :rule (Hash)

    Recursion rule.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/data/validator.rb', line 78

def initialize(rule = {})
  @isa  = rule.delete(:isa) || Object # ??
  @rule = rule
  if rule.has_key? :rule then
    raise TypeError, "rule must be a hash" unless rule[:rule].is_a? Hash

    case
    when @isa == Hash then
      recur = rule[:rule].each_pair.each_with_object Hash.new do |(k, v), r|
        case v when Hash then
          r[k] = self.class.send :bare_new, v
        else
          r[k] = self.class.send :bare_new, isa: v
        end
      end
      @rule = rule.merge rule: recur
    when @isa == Array then
      recur = self.class.new rule[:rule]
      @rule = rule.merge rule: recur
    end
  end
end

Class Method Details

.bare_newObject



29
# File 'lib/data/validator.rb', line 29

alias bare_new new

.new(hash) ⇒ Data::Validator .new(array) ⇒ Data::Validator .new(validator) ⇒ Data::Validator .new(object) ⇒ Data::Validator

Creates a new validator.

Overloads:

  • .new(hash) ⇒ Data::Validator

    This is the basic validator that validates one hash

    Parameters:

    • hash (Hash)

      Describes validation rule.

    Returns:

  • .new(array) ⇒ Data::Validator

    What a ‘->with(’Sequenced’)‘ intends in perl version. In perl a `(…)` can either be an array or a hash but in ruby they are different.

    Parameters:

    • array (<Hash>)

      Describes validation rule.

    Returns:

  • .new(validator) ⇒ Data::Validator

    For recursive call.

    Parameters:

    Returns:

  • .new(object) ⇒ Data::Validator

    Arbitrary objects can be specified, for simplicity.

    Parameters:

    • object (Object)

      Anything.

    Returns:



59
60
61
62
63
64
65
66
# File 'lib/data/validator.rb', line 59

def new rule
  case rule
  when self  then rule # already
  when Hash  then bare_new isa: Hash,  rule: rule
  when Array then bare_new isa: Array, rule: rule
  else            bare_new isa: rule
  end
end

Instance Method Details

#validate(actual = @isa.new) ⇒ Object

Note:

this does not modify the validator, but does modify the argument object.

Validates the input

Parameters:

  • actual (Object) (defaults to: @isa.new)

    Thing to validate.

Returns:

  • (Object)

    The argument, validated and filled defaults.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/data/validator.rb', line 108

def validate actual = @isa.new
  case actual when *@isa then
    if @rule.has_key? :rule then
      case
      when @isa == Hash  then return validate_hash actual
      when @isa == Array then return validate_array actual
      else raise RuntimeError, "[bug] notreached"
      end
    else
      return actual
    end
  else
    raise Error, "type mismatch"
  end
end

#with(extension) ⇒ Object

add options

Parameters:

  • extension ('AllowExtra')

    ‘AllowExtra’ only for now

Returns:

  • self



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/data/validator.rb', line 127

def with extension
  case extension
  when 'AllowExtra' then
    @rule[:allow_extra] = true
    if @rule.has_key? :rule
      case
      when @isa == Hash  then
        @rule[:rule].each_value do |rule|
          rule.with extension if rule.is_a? self.class
        end
      else
        @rule[:rule].with extension if @rule[:rule].is_a? self.class
      end
    end
  else
    raise ArgumentError, "unsupported extension #{extension}"
  end
  return self
end