lib/compel/contract.rb in compel-0.1.3 vs lib/compel/contract.rb in compel-0.2.0

- old
+ new

@@ -1,82 +1,38 @@ module Compel class Contract attr_reader :errors, - :conditions, :serialized_errors - def initialize(params, &block) - if params.nil? || !Coercion.valid?(params, Hash) - raise ParamTypeError, 'params must be an Hash' + def initialize(hash, schema) + if hash.nil? || !Coercion.valid?(hash, Hash) + raise Compel::TypeError, 'must be an Hash' end - @errors = Errors.new - @params = Hashie::Mash.new(params) - @conditions = Hashie::Mash.new - @coerced_params = Hashie::Mash.new - - instance_eval(&block) + @hash = Hashie::Mash.new(hash) + @schema = schema + @coerced_hash = Hashie::Mash.new end def validate - @conditions.values.each do |param| - begin - # If it is an Hash and it was given conditions for that Hash, - # build a new Compel::Contract form inner conditions - if (param.hash? && param.conditions?) + validator = Validators::HashValidator.new(@hash, @schema).validate - # If this param is required, a value must be given to build the Compel::Contract - # otherwise, only build it if is given a value for the param - if (param.required? && !param.value.nil?) || !param.value.nil? - contract = Contract.new(param.value, &param.conditions).validate + @errors = validator.errors + @coerced_hash = validator.output - @errors.add(param.name, contract.errors) - - # Update the param value with coerced values to use later - # when coercing param parent - @coerced_params[param.name] = contract.coerced_params - end - end - - # All values must coerce before going through validation, - # raise exception to avoid validation - - # If the param value has already been coerced from digging into child Hash - # use that value instead, so we don't lose the previous coerced values - coerced_value = Coercion.coerce! \ - (@coerced_params[param.name].nil? ? param.value : @coerced_params[param.name]), param.type, param.options - - # Only add to coerced values if not nil - if !coerced_value.nil? - @coerced_params[param.name] = coerced_value - end - - @errors.add \ - param.name, Validation.validate(param.value, param.options) - - rescue Compel::ParamTypeError => exception - @errors.add(param.name, exception.message) - end - end - self end - def param(name, type, options = {}, &block) - @conditions[name] = \ - Param.new(name, type, @params.delete(name), options, &block) + def coerced_hash + # @hash has all params that are not affected by the validation + @hash.merge(@coerced_hash) end - def coerced_params - # @params has all params that are not affected by the validation - @params.merge(@coerced_params) - end - def serialize - coerced_params.tap do |hash| + coerced_hash.tap do |hash| if !valid? hash[:errors] = serialized_errors end end end @@ -89,17 +45,17 @@ @errors.to_hash end def raise? if !valid? - exception = InvalidParamsError.new - exception.params = coerced_params + exception = InvalidHashError.new + exception.object = coerced_hash exception.errors = serialized_errors - raise exception, 'params are invalid' + raise exception, 'hash has errors' end - coerced_params + coerced_hash end end end