From 58bb5f0eb1bf61038870fdd0fb1dd78aa7fa34c5 Mon Sep 17 00:00:00 2001 From: claire bontempo <68122737+hellobontempo@users.noreply.github.com> Date: Mon, 7 Aug 2023 12:31:57 -0700 Subject: [PATCH] add ends in slash validator (#22218) --- ui/app/utils/validators.js | 10 +++++++++- ui/tests/unit/utils/validators-test.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ui/app/utils/validators.js b/ui/app/utils/validators.js index ceab04ef39..03f74dd9e6 100644 --- a/ui/app/utils/validators.js +++ b/ui/app/utils/validators.js @@ -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 }; diff --git a/ui/tests/unit/utils/validators-test.js b/ui/tests/unit/utils/validators-test.js index e375d1b9b7..0f01cddf0e 100644 --- a/ui/tests/unit/utils/validators-test.js +++ b/ui/tests/unit/utils/validators-test.js @@ -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'); + }); });