Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #30 from erquhart/add-subject-pattern
Browse files Browse the repository at this point in the history
feat(subjectPattern): allow simple subject validation
  • Loading branch information
Kent C. Dodds committed Mar 29, 2016
2 parents 5b569a0 + d522ba6 commit d6b34b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ You can specify options in `package.json`
"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "revert"], // default
"warnOnFail": false, // default
"maxSubjectLength": 100, // default
"subjectPattern": ".+", // default
"subjectPatternErrorMsg": 'subject does not match subject pattern!', // default
"helpMessage": "" //default
}
}
Expand All @@ -49,6 +51,14 @@ If this is set to `true` errors will be logged to the console, however the commi

This will control the maximum length of the subject.

#### subjectPattern

Optional, accepts a RegExp to match the commit message subject against.

#### subjectPatternErrorMsg

If `subjectPattern` is provided, this message will be displayed if the commit message subject does not match the pattern.

#### helpMessage

If provided, the helpMessage string is displayed when a commit message is not valid. This allows projects to provide a better developer experience for new contributors.
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ var validateMessage = function(raw) {
var scope = match[4];
var subject = match[5];

var SUBJECT_PATTERN = new RegExp(config.subjectPattern || '.+');
var SUBJECT_PATTERN_ERROR_MSG = config.subjectPatternErrorMsg || 'subject does not match subject pattern!';

if (firstLine.length > MAX_LENGTH && !squashing) {
error('is longer than %d characters !', MAX_LENGTH);
isValid = false;
Expand All @@ -76,6 +79,11 @@ var validateMessage = function(raw) {
error('"%s" is not allowed type !', type);
isValid = false;
}

if (!SUBJECT_PATTERN.exec(subject)) {
error(SUBJECT_PATTERN_ERROR_MSG);
isValid = false;
}
}

// Some more ideas, do want anything like this ?
Expand Down
12 changes: 12 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ describe('validate-commit-msg.js', function() {
expect(m.validateMessage('Merge branch master into validate-commit-msg-integration')).to.equal(INVALID);
expect(m.validateMessage('Merge branch \'master\' into validate-commit_msg-integration')).to.equal(VALID);
});

it('should validate subject against subjectPattern if provided', function() {
var msg = 'chore(build): A something Z';
m.config.subjectPattern = /^A.*Z$/;
expect(m.validateMessage(msg)).to.equal(VALID);

msg = 'chore(build): something';
expect(m.validateMessage(msg)).to.equal(INVALID);

expect(errors).to.deep.equal(['INVALID COMMIT MSG: subject does not match subject pattern!']);
expect(logs).to.deep.equal([msg]);
});
});

describe('handle .git as folder', function()
Expand Down

0 comments on commit d6b34b8

Please sign in to comment.