Skip to content

Commit

Permalink
[FEATURE] Add scheme option for url type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
blakegearin committed Feb 24, 2022
1 parent 1282748 commit bbdc385
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
19 changes: 19 additions & 0 deletions addon/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const { canInvoke } = Ember;
* @param {Regex} options.regex The regular expression to test against
* @param {Boolean} options.allowNonTld If true, the predefined regular expression `email` allows non top-level domains
* @param {Number} options.minTldLength The min length of the top-level domain on the predefined `email` regular expression
* @param {Any} options.scheme Adds scheme requirements on the predefined regular expression `url`
* @param {Object} model
* @param {String} attribute
*/
Expand Down Expand Up @@ -58,6 +59,11 @@ export default function validateFormat(value, options, model, attribute) {
regexTest = formatEmailRegex(options);
}
Object.assign({}, options, { regex: regexTest });
} else if (type === 'url') {
if (regexTest === regularExpressions.url) {
regexTest = formatUrlRegex(options);
}
Object.assign({}, options, { regex: regexTest });
}

if (
Expand Down Expand Up @@ -94,3 +100,16 @@ function formatEmailRegex(options) {

return new RegExp(source, 'i');
}

function formatUrlRegex(options) {
let { source } = regularExpressions.url;
let { scheme } = options;

if (scheme === true) {
source = `^([a-zA-Z0-9-+&.]+)://${source}`;
} else if (scheme instanceof Array) {
source = `^(${scheme.join("|")}){1}://${source}`;
}

return new RegExp(source);
}
65 changes: 65 additions & 0 deletions tests/unit/validators/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,71 @@ test('url', function (assert) {
assert.true(processResult(result));
});

test('url + scheme as true', function (assert) {
assert.expect(5);

options = {
type: 'url',
scheme: true,
};

options = cloneOptions(options);

result = validate('offirgolan', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('offir.golan', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('ht_tp://offir.golan', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('http://www.offirgolan.com', options);
assert.true(processResult(result));

result = validate('a+B.c-d&e://www.offirgolan.com', options);
assert.true(processResult(result));
});

test('url + scheme as array', function (assert) {
assert.expect(8);

options = {
type: 'url',
scheme: [
'https',
'http',
],
};

options = cloneOptions(options);

result = validate('offirgolan', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('offir.golan', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('htt://offirgolan.com', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('httpextra://www.offirgolan.com', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('extrahttp://www.offirgolan.com', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('httphttps://www.offirgolan.com', options);
assert.strictEqual(processResult(result), 'This field must be a valid url');

result = validate('http://www.offirgolan.com', options);
assert.true(processResult(result));

result = validate('https://www.offirgolan.com', options);
assert.true(processResult(result));
});


test('inverse - with type', function (assert) {
assert.expect(2);

Expand Down

0 comments on commit bbdc385

Please sign in to comment.