forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(aio): create minLength content rule (angular#22759)
This rule can be used to ensure that properties contain a minimum number of characters. PR Close angular#22759
- Loading branch information
1 parent
41de37b
commit 3273e26
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
aio/tools/transforms/angular-api-package/content-rules/minLength.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = function createMinLengthRule(minLength) { | ||
minLength = minLength || 2; | ||
return (doc, prop, value) => { | ||
if (value.length < minLength) { | ||
return `Invalid "${prop}" property: "${value}". It must have at least ${minLength} characters.`; | ||
} | ||
}; | ||
}; |
19 changes: 19 additions & 0 deletions
19
aio/tools/transforms/angular-api-package/content-rules/minLength.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const createMinLengthRule = require('./minLength'); | ||
|
||
describe('createMinLength rule', () => { | ||
|
||
const defaultRule = createMinLengthRule(); | ||
const atLeast5CharsRule = createMinLengthRule(5); | ||
|
||
it('should return `undefined` if the length of the property value is long enough', () => { | ||
expect(defaultRule({}, 'description', 'abc')).toBeUndefined(); | ||
expect(atLeast5CharsRule({}, 'description', 'abcde')).toBeUndefined(); | ||
}); | ||
|
||
it('should return an error message if length of the property value is not long enough', () => { | ||
expect(defaultRule({}, 'description', 'a')) | ||
.toEqual('Invalid "description" property: "a". It must have at least 2 characters.'); | ||
expect(atLeast5CharsRule({}, 'description', 'abcd')) | ||
.toEqual('Invalid "description" property: "abcd". It must have at least 5 characters.'); | ||
}); | ||
}); |