Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added name argument to validators for ticket validations #10572

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def edit_person_validators
ticket.metadata.tickets_edit_person_fields.each do |edit_person_field|
case edit_person_field[:field_name]
when TicketsEditPersonField.field_names[:dob]
dob_validation_issues = ResultsValidators::PersonsValidator.dob_validations(Date.parse(edit_person_field[:new_value]))
dob_validation_issues = ResultsValidators::PersonsValidator.dob_validations(Date.parse(edit_person_field[:new_value]), { name: ticket.metadata.wca_id })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like my other suggestion about the method signature, then this can become...

Suggested change
dob_validation_issues = ResultsValidators::PersonsValidator.dob_validations(Date.parse(edit_person_field[:new_value]), { name: ticket.metadata.wca_id })
dob_validation_issues = ResultsValidators::PersonsValidator.dob_validations(Date.parse(edit_person_field[:new_value]), name: ticket.metadata.wca_id)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidenote: You should probably extract the Date.parse into its own line, helps with debugging :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/results_validators/persons_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ def include_persons?
true
end

def self.dob_validations(dob, competition_id = nil, name = nil)
def self.dob_validations(dob, message_args = {}, competition_id = nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make it more Ruby-esque by something like...

Suggested change
def self.dob_validations(dob, message_args = {}, competition_id = nil)
def self.dob_validations(dob, competition_id = nil, **message_args)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you take this suggestion, then below you can "spread" the parameters into the ValidationIssue class as well: ValidationWarning.new(:persons, competition_id, DOB_0101_WARNING, **message_args)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay done.

validation_issues = []

# Check if DOB is January 1
if dob.month == 1 && dob.day == 1
validation_issues << ValidationWarning.new(:persons, competition_id, DOB_0101_WARNING, name: name)
validation_issues << ValidationWarning.new(:persons, competition_id, DOB_0101_WARNING, name: message_args[:name])
end

# Check if DOB is very young, competitor less than 3 years old are extremely rare, so we'd better check these birthdate are correct.
if dob.year >= Time.now.year - 3
validation_issues << ValidationWarning.new(:persons, competition_id, VERY_YOUNG_PERSON_WARNING, name: name)
validation_issues << ValidationWarning.new(:persons, competition_id, VERY_YOUNG_PERSON_WARNING, name: message_args[:name])
end

# Check if DOB is not so young
if dob.year <= Time.now.year - 100
validation_issues << ValidationWarning.new(:persons, competition_id, NOT_SO_YOUNG_PERSON_WARNING, name: name)
validation_issues << ValidationWarning.new(:persons, competition_id, NOT_SO_YOUNG_PERSON_WARNING, name: message_args[:name])
end

validation_issues
Expand Down Expand Up @@ -101,7 +101,7 @@ def run_validation(validator_data)
name: p.name)
end

PersonsValidator.dob_validations(p.dob, competition.id, p.name).each do |validation|
PersonsValidator.dob_validations(p.dob, { name: p.name }, competition.id).each do |validation|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my suggestion above, this could be changed to PersonsValidator.dob_validations(p.dob, competition.id, name: p.name)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if validation.is_a?(ValidationError)
@errors << validation
elsif validation.is_a?(ValidationWarning)
Expand Down
Loading