add ends in slash validator (#22218)

This commit is contained in:
claire bontempo 2023-08-07 12:31:57 -07:00 committed by GitHub
parent 6654c425d2
commit 58bb5f0eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -25,9 +25,17 @@ export const number = (value, { nullable = false } = {}) => {
return !isNaN(value);
};
/*
the following validations return false (invalid) if the condition is met
*/
export const containsWhiteSpace = (value) => {
const validation = new RegExp('\\s', 'g'); // search for whitespace
return !validation.test(value);
};
export default { presence, length, number, containsWhiteSpace };
export const endsInSlash = (value) => {
const validation = new RegExp('/$');
return !validation.test(value);
};
export default { presence, length, number, containsWhiteSpace, endsInSlash };

View File

@ -92,4 +92,17 @@ module('Unit | Util | validators', function (hooks) {
check('trailingSpace ');
assert.false(isValid, 'Invalid when text has trailing whitespace');
});
test('it should validate value ends in a slash', function (assert) {
let isValid;
const check = (prop) => (isValid = validators.endsInSlash(prop));
check('validText');
assert.true(isValid, 'Valid when text does not end in slash');
check('valid/Text');
assert.true(isValid, 'Valid when text only contains slash');
check('invalid/');
assert.false(isValid, 'Invalid when text ends in slash');
check('also/invalid/');
assert.false(isValid, 'Invalid when text contains and ends in slash');
});
});