Created
December 6, 2018 16:13
-
-
Save Levii01/918339b2616ceda58aa2779fe30f5dc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Author
Levii01
commented
Dec 6, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment