Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isSlug): isSlug validator #1096

Merged
merged 1 commit into from Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -155,6 +155,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
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
@@ -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
Expand Up @@ -97,6 +97,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 @@ -181,6 +183,7 @@ const validator = {
isWhitelisted,
normalizeEmail,
toString,
isSlug,
};

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

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes I requested were implemented here.

export default function isSlug(str) {
assertString(str);
return (charsetRegex.test(str));
}
16 changes: 16 additions & 0 deletions test/validators.js
Expand Up @@ -6910,4 +6910,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
Expand Up @@ -2027,6 +2027,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 @@ -2109,7 +2115,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.