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

alphaNumericWithUnderscore rule added #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@
return defaultValue;
};

purify.nonEmptyAlphaNumericWithUnderscore = function (rawValue, defaultValue) {
if (typeof rawValue === 'string' && /^\w+$/i.test(rawValue)) {
return rawValue;
} else if (typeof rawValue === 'number' && isFinite(rawValue) && !isNaN(rawValue)) {
return String(rawValue);
}
return defaultValue;
};

purify.alphaNumericWithUnderscore = function (rawValue, defaultValue) {
if (typeof rawValue === 'string' && /^\w*$/i.test(rawValue)) {
return rawValue;
} else if (typeof rawValue === 'number' && isFinite(rawValue) && !isNaN(rawValue)) {
return String(rawValue);
}
return defaultValue;
};

purify.uuid = function (rawValue, defaultValue) {
if (typeof rawValue === 'string' && oneValidation.uuid.test(rawValue)) {
return rawValue;
Expand Down
30 changes: 30 additions & 0 deletions test/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,34 @@ describe('#purify', function () {
expect('json', 'not to allow', '{foo:"bar"}');
});
});

describe('#alphaNumericWithUnderscore()', function () {
it('should accept valid input', function () {
expect('alphaNumericWithUnderscore', 'to allow', 'user_id');
expect('alphaNumericWithUnderscore', 'to allow', 1234, '1234');
expect('alphaNumericWithUnderscore', 'to allow', 12.34, '12.34');
expect('alphaNumericWithUnderscore', 'to allow', 'userName');
expect('alphaNumericWithUnderscore', 'to allow', 'abc__def');
expect('alphaNumericWithUnderscore', 'to allow', '');
});

it('should reject invalid input', function () {
expect('alphaNumericWithUnderscore', 'not to allow', 'abcdef:abc');
});
});

describe('#nonEmptyAlphaNumericWithUnderscore()', function () {
it('should accept valid input', function () {
expect('nonEmptyAlphaNumericWithUnderscore', 'to allow', 'user_id');
expect('nonEmptyAlphaNumericWithUnderscore', 'to allow', 1234, '1234');
expect('nonEmptyAlphaNumericWithUnderscore', 'to allow', 12.34, '12.34');
expect('nonEmptyAlphaNumericWithUnderscore', 'to allow', 'userName');
expect('nonEmptyAlphaNumericWithUnderscore', 'to allow', 'abc__def');
});

it('should reject invalid input', function () {
expect('nonEmptyAlphaNumericWithUnderscore', 'not to allow', '');
expect('nonEmptyAlphaNumericWithUnderscore', 'not to allow', 'abcdef:abc');
});
});
});