validators.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. var validator = require('../validator')
  2. , format = require('util').format
  3. , contextify = require('contextify')
  4. , assert = require('assert')
  5. , path = require('path')
  6. , fs = require('fs');
  7. var validator_js = fs.readFileSync(path.join(__dirname, '../validator.js')).toString();
  8. function test(options) {
  9. var args = options.args || [];
  10. args.unshift(null);
  11. if (options.valid) {
  12. options.valid.forEach(function (valid) {
  13. args[0] = valid;
  14. if (validator[options.validator].apply(validator, args) !== true) {
  15. var warning = format('validator.%s(%s) failed but should have passed',
  16. options.validator, args.join(', '));
  17. throw new Error(warning);
  18. }
  19. });
  20. }
  21. if (options.invalid) {
  22. options.invalid.forEach(function (invalid) {
  23. args[0] = invalid;
  24. if (validator[options.validator].apply(validator, args) !== false) {
  25. var warning = format('validator.%s(%s) passed but should have failed',
  26. options.validator, args.join(', '));
  27. throw new Error(warning);
  28. }
  29. });
  30. }
  31. }
  32. describe('Validators', function () {
  33. it('should validate email addresses', function () {
  34. test({
  35. validator: 'isEmail'
  36. , valid: [
  37. 'foo@bar.com'
  38. , 'x@x.x'
  39. , 'foo@bar.com.au'
  40. , 'foo+bar@bar.com'
  41. , 'hans.m端ller@test.com'
  42. , 'hans@m端ller.com'
  43. , 'test|123@m端ller.com'
  44. ]
  45. , invalid: [
  46. 'invalidemail@'
  47. , 'invalid.com'
  48. , '@invalid.com'
  49. , 'foo@bar.com.'
  50. , 'foo@bar.co.uk.'
  51. ]
  52. });
  53. });
  54. it('should validate URLs', function () {
  55. test({
  56. validator: 'isURL'
  57. , valid: [
  58. 'foobar.com'
  59. , 'www.foobar.com'
  60. , 'foobar.com/'
  61. , 'valid.au'
  62. , 'http://www.foobar.com/'
  63. , 'http://www.foobar.com:23/'
  64. , 'http://www.foobar.com:65535/'
  65. , 'http://www.foobar.com:5/'
  66. , 'https://www.foobar.com/'
  67. , 'ftp://www.foobar.com/'
  68. , 'http://www.foobar.com/~foobar'
  69. , 'http://user:pass@www.foobar.com/'
  70. , 'http://127.0.0.1/'
  71. , 'http://10.0.0.0/'
  72. , 'http://189.123.14.13/'
  73. , 'http://duckduckgo.com/?q=%2F'
  74. , 'http://foobar.com/t$-_.+!*\'(),'
  75. , 'http://localhost:3000/'
  76. , 'http://foobar.com/?foo=bar#baz=qux'
  77. , 'http://foobar.com?foo=bar'
  78. , 'http://foobar.com#baz=qux'
  79. , 'http://www.xn--froschgrn-x9a.net/'
  80. , 'http://xn--froschgrn-x9a.com/'
  81. , 'http://foo--bar.com'
  82. ]
  83. , invalid: [
  84. 'xyz://foobar.com'
  85. , 'invalid/'
  86. , 'invalid.x'
  87. , 'invalid.'
  88. , '.com'
  89. , 'http://com/'
  90. , 'http://300.0.0.1/'
  91. , 'mailto:foo@bar.com'
  92. , 'rtmp://foobar.com'
  93. , 'http://www.xn--.com/'
  94. , 'http://xn--.com/'
  95. , 'http://www.foobar.com:0/'
  96. , 'http://www.foobar.com:70000/'
  97. , 'http://www.foobar.com:99999/'
  98. , 'http://www.-foobar.com/'
  99. , 'http://www.foobar-.com/'
  100. , 'http://www.foo---bar.com/'
  101. , 'http://www.foo_bar.com/'
  102. , ''
  103. , 'http://foobar.com/' + new Array(2083).join('f'),
  104. , 'http://*.foo.com',
  105. , '*.foo.com',
  106. , '!.foo.com'
  107. ]
  108. });
  109. });
  110. it('should validate URLs with custom protocols', function () {
  111. test({
  112. validator: 'isURL'
  113. , args: [{
  114. protocols: [ 'rtmp' ]
  115. }]
  116. , valid: [
  117. , 'rtmp://foobar.com'
  118. ]
  119. , invalid: [
  120. 'http://foobar.com'
  121. ]
  122. });
  123. });
  124. it('should validate URLs with underscores', function () {
  125. test({
  126. validator: 'isURL'
  127. , args: [{
  128. allow_underscores: true
  129. }]
  130. , valid: [
  131. 'http://foo_bar.com'
  132. , 'http://pr.example_com.294.example.com/'
  133. ]
  134. , invalid: [
  135. 'http://foo__bar.com'
  136. ]
  137. });
  138. });
  139. it('should validate URLs that do not have a TLD', function () {
  140. test({
  141. validator: 'isURL'
  142. , args: [{
  143. require_tld: false
  144. }]
  145. , valid: [
  146. , 'http://foobar.com/'
  147. , 'http://foobar/'
  148. , 'foobar/'
  149. , 'foobar'
  150. ]
  151. , invalid: [
  152. 'foobar.'
  153. ]
  154. });
  155. });
  156. it('should let users specify whether URLs require a protocol', function () {
  157. test({
  158. validator: 'isURL'
  159. , args: [{
  160. require_protocol: true
  161. }]
  162. , valid: [
  163. , 'http://foobar.com/'
  164. , 'http://localhost/'
  165. ]
  166. , invalid: [
  167. 'foobar.com'
  168. , 'foobar'
  169. ]
  170. });
  171. });
  172. it('should let users specify a host whitelist', function () {
  173. test({
  174. validator: 'isURL'
  175. , args: [{
  176. host_whitelist: ['foo.com', 'bar.com']
  177. }]
  178. , valid: [
  179. 'http://bar.com/'
  180. , 'http://foo.com/'
  181. ]
  182. , invalid: [
  183. 'http://foobar.com'
  184. , 'http://foo.bar.com/'
  185. , 'http://qux.com'
  186. ]
  187. });
  188. });
  189. it('should let users specify a host blacklist', function () {
  190. test({
  191. validator: 'isURL'
  192. , args: [{
  193. host_blacklist: ['foo.com', 'bar.com']
  194. }]
  195. , valid: [
  196. 'http://foobar.com'
  197. , 'http://foo.bar.com/'
  198. , 'http://qux.com'
  199. ]
  200. , invalid: [
  201. 'http://bar.com/'
  202. , 'http://foo.com/'
  203. ]
  204. });
  205. });
  206. it('should validate IP addresses', function () {
  207. test({
  208. validator: 'isIP'
  209. , valid: [
  210. '127.0.0.1'
  211. , '0.0.0.0'
  212. , '255.255.255.255'
  213. , '1.2.3.4'
  214. , '::1'
  215. , '2001:db8:0000:1:1:1:1:1'
  216. ]
  217. , invalid: [
  218. 'abc'
  219. , '256.0.0.0'
  220. , '0.0.0.256'
  221. , '26.0.0.256'
  222. ]
  223. });
  224. test({
  225. validator: 'isIP'
  226. , args: [ 4 ]
  227. , valid: [
  228. '127.0.0.1'
  229. , '0.0.0.0'
  230. , '255.255.255.255'
  231. , '1.2.3.4'
  232. ]
  233. , invalid: [
  234. '::1'
  235. , '2001:db8:0000:1:1:1:1:1'
  236. ]
  237. });
  238. test({
  239. validator: 'isIP'
  240. , args: [ 6 ]
  241. , valid: [
  242. '::1'
  243. , '2001:db8:0000:1:1:1:1:1'
  244. ]
  245. , invalid: [
  246. '127.0.0.1'
  247. , '0.0.0.0'
  248. , '255.255.255.255'
  249. , '1.2.3.4'
  250. ]
  251. });
  252. test({
  253. validator: 'isIP'
  254. , args: [ 10 ]
  255. , valid: [
  256. ]
  257. , invalid: [
  258. '127.0.0.1'
  259. , '0.0.0.0'
  260. , '255.255.255.255'
  261. , '1.2.3.4'
  262. , '::1'
  263. , '2001:db8:0000:1:1:1:1:1'
  264. ]
  265. });
  266. });
  267. it('should validate FQDN', function () {
  268. test({
  269. validator: 'isFQDN'
  270. , valid: [
  271. 'domain.com'
  272. , 'dom.plato'
  273. , 'a.domain.co',
  274. , 'foo--bar.com',
  275. , 'xn--froschgrn-x9a.com',
  276. , 'rebecca.blackfriday'
  277. ]
  278. , invalid: [
  279. 'abc'
  280. , '256.0.0.0'
  281. , '_.com',
  282. , '*.some.com',
  283. , 's!ome.com',
  284. , 'domain.com/',
  285. , '/more.com'
  286. ]
  287. });
  288. });
  289. it('should validate alpha strings', function () {
  290. test({
  291. validator: 'isAlpha'
  292. , valid: [
  293. 'abc'
  294. , 'ABC'
  295. , 'FoObar'
  296. ]
  297. , invalid: [
  298. 'abc1'
  299. , ' foo '
  300. , ''
  301. ]
  302. });
  303. });
  304. it('should validate alphanumeric strings', function () {
  305. test({
  306. validator: 'isAlphanumeric'
  307. , valid: [
  308. 'abc123'
  309. , 'ABC11'
  310. ]
  311. , invalid: [
  312. 'abc '
  313. , 'foo!!'
  314. ]
  315. });
  316. });
  317. it('should validate numeric strings', function () {
  318. test({
  319. validator: 'isNumeric'
  320. , valid: [
  321. '123'
  322. , '00123'
  323. , '-00123'
  324. , '0'
  325. , '-0'
  326. ]
  327. , invalid: [
  328. '123.123'
  329. , ' '
  330. , '.'
  331. ]
  332. });
  333. });
  334. it('should validate lowercase strings', function () {
  335. test({
  336. validator: 'isLowercase'
  337. , valid: [
  338. 'abc'
  339. , 'abc123'
  340. , 'this is lowercase.'
  341. , 'tr竪s 端ber'
  342. ]
  343. , invalid: [
  344. 'fooBar'
  345. , '123A'
  346. ]
  347. });
  348. });
  349. it('should validate uppercase strings', function () {
  350. test({
  351. validator: 'isUppercase'
  352. , valid: [
  353. 'ABC'
  354. , 'ABC123'
  355. , 'ALL CAPS IS FUN.'
  356. , ' .'
  357. ]
  358. , invalid: [
  359. 'fooBar'
  360. , '123abc'
  361. ]
  362. });
  363. });
  364. it('should validate integers', function () {
  365. test({
  366. validator: 'isInt'
  367. , valid: [
  368. '13'
  369. , '123'
  370. , '0'
  371. , '123'
  372. , '-0'
  373. ]
  374. , invalid: [
  375. '01'
  376. , '-01'
  377. , '000'
  378. , '100e10'
  379. , '123.123'
  380. , ' '
  381. , ''
  382. ]
  383. });
  384. });
  385. it('should validate floats', function () {
  386. test({
  387. validator: 'isFloat'
  388. , valid: [
  389. '123'
  390. , '123.'
  391. , '123.123'
  392. , '-123.123'
  393. , '-0.123'
  394. , '0.123'
  395. , '.0'
  396. , '01.123'
  397. , '-0.22250738585072011e-307'
  398. ]
  399. , invalid: [
  400. '-.123'
  401. , ' '
  402. , ''
  403. , 'foo'
  404. ]
  405. });
  406. });
  407. it('should validate hexadecimal strings', function () {
  408. test({
  409. validator: 'isHexadecimal'
  410. , valid: [
  411. 'deadBEEF'
  412. , 'ff0044'
  413. ]
  414. , invalid: [
  415. 'abcdefg'
  416. , ''
  417. , '..'
  418. ]
  419. });
  420. });
  421. it('should validate hexadecimal color strings', function () {
  422. test({
  423. validator: 'isHexColor'
  424. , valid: [
  425. '#ff0034'
  426. , '#CCCCCC'
  427. , 'fff'
  428. , '#f00'
  429. ]
  430. , invalid: [
  431. '#ff'
  432. , 'fff0'
  433. , '#ff12FG'
  434. ]
  435. });
  436. });
  437. it('should validate null strings', function () {
  438. test({
  439. validator: 'isNull'
  440. , valid: [
  441. ''
  442. , NaN
  443. , []
  444. , undefined
  445. , null
  446. ]
  447. , invalid: [
  448. , ' '
  449. , 'foo'
  450. ]
  451. });
  452. });
  453. it('should validate strings against an expected value', function () {
  454. test({ validator: 'equals', args: ['abc'], valid: ['abc'], invalid: ['Abc', '123'] });
  455. });
  456. it('should validate strings contain another string', function () {
  457. test({ validator: 'contains', args: ['foo'], valid: ['foo', 'foobar', 'bazfoo'],
  458. invalid: ['bar', 'fobar'] });
  459. });
  460. it('should validate strings against a pattern', function () {
  461. test({ validator: 'matches', args: [/abc/], valid: ['abc', 'abcdef', '123abc'],
  462. invalid: ['acb', 'Abc'] });
  463. test({ validator: 'matches', args: ['abc'], valid: ['abc', 'abcdef', '123abc'],
  464. invalid: ['acb', 'Abc'] });
  465. test({ validator: 'matches', args: ['abc', 'i'], valid: ['abc', 'abcdef', '123abc', 'AbC'],
  466. invalid: ['acb'] });
  467. });
  468. it('should validate strings by length', function () {
  469. test({ validator: 'isLength', args: [2], valid: ['abc', 'de', 'abcd'], invalid: [ '', 'a' ] });
  470. test({ validator: 'isLength', args: [2, 3], valid: ['abc', 'de'], invalid: [ '', 'a', 'abcd' ] });
  471. test({ validator: 'isLength', args: [2, 3], valid: ['干𩸽', '𠮷野家'], invalid: [ '', '𠀋', '千竈通り' ] });
  472. });
  473. it('should validate strings by byte length', function () {
  474. test({ validator: 'isByteLength', args: [2], valid: ['abc', 'de', 'abcd'], invalid: [ '', 'a' ] });
  475. test({ validator: 'isByteLength', args: [2, 3], valid: ['abc', 'de'], invalid: [ '', 'a', 'abcd' ] });
  476. });
  477. it('should validate UUIDs', function () {
  478. test({
  479. validator: 'isUUID'
  480. , valid: [
  481. 'A987FBC9-4BED-3078-CF07-9141BA07C9F3'
  482. , 'A987FBC9-4BED-4078-8F07-9141BA07C9F3'
  483. , 'A987FBC9-4BED-5078-AF07-9141BA07C9F3'
  484. ]
  485. , invalid: [
  486. ''
  487. , 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3'
  488. , 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx'
  489. , 'A987FBC94BED3078CF079141BA07C9F3'
  490. , '934859'
  491. , '987FBC9-4BED-3078-CF07A-9141BA07C9F3'
  492. , 'AAAAAAAA-1111-1111-AAAG-111111111111'
  493. ]
  494. });
  495. test({
  496. validator: 'isUUID'
  497. , args: [ 3 ]
  498. , valid: [
  499. 'A987FBC9-4BED-3078-CF07-9141BA07C9F3'
  500. ]
  501. , invalid: [
  502. ''
  503. , 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3'
  504. , '934859'
  505. , 'AAAAAAAA-1111-1111-AAAG-111111111111'
  506. , 'A987FBC9-4BED-4078-8F07-9141BA07C9F3'
  507. , 'A987FBC9-4BED-5078-AF07-9141BA07C9F3'
  508. ]
  509. });
  510. test({
  511. validator: 'isUUID'
  512. , args: [ 4 ]
  513. , valid: [
  514. '713ae7e3-cb32-45f9-adcb-7c4fa86b90c1'
  515. , '625e63f3-58f5-40b7-83a1-a72ad31acffb'
  516. , '57b73598-8764-4ad0-a76a-679bb6640eb1'
  517. , '9c858901-8a57-4791-81fe-4c455b099bc9'
  518. ]
  519. , invalid: [
  520. ''
  521. , 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3'
  522. , '934859'
  523. , 'AAAAAAAA-1111-1111-AAAG-111111111111'
  524. , 'A987FBC9-4BED-5078-AF07-9141BA07C9F3'
  525. , 'A987FBC9-4BED-3078-CF07-9141BA07C9F3'
  526. ]
  527. });
  528. test({
  529. validator: 'isUUID'
  530. , args: [ 5 ]
  531. , valid: [
  532. '987FBC97-4BED-5078-AF07-9141BA07C9F3'
  533. , '987FBC97-4BED-5078-BF07-9141BA07C9F3'
  534. , '987FBC97-4BED-5078-8F07-9141BA07C9F3'
  535. , '987FBC97-4BED-5078-9F07-9141BA07C9F3'
  536. ]
  537. , invalid: [
  538. ''
  539. , 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3'
  540. , '934859'
  541. , 'AAAAAAAA-1111-1111-AAAG-111111111111'
  542. , '9c858901-8a57-4791-81fe-4c455b099bc9'
  543. , 'A987FBC9-4BED-3078-CF07-9141BA07C9F3'
  544. ]
  545. });
  546. });
  547. it('should validate a string that is in another string or array', function () {
  548. test({ validator: 'isIn', args: ['foobar'], valid: ['foo', 'bar', 'foobar', ''],
  549. invalid: ['foobarbaz', 'barfoo'] });
  550. test({ validator: 'isIn', args: [['foo', 'bar']], valid: ['foo', 'bar'],
  551. invalid: ['foobar', 'barfoo', ''] });
  552. test({ validator: 'isIn', args: [[1, 2, 3]], valid: ['1', '2', '3'],
  553. invalid: ['4', ''] });
  554. test({ validator: 'isIn', invalid: ['foo', ''] });
  555. });
  556. it('should validate dates', function () {
  557. test({
  558. validator: 'isDate'
  559. , valid: [
  560. '2011-08-04'
  561. , '04. 08. 2011.'
  562. , '08/04/2011'
  563. , '2011.08.04'
  564. , '4. 8. 2011. GMT'
  565. , '2011-08-04 12:00'
  566. ]
  567. , invalid: [
  568. 'foo'
  569. , '2011-foo-04'
  570. , 'GMT'
  571. ]
  572. });
  573. });
  574. it('should validate dates against a start date', function () {
  575. test({ validator: 'isAfter', args: ['2011-08-03'],
  576. valid: [ '2011-08-04', new Date(2011, 8, 10) ],
  577. invalid: [ '2010-07-02', '2011-08-03', new Date(0), 'foo'] });
  578. test({ validator: 'isAfter',
  579. valid: [ '2100-08-04', new Date(Date.now() + 86400000) ],
  580. invalid: [ '2010-07-02', new Date(0) ] });
  581. });
  582. it('should validate dates against an end date', function () {
  583. test({ validator: 'isBefore', args: ['08/04/2011'],
  584. valid: [ '2010-07-02', '2010-08-04', new Date(0) ],
  585. invalid: [ '08/04/2011', new Date(2011, 9, 10) ] });
  586. test({ validator: 'isBefore', args: [ new Date(2011, 7, 4) ],
  587. valid: [ '2010-07-02', '2010-08-04', new Date(0) ],
  588. invalid: [ '08/04/2011', new Date(2011, 9, 10) ] });
  589. test({ validator: 'isBefore',
  590. valid: [ '2000-08-04', new Date(0), new Date(Date.now() - 86400000) ],
  591. invalid: [ '2100-07-02', new Date(2017, 10, 10) ] });
  592. });
  593. it('should validate that integer strings are divisible by a number', function () {
  594. test({
  595. validator: 'isDivisibleBy'
  596. , args: [ 2 ]
  597. , valid: [ '2', '4', '100', '1000' ]
  598. , invalid: [
  599. '1'
  600. , '2.5'
  601. , '101'
  602. , 'foo'
  603. , ''
  604. ]
  605. });
  606. });
  607. it('should validate credit cards', function () {
  608. test({
  609. validator: 'isCreditCard'
  610. , valid: [
  611. '375556917985515'
  612. , '36050234196908'
  613. , '4716461583322103'
  614. , '4716-2210-5188-5662'
  615. , '4929 7226 5379 7141'
  616. , '5398228707871527'
  617. ]
  618. , invalid: [
  619. 'foo'
  620. , 'foo'
  621. , '5398228707871528'
  622. ]
  623. });
  624. });
  625. it('should validate ISBNs', function () {
  626. test({
  627. validator: 'isISBN'
  628. , args: [ 10 ]
  629. , valid: [
  630. '3836221195', '3-8362-2119-5', '3 8362 2119 5'
  631. , '1617290858', '1-61729-085-8', '1 61729 085-8'
  632. , '0007269706', '0-00-726970-6', '0 00 726970 6'
  633. , '3423214120', '3-423-21412-0', '3 423 21412 0'
  634. , '340101319X', '3-401-01319-X', '3 401 01319 X'
  635. ]
  636. , invalid: [
  637. '3423214121', '3-423-21412-1', '3 423 21412 1'
  638. , '978-3836221191', '9783836221191',
  639. , '123456789a', 'foo', ''
  640. ]
  641. });
  642. test({
  643. validator: 'isISBN'
  644. , args: [ 13 ]
  645. , valid: [
  646. '9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1'
  647. , '9783401013190', '978-3401013190', '978 3401013190'
  648. , '9784873113685', '978-4-87311-368-5', '978 4 87311 368 5'
  649. ]
  650. , invalid: [
  651. '9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0'
  652. , '3836221195', '3-8362-2119-5', '3 8362 2119 5'
  653. , '01234567890ab', 'foo', ''
  654. ]
  655. });
  656. test({
  657. validator: 'isISBN'
  658. , valid: [
  659. '340101319X'
  660. , '9784873113685'
  661. ]
  662. , invalid: [
  663. '3423214121'
  664. , '9783836221190'
  665. ]
  666. });
  667. test({
  668. validator: 'isISBN'
  669. , args: [ 'foo' ]
  670. , invalid: [
  671. '340101319X'
  672. , '9784873113685'
  673. ]
  674. });
  675. });
  676. it('should validate JSON', function () {
  677. test({
  678. validator: 'isJSON'
  679. , valid: [
  680. '{ "key": "value" }'
  681. ]
  682. , invalid: [
  683. '{ key: "value" }'
  684. , { "key": "value" }
  685. , { key: 'value' }
  686. , '{ \'key\': \'value\' }'
  687. ]
  688. });
  689. });
  690. it('should validate multibyte strings', function () {
  691. test({
  692. validator: 'isMultibyte'
  693. , valid: [
  694. 'ひらがな・カタカナ、.漢字'
  695. , 'あいうえお foobar'
  696. , 'test@example.com'
  697. , '1234abcDExyz'
  698. , 'カタカナ'
  699. ]
  700. , invalid: [
  701. 'abc'
  702. , 'abc123'
  703. , '<>@" *.'
  704. ]
  705. });
  706. });
  707. it('should validate ascii strings', function () {
  708. test({
  709. validator: 'isAscii'
  710. , valid: [
  711. 'foobar'
  712. , '0987654321'
  713. , 'test@example.com'
  714. , '1234abcDEF'
  715. ]
  716. , invalid: [
  717. 'foobar'
  718. , 'xyz098'
  719. , '123456'
  720. , 'カタカナ'
  721. ]
  722. });
  723. });
  724. it('should validate full-width strings', function () {
  725. test({
  726. validator: 'isFullWidth'
  727. , valid: [
  728. 'ひらがな・カタカナ、.漢字'
  729. , '3ー0 a@com'
  730. , 'Fカタカナ゙ᆲ'
  731. , 'Good=Parts'
  732. ]
  733. , invalid: [
  734. 'abc'
  735. , 'abc123'
  736. , '!"#$%&()<>/+=-_? ~^|.,@`{}[]'
  737. ]
  738. });
  739. });
  740. it('should validate half-width strings', function () {
  741. test({
  742. validator: 'isHalfWidth'
  743. , valid: [
  744. '!"#$%&()<>/+=-_? ~^|.,@`{}[]'
  745. , 'l-btn_02--active'
  746. , 'abc123い'
  747. , 'カタカナ゙ᆲ←'
  748. ]
  749. , invalid: [
  750. , 'あいうえお'
  751. , '0011'
  752. ]
  753. });
  754. });
  755. it('should validate variable-width strings', function () {
  756. test({
  757. validator: 'isVariableWidth'
  758. , valid: [
  759. 'ひらがなカタカナ漢字ABCDE'
  760. , '3ー0123'
  761. , 'Fカタカナ゙ᆲ'
  762. , 'Good=Parts'
  763. ]
  764. , invalid: [
  765. 'abc'
  766. , 'abc123'
  767. , '!"#$%&()<>/+=-_? ~^|.,@`{}[]'
  768. , 'ひらがな・カタカナ、.漢字'
  769. , '123456'
  770. , 'カタカナ゙ᆲ'
  771. ]
  772. });
  773. });
  774. it('should validate surrogate pair strings', function () {
  775. test({
  776. validator: 'isSurrogatePair'
  777. , valid: [
  778. '𠮷野𠮷'
  779. , '𩸽'
  780. , 'ABC千𥧄1-2-3'
  781. ]
  782. , invalid: [
  783. '吉野竈'
  784. , '鮪'
  785. , 'ABC1-2-3'
  786. ]
  787. });
  788. });
  789. it('should validate base64 strings', function () {
  790. test({
  791. validator: 'isBase64'
  792. , valid: [
  793. 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4='
  794. , 'Vml2YW11cyBmZXJtZW50dW0gc2VtcGVyIHBvcnRhLg=='
  795. , 'U3VzcGVuZGlzc2UgbGVjdHVzIGxlbw=='
  796. , 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuMPNS1Ufof9EW/M98FNw'+
  797. 'UAKrwflsqVxaxQjBQnHQmiI7Vac40t8x7pIb8gLGV6wL7sBTJiPovJ0V7y7oc0Ye'+
  798. 'rhKh0Rm4skP2z/jHwwZICgGzBvA0rH8xlhUiTvcwDCJ0kc+fh35hNt8srZQM4619'+
  799. 'FTgB66Xmp4EtVyhpQV+t02g6NzK72oZI0vnAvqhpkxLeLiMCyrI416wHm5Tkukhx'+
  800. 'QmcL2a6hNOyu0ixX/x2kSFXApEnVrJ+/IxGyfyw8kf4N2IZpW5nEP847lpfj0SZZ'+
  801. 'Fwrd1mnfnDbYohX2zRptLy2ZUn06Qo9pkG5ntvFEPo9bfZeULtjYzIl6K8gJ2uGZ'+
  802. 'HQIDAQAB'
  803. ]
  804. , invalid: [
  805. '12345'
  806. , ''
  807. , 'Vml2YW11cyBmZXJtZtesting123'
  808. ]
  809. });
  810. for (var i = 0, str = '', encoded; i < 1000; i++) {
  811. str += String.fromCharCode(Math.random() * 26 | 97);
  812. encoded = new Buffer(str).toString('base64');
  813. if (!validator.isBase64(encoded)) {
  814. var msg = format('validator.isBase64() failed with "%s"', encoded);
  815. throw new Error(msg);
  816. }
  817. }
  818. });
  819. it('should define the module using an AMD-compatible loader', function () {
  820. var window = {
  821. validator: null
  822. , define: function (module) {
  823. this.validator = module;
  824. }
  825. };
  826. window.define.amd = true;
  827. var sandbox = contextify(window);
  828. sandbox.run(validator_js);
  829. sandbox.dispose();
  830. assert.equal(window.validator.trim(' foobar '), 'foobar');
  831. });
  832. it('should bind validator to the window if no module loaders are available', function () {
  833. var window = {};
  834. var sandbox = contextify(window);
  835. sandbox.run(validator_js);
  836. sandbox.dispose();
  837. assert.equal(window.validator.trim(' foobar '), 'foobar');
  838. });
  839. });