Class: Commitlint::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/commitlint/validator.rb

Overview

Validator class is responsible for validating commit messages and elements inside the message to comply with Conventional Commit schema.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit_message) ⇒ Validator

Returns a new instance of Validator.



9
10
11
12
13
# File 'lib/commitlint/validator.rb', line 9

def initialize(commit_message)
  @commit_message = commit_message
  @has_validated = false
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/commitlint/validator.rb', line 7

def errors
  @errors
end

Instance Method Details

#commit_message_not_empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/commitlint/validator.rb', line 33

def commit_message_not_empty?
  @errors << "Commit message cannot be empty." if @commit_message.empty?
end

#subject_not_empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/commitlint/validator.rb', line 53

def subject_not_empty?
  @errors << "Subject is empty." if @matches && (@matches[:subject].nil? || @matches[:subject].empty?)
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/commitlint/validator.rb', line 15

def valid?
  validate!

  errors.empty?
end

#valid_format?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/commitlint/validator.rb', line 37

def valid_format?
  @matches = @commit_message.match(Commitlint::CONVENTIONAL_COMMIT_SCHEMA)

  @errors << "Invalid commit message format." unless @matches
end

#valid_scope?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/commitlint/validator.rb', line 43

def valid_scope?
  invalid_scope = @matches && @matches[:scope] && @matches[:scope].size < 2
  @errors << "Scope is invalid. Must be at least 2 characters" if invalid_scope
end

#valid_type?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/commitlint/validator.rb', line 48

def valid_type?
  valid_type = @matches && @matches[:type] && Commitlint::VALID_TYPES.include?(@matches[:type])
  @errors << "Type is invalid, must be one of \"#{Commitlint::VALID_TYPES.join(", ")}\"." unless valid_type
end

#validate!Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/commitlint/validator.rb', line 21

def validate!
  return if @has_validated

  commit_message_not_empty?
  valid_format?
  valid_type?
  valid_scope?
  subject_not_empty?

  @has_validated = true
end