Skip to content

Instantly share code, notes, and snippets.

@Levii01
Created Dec 6, 2018
Embed
What would you like to do?
# class SsnValidator < Grape::Validations::Validator
# module GrapeValidators
class Ssn < Grape::Validations::Base
SSN_FORMAT = /^[0-8]\d{2}-\d{2}-\d{4}$/
def validate_param!(attr_name, params)
@params = params
binding.pry
# return if skip_validation?
unless skip_validation? || (validate_ssn? && validate!)
# fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'SSN must be valid'
end
end
attr_reader :params
def validate_ssn?
params[:ssn].present? || params[:ssn_required] == 'true'
end
def validate!
params[:ssn] =~ SSN_FORMAT ? true : false
end
def skip_validation?
params[:ssn].blank? && params[:ssn_required] == 'false'
end
end
# class SsnValidator < Grape::Validations::Validator
# module GrapeValidators
# class SsnValidator < Grape::Validations::Base
# # class Test < Grape::Validations::Base
# def validate_param!(attr_name, params)
# binding.pry
# unless params[attr_name] =~ /\A[[:alnum:]]+\z/
# puts 'hi :)'
# binding.pry
# # fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'must consist of alpha-numeric characters'
# end
# end
# end
# end
@Levii01

This comment has been minimized.

Copy link
Owner Author

@Levii01 Levii01 commented Dec 6, 2018

# class SsnValidator < Grape::Validations::Validator
# module GrapeValidators
# class Ssn < Grape::Validations::Base
#   def validate_param!(attr_name, params)
#     binding.pry
#     return if params[:ssn].blank? && params[:ssn_required] == 'false'
#     unless params[:ssn].present? || params[:ssn_required] == 'true' &&  params[:ssn] =~ /^[0-8]\d{2}-\d{2}-\d{4}$/
#       fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'SSN must be valid'
#     end
#   end
# end

# class SsnValidator < Grape::Validations::Validator
# module GrapeValidators
  class Ssn < Grape::Validations::Base
    def validate_param!(attr_name, params)
      binding.pry
      unless params[attr_name] =~ /\A[[:alnum:]]+\z/
        puts 'hi :)'
        binding.pry
        # fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'must consist of alpha-numeric characters'
      end
    end
  end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment