diff --git a/aio/tools/transforms/angular-api-package/content-rules/minLength.js b/aio/tools/transforms/angular-api-package/content-rules/minLength.js new file mode 100644 index 0000000000..bbcd77613d --- /dev/null +++ b/aio/tools/transforms/angular-api-package/content-rules/minLength.js @@ -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.`; + } + }; +}; diff --git a/aio/tools/transforms/angular-api-package/content-rules/minLength.spec.js b/aio/tools/transforms/angular-api-package/content-rules/minLength.spec.js new file mode 100644 index 0000000000..66f16e7550 --- /dev/null +++ b/aio/tools/transforms/angular-api-package/content-rules/minLength.spec.js @@ -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.'); + }); +});