-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extensions): add extra string formats (#19)
- Loading branch information
1 parent
85dcc57
commit f6729df
Showing
8 changed files
with
347 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
var _ = require('lodash'); | ||
var assert = require('../assert'); | ||
|
||
|
||
var ip = /(\d{1,3}\.){3}\d{1,3}(\/\d{1,2})?/i; | ||
var domain = /([0-9a-z-]+\.?)+(\.)?[0-9a-z-]+/i; | ||
var hostname = new RegExp(`(${domain.source}|${ip.source})`, 'i'); | ||
var REGEXES = { | ||
hex: /^[0-9a-f]*$/i, | ||
alphanumeric: /^\w*$/i, | ||
creditCard: /^\d{13,16}$/, | ||
uuid: /^[0-9a-f]{4}([0-9a-f]{4}-){4}[0-9a-f]{12}$/i, | ||
ip: new RegExp(`^${ip.source}$`), | ||
domain: new RegExp(`^${domain.source}$`, 'i'), | ||
email: [/^\S+@/, domain, /$/], | ||
hostname: new RegExp(`^${hostname.source}$`, 'i'), | ||
uri: [ | ||
/^[a-z0-9+.-]+:(\/\/)?/, // scheme | ||
/(\S+@)?/, // userinfo | ||
hostname, // hostname | ||
/(:\d+)?/, | ||
], | ||
}; | ||
|
||
var compose = function (regexes) { | ||
return regexes.reduce(function (regexp, part) { | ||
var first = regexp ? regexp.source : ''; | ||
return new RegExp(first + part.source); | ||
}, false); | ||
}; | ||
|
||
var validation = function (name) { | ||
var regex = _.isArray(REGEXES[name]) ? | ||
compose(REGEXES[name]) : REGEXES[name]; | ||
var msg = `value should match ${regex}`; | ||
|
||
return value => { | ||
var match = _.get(value, 'match', _.noop); | ||
assert.typeof(match, 'function'); | ||
assert.ok(match.call(value, regex), msg); | ||
}; | ||
}; | ||
|
||
module.exports = function () { | ||
var extension = { | ||
builders: true, | ||
formats: { | ||
string: [ | ||
'uuid', 'alphanumeric', 'hex', | ||
'ip', 'uri', 'domain', 'email', | ||
'creditCard', | ||
], | ||
}, | ||
transformations: { | ||
string: { | ||
creditCard: function (value) { | ||
if (typeof value === 'string') { | ||
return value.replace(/[-\s]+/g, ''); | ||
} else { | ||
return value; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
extension.validations = { | ||
string: _.map(extension.formats.string, format => { | ||
return _.set({}, format, validation(format)); | ||
}).reduce(_.merge, {}) | ||
}; | ||
|
||
return extension; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
defineTest('extensions/strings.js', function (extensionFactory) { | ||
var extension = extensionFactory(); | ||
|
||
describe('#transformations', function () { | ||
describe('creditCard', function () { | ||
var transform = extension.transformations.string.creditCard; | ||
it('should remove dashes and spaces', function () { | ||
transform('444-4444-4444').should.equal('44444444444'); | ||
transform('444 4444 4444').should.equal('44444444444'); | ||
}); | ||
|
||
it('should not remove dashes and spaces when non-string', function () { | ||
var obj = {}; | ||
transform(12).should.equal(12); | ||
transform(obj).should.equal(obj); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#validations', function () { | ||
describe('uuid', function () { | ||
var validate = extension.validations.string.uuid; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'11111111-1111-1111-1111-111111111111', | ||
'1e1af1d1-2111-4111-1121-1a1e111ec111', | ||
'8aebedcd-474d-4a32-bd7e-425b92834fb3', | ||
'f83b75b8-8387-4657-9e56-fb7a501ca578', | ||
'367aa2b0-52e4-11e6-beb8-9e71128cae77', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], '', false, | ||
' 11111111-1111-1111-1111-111111111111', | ||
'11111111-1111-1111-1111-111111111111 ', | ||
'11111111-1111-1111- 1111-111111111111 ', | ||
'11111111-1111-1111-1111-11111111111g', | ||
'11111111-1111-1111-1111-11111111111', | ||
'1e1af1d1-2111-4111-112-1a1e111ec111', | ||
'8aebedcd-474d-4a3-bd7e-425b92834fb3', | ||
'f83b75b8-887-4657-9e56-fb7a501ca578', | ||
'367aa2b-52e4-11e6-beb8-9e71128cae77', | ||
'367aa2b052e411e6beb89e71128cae77', | ||
], validate); | ||
}); | ||
}); | ||
|
||
describe('alphanumeric', function () { | ||
var validate = extension.validations.string.alphanumeric; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'', 'A', '112', 'DFfa234', | ||
'15cyA7uYR4y4CpLuhMfaQ0tTeKDrf5S', | ||
'g1dNsuxnFYQeZzFyd4vfmNyCGarkzal', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], false, ' asdf', '12 23', 'foo ', | ||
'asdf-sfasf-sfsdf-sasdf', '1231-123123', | ||
'asfafs14234$%@#sdf', 'FSdioJFEJW\nasdf', | ||
], validate); | ||
}); | ||
}); | ||
|
||
describe('hex', function () { | ||
var validate = extension.validations.string.hex; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'', 'A', '112', 'DFfa234', | ||
'367aa2b052e411e6beb89e71128cae77', | ||
'df42447a701d4d0f9506e9087bb604a0', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], false, 'asdf fasd', | ||
' abcdef', 'abcdef ', 'abc def', '12 23', | ||
'asdf-sfasf-sfsdf-sasdf', '1231-123123', | ||
'abcdef%@#abcdef', 'FSdioJFEJWasdf', | ||
], validate); | ||
}); | ||
}); | ||
|
||
describe('ip', function () { | ||
var validate = extension.validations.string.ip; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'172.32.0.1', '192.168.0.1', '10.10.0.1', | ||
'255.255.255.0', '8.8.8.8', '75.75.75.75', | ||
'10.0.0.0/8', '172.20.0.0/16', '192.168.0.0/24', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], false, 'asdf fasd', '12 23', | ||
'172.32.0.1 ', ' 172.32.0.1', '172. 32.0.1', | ||
'asdf-sfasf-sfsdf-sasdf', '1231-123123', | ||
'abcdef%@#abcdef', 'FSdioJFEJWasdf', | ||
], validate); | ||
}); | ||
}); | ||
|
||
describe('uri', function () { | ||
var validate = extension.validations.string.uri; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'http://www.foo-bar.com/asdfsfd?asdfsf#go', | ||
'https://192.168.0.0:3200/something?user=#other', | ||
'ftp://user:password@home.net/my/folder', | ||
'ssh://git@github.com:patrickhulce/klay', | ||
'git+https://github.com/patrickhulce/klay', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], false, 'www.google.com', | ||
' http://www.foo-bar.com/asdfsfd?asdfsf#go', | ||
'http:/my-something', 'sdf%#://#%@asdf', | ||
'asdfasfasfd', '23423423423', 'asdf asdf', | ||
], validate); | ||
}); | ||
}); | ||
|
||
describe('domain', function () { | ||
var validate = extension.validations.string.domain; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'www.foo-bar.com', 'home.net', 'github.com', | ||
'something.private.domain', 'other-123.local', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], false, '192.168.0.0/24', | ||
' www.foobar.com', 'home.net ', 'git hub.com', | ||
'http:/my-something', 'sdf%#://#%@asdf', | ||
'asdfas fasfd', '23 423423423', 'asdf asdf', | ||
], validate); | ||
}); | ||
}); | ||
|
||
describe('email', function () { | ||
var validate = extension.validations.string.email; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'me@www.foo-bar.com', 'me+spam@home.net', | ||
'other.special@github.com', 'math123@cool.co.uk', | ||
'admin@localhost', 'foo+.sf@other-123.local', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], false, '192.168.0.0/24', | ||
' me@foobar.com', 'you@home.net ', 't@git hub.com', | ||
'http:/my-something', 'sdf%#://#%asdf', | ||
'asdfasfasfd', '23423423423', 'asdf asdf', | ||
], validate); | ||
}); | ||
}); | ||
|
||
describe('creditCard', function () { | ||
var validate = extension.validations.string.creditCard; | ||
|
||
it('should pass valid values', function () { | ||
testPassingValues([ | ||
'4123412341234123', | ||
'4123412341234', | ||
'567256725672562', | ||
], validate); | ||
}); | ||
|
||
it('should fail invalid values', function () { | ||
testFailingValues([ | ||
123, null, [], false, | ||
'567256725672562232', | ||
'41-234-12341-234-123', | ||
'41 234 12341234 123', | ||
'4123-4123', '1234242234', | ||
], validate); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.