Skip to content

Commit

Permalink
[FEAT] Validation Helper (#33)
Browse files Browse the repository at this point in the history
* feat: add validation helper

* test: add test coverage for validation helper

* chore: rename 'Errors' dir to 'errors'
  • Loading branch information
Connor-Bernard authored Apr 17, 2024
1 parent df07fa1 commit b606779
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/errors/validation_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
##
# Base class for a validation error.
class ValidationError < StandardError
##
# Constructor for error.
#
# @param message the error message (defaults to "Validation Error").
def initialize(message="Validation Error")
super(message)
end
end
63 changes: 63 additions & 0 deletions app/helpers/canvas_validation_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module CanvasValidationHelper
OVERRIDE_TITLE_MAX_CHARACTERS = 40

##
# Checks if the provided course id is valid.
#
# @param [Integer] courseId the course id to check.
# @return [Boolean] whether the provided id is valid.
def is_valid_course_id(courseId)
courseId.is_a?(Integer) && courseId > 0
end

##
# Checks if the provided assignment id is valid.
#
# @param [Integer] assignmentId the assignment id to check.
# @return [Boolean] whether the assignment id is valid.
def is_valid_assignment_id(assignmentId)
assignmentId.is_a?(Integer) && assignmentId > 0
end

##
# Checks if the student id is valid.
#
# @param [Integer] studentId the student id to check.
# @return [Boolean] whether the student id is valid.
def is_valid_student_id(studentId)
studentId.is_a?(Integer) && studentId > 0
end

##
# Checks if the list of student ids is valid.
#
# @param [Enumerable] the list of student ids to validate.
# @return [Boolean] whether all student ids in the list are valid.
def is_valid_student_ids(studentIds)
studentIds.all? do |studentId|
is_valid_student_id(studentId)
end
end

##
# Checks to see if the provided override title is valid.
#
# @param [String] title the title to check.
# @return [Boolean] whether the title is valid.
def is_valid_title(title)
/^[A-Za-z0-9\-_ ]*$/.match?(title) && title.length < OVERRIDE_TITLE_MAX_CHARACTERS
end

##
# Checks whether the provided date is a valid Canvas date.
# TODO: maybe want to refine this to actually valid dates not just format?
#
# @param [String] date the date string to validate.
# @return [Boolean] whether the date string is valid.
def is_valid_date(date)
if (date == nil)
return true
end
/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/.match?(date)
end
end
112 changes: 112 additions & 0 deletions spec/Helpers/canvas_validation_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require 'rails_helper'
require_relative '../../app/helpers/canvas_validation_helper'

describe 'CanvasValidationHelper', type: :helper do
class Helper
include CanvasValidationHelper
end
attr_reader :helper

before do
@helper = Helper.new
end

describe 'is_valid_course_id' do
it 'returns true on positive integer input' do
expect(helper.is_valid_course_id(16)).to be(true)
end

it 'returns false on negative integer input' do
expect(helper.is_valid_course_id(-16)).to be(false)
end

it 'returns false on boolean input' do
expect(helper.is_valid_course_id(0.1)).to be(false)
end

it 'returns false on alphabetical input' do
expect(helper.is_valid_course_id('abc')).to be(false)
end
end

describe 'is_valid_assignment_id' do
it 'returns true on positive integer input' do
expect(helper.is_valid_assignment_id(16)).to be(true)
end

it 'returns false on negative integer input' do
expect(helper.is_valid_assignment_id(-16)).to be(false)
end

it 'returns false on boolean input' do
expect(helper.is_valid_assignment_id(0.1)).to be(false)
end

it 'returns false on alphabetical input' do
expect(helper.is_valid_assignment_id('abc')).to be(false)
end
end

describe 'is_valid_student_id' do
it 'returns true on positive integer input' do
expect(helper.is_valid_student_id(16)).to be(true)
end

it 'returns false on negative integer input' do
expect(helper.is_valid_student_id(-16)).to be(false)
end

it 'returns false on boolean input' do
expect(helper.is_valid_student_id(0.1)).to be(false)
end

it 'returns false on alphabetical input' do
expect(helper.is_valid_student_id('abc')).to be(false)
end
end

describe 'is_valid_student_ids' do
it 'returns true on positive integral input' do
expect(helper.is_valid_student_ids([16, 18])).to be(true)
end

it 'returns false on negative integer input' do
expect(helper.is_valid_student_ids([-16, 16])).to be(false)
end

it 'returns false on boolean input' do
expect(helper.is_valid_student_ids([16, 0.1])).to be(false)
end

it 'returns false on alphabetical input' do
expect(helper.is_valid_student_ids(['abc', 16])).to be(false)
end
end
describe 'is_valid_title' do
it 'returns true on valid input' do
expect(helper.is_valid_title('hello world')).to be(true)
end

it 'returns false invalid characters' do
expect(helper.is_valid_title('**')).to be(false)
end

it 'returns false on too long input' do
expect(helper.is_valid_title('A' * 50)).to be(false)
end
end

describe 'is_valid_date' do
it 'returns true on properly formatted dates' do
expect(helper.is_valid_date('2002-03-16T12:00:00Z')).to be(true)
end

it 'returns false on inproperly formatted dates' do
expect(helper.is_valid_date('March 16th 2002')).to be(false)
end

it 'returns false on strings with too much text' do
expect(helper.is_valid_date('2002-03-16T12:00:00Z*')).to be(false)
end
end
end

0 comments on commit b606779

Please sign in to comment.