Skip to content

Commit

Permalink
build(aio): create minLength content rule (#22759)
Browse files Browse the repository at this point in the history
This rule can be used to ensure that properties contain a minimum
number of characters.

PR Close #22759
  • Loading branch information
petebacondarwin authored and IgorMinar committed Apr 12, 2018
1 parent 1619160 commit fa11d78
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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.`;
}
};
};
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.');
});
});

0 comments on commit fa11d78

Please sign in to comment.