11# frozen_string_literal: true
22
33require 'date'
4+ require 'uri'
45
56# A UC Berkeley-specific validator for Jekyll sites
67# This class validates the following options:
@@ -48,7 +49,8 @@ class ConfigValidator
4849 course_department : :inclusion_validator ,
4950 color_scheme : :inclusion_validator ,
5051 semester_start_date : :validate_iso8601_date ,
51- semester_end_date : :validate_iso8601_date
52+ semester_end_date : :validate_iso8601_date ,
53+ class_archive_path : :validate_archive_path
5254 } . freeze
5355
5456 attr_accessor :config , :errors
@@ -73,7 +75,7 @@ def validate
7375 end
7476
7577 def validate_keys!
76- required_keys = %i[ baseurl course_department semester_start_date semester_end_date ]
78+ required_keys = %i[ baseurl course_department semester_start_date semester_end_date class_archive_path ]
7779 required_keys . each do |key |
7880 errors << "#{ key } is missing from site config" unless @config . key? ( key . to_s )
7981 end
@@ -117,6 +119,19 @@ def validate_semester_date_range
117119 errors << '`semester_start_date` must be on or before `semester_end_date`'
118120 end
119121
122+ def validate_archive_path ( _key , value )
123+ path = value . to_s . strip
124+ return errors << '`class_archive_path` cannot be blank' if path . empty?
125+ return if path . match? ( %r{^/} )
126+
127+ uri = URI . parse ( path )
128+ return if uri . is_a? ( URI ::HTTP ) && uri . host
129+
130+ errors << "`class_archive_path` must be an absolute path starting with `/` or a full URL, not '#{ value } '"
131+ rescue URI ::InvalidURIError
132+ errors << "`class_archive_path` must be an absolute path starting with `/` or a full URL, not '#{ value } '"
133+ end
134+
120135 def parse_iso8601_date ( value )
121136 Date . iso8601 ( value . to_s )
122137 rescue Date ::Error
0 commit comments