Skip to content

Commit

Permalink
feat(isSlug): add isSlug validator (#1096)
Browse files Browse the repository at this point in the history
closes #952
  • Loading branch information
Nicanor008 authored and profnandaa committed Oct 16, 2019
1 parent 71c46e1 commit 2222e5a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Sanitizer | Description
**toInt(input [, radix])** | convert the input string to an integer, or `NaN` if the input is not an integer.
**trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input.
**whitelist(input, chars)** | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `whitelist(input, '\\[\\]')`.
**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]

### XSS Sanitization

Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ var _isWhitelisted = _interopRequireDefault(require("./lib/isWhitelisted"));

var _normalizeEmail = _interopRequireDefault(require("./lib/normalizeEmail"));

var _isSlug = _interopRequireDefault(require("./lib/isSlug"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand Down Expand Up @@ -239,7 +241,8 @@ var validator = {
blacklist: _blacklist.default,
isWhitelisted: _isWhitelisted.default,
normalizeEmail: _normalizeEmail.default,
toString: toString
toString: toString,
isSlug: _isSlug.default
};
var _default = validator;
exports.default = _default;
Expand Down
20 changes: 20 additions & 0 deletions lib/isSlug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isSlug;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var charsetRegex = /^[^-_](?!.*?[-_]{2,})([a-z0-9\\-]{1,}).*[^-_]$/;

function isSlug(str) {
(0, _assertString.default)(str);
return charsetRegex.test(str);
}

module.exports = exports.default;
module.exports.default = exports.default;
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ import isWhitelisted from './lib/isWhitelisted';

import normalizeEmail from './lib/normalizeEmail';

import isSlug from './lib/isSlug';

const version = '11.1.0';

const validator = {
Expand Down Expand Up @@ -183,6 +185,7 @@ const validator = {
isWhitelisted,
normalizeEmail,
toString,
isSlug,
};

export default validator;
8 changes: 8 additions & 0 deletions src/lib/isSlug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import assertString from './util/assertString';

let charsetRegex = /^[^-_](?!.*?[-_]{2,})([a-z0-9\\-]{1,}).*[^-_]$/;

export default function isSlug(str) {
assertString(str);
return (charsetRegex.test(str));
}
16 changes: 16 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7039,4 +7039,20 @@ describe('Validators', () => {
],
});
});

it('should validate slug', () => {
test({
validator: 'isSlug',
args: ['cs_67CZ'],
valid: ['cs-cz', 'cscz'],
invalid: [
'not-----------slug',
'@#_$@',
'-not-slug',
'not-slug-',
'_not-slug',
'not-slug_',
],
});
});
});
9 changes: 8 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,12 @@ function normalizeEmail(email, options) {
return parts.join('@');
}

var charsetRegex = /^[^-_](?!.*?[-_]{2,})([a-z0-9\\-]{1,}).*[^-_]$/;
function isSlug(str) {
assertString(str);
return charsetRegex.test(str);
}

var version = '11.1.0';
var validator = {
version: version,
Expand Down Expand Up @@ -2111,7 +2117,8 @@ var validator = {
blacklist: blacklist$1,
isWhitelisted: isWhitelisted,
normalizeEmail: normalizeEmail,
toString: toString
toString: toString,
isSlug: isSlug
};

return validator;
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 2222e5a

Please sign in to comment.