class Pacto::Validators::BodyValidator

Public Class Methods

section_name() click to toggle source
# File lib/pacto/validators/body_validator.rb, line 4
def self.section_name
  fail 'section name should be provided by subclass'
end
subschema(contract) click to toggle source
# File lib/pacto/validators/body_validator.rb, line 8
def self.subschema(contract)
  fail 'override to return the proper subschema the contract'
end
validate(contract, body) click to toggle source

FIXME: github.com/thoughtworks/pacto/issues/10#issuecomment-31281238 rubocop:disable MethodLenth

# File lib/pacto/validators/body_validator.rb, line 14
def self.validate(contract, body)
  schema = subschema(contract)
  if schema
    schema['id'] = contract.file unless schema.key? 'id'
    if schema['type'] && schema['type'] == 'string'
      validate_as_pure_string schema, body.body
    else
      validate_as_json(schema, body)
    end
  end || []
end

Private Class Methods

validate_as_json(schema, body) click to toggle source
# File lib/pacto/validators/body_validator.rb, line 43
def self.validate_as_json(schema, body)
  body = body.body if body.respond_to? :body
  JSON::Validator.fully_validate(schema, body, :version => :draft3)
end
validate_as_pure_string(schema, body) click to toggle source

rubocop:enable MethodLenth

# File lib/pacto/validators/body_validator.rb, line 29
def self.validate_as_pure_string(schema, body)
  errors = []
  if schema['required'] && body.nil?
    errors << "The #{section_name} does not contain a body"
  end

  pattern = schema['pattern']
  if pattern && !(body =~ Regexp.new(pattern))
    errors << "The #{section_name} does not match the pattern #{pattern}"
  end

  errors
end