Skip to content

Commit

Permalink
add isSlug validator, issue validatorjs#952
Browse files Browse the repository at this point in the history
  • Loading branch information
Heydad-Helfer committed Jan 7, 2019
1 parent 5ca54d2 commit 325210e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Validator | Description
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{no_symbols: false}`. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).
**isPort(str)** | check if the string is a valid port number.
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.).
**isSlug(str)** | check is the string is a slug ("this-is-a-slug", "this is not a slug")

This comment has been minimized.

Copy link
@ezkemboi

ezkemboi Jul 28, 2019

This work looks good.

**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.
**isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5).
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ var _normalizeEmail = _interopRequireDefault(require("./lib/normalizeEmail"));

var _toString = _interopRequireDefault(require("./lib/util/toString"));

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 @@ -235,7 +237,8 @@ var validator = {
blacklist: _blacklist.default,
isWhitelisted: _isWhitelisted.default,
normalizeEmail: _normalizeEmail.default,
toString: _toString.default
toString: _toString.default,
isSlug: _isSlug.default
};
var _default = validator;
exports.default = _default;
Expand Down
19 changes: 19 additions & 0 deletions lib/isSlug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"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 }; }

// const numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
// const numericNoSymbols = /^[0-9]+$/;
function isSlug(str) {
(0, _assertString.default)(str);
return str.indexOf(' ') === -1;
}

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

import toString from './lib/util/toString';

import isSlug from './lib/isSlug';

const version = '10.10.0';

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

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

// const numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
// const numericNoSymbols = /^[0-9]+$/;

export default function isSlug(str) {

This comment has been minimized.

Copy link
@ezkemboi

ezkemboi Jul 28, 2019

Good work.

assertString(str);

return str.indexOf(' ') === -1;
}
21 changes: 21 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6208,4 +6208,25 @@ describe('Validators', () => {
],
});
});

it('should validate a slug string', () => {
test({
validator: 'isSlug',
valid: [
'hello-world',
'this-is-a-slug',
'slug',
'thisIsASlug',
'',
'ThisIsASlug',
'This_is_a_slug',
],
invalid: [
'not a slug',
'not-a slug',
'not-a-slug ',
'ThisIs NotASlug',
],
});
});
});
10 changes: 9 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,13 @@ function normalizeEmail(email, options) {
return parts.join('@');
}

// const numericNoSymbols = /^[0-9]+$/;

function isSlug(str) {
assertString(str);
return str.indexOf(' ') === -1;
}

var version = '10.10.0';
var validator = {
version: version,
Expand Down Expand Up @@ -2089,7 +2096,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.

1 comment on commit 325210e

@ezkemboi
Copy link

Choose a reason for hiding this comment

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

@Heydad-Helfer, can you help us get this as a PR in validator.js?
We need to make reviews on the same.

Thanks. Looking forward to your reply.

Please sign in to comment.