formats.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict';
  2. var util = require('./util');
  3. var DATE = /^\d\d\d\d-(\d\d)-(\d\d)$/;
  4. var DAYS = [0,31,29,31,30,31,30,31,31,30,31,30,31];
  5. var TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d:\d\d)?$/i;
  6. var HOSTNAME = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*$/i;
  7. var URI = /^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@\/?]|%[0-9a-f]{2})*)?(?:\#(?:[a-z0-9\-._~!$&'()*+,;=:@\/?]|%[0-9a-f]{2})*)?$/i;
  8. var UUID = /^(?:urn\:uuid\:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
  9. var JSON_POINTER = /^(?:\/(?:[^~\/]|~0|~1)*)*$|^\#(?:\/(?:[a-z0-9_\-\.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i;
  10. var RELATIVE_JSON_POINTER = /^(?:0|[1-9][0-9]*)(?:\#|(?:\/(?:[^~\/]|~0|~1)*)*)$/;
  11. module.exports = formats;
  12. function formats(mode) {
  13. mode = mode == 'full' ? 'full' : 'fast';
  14. var formatDefs = util.copy(formats[mode]);
  15. for (var fName in formats.compare) {
  16. formatDefs[fName] = {
  17. validate: formatDefs[fName],
  18. compare: formats.compare[fName]
  19. };
  20. }
  21. return formatDefs;
  22. }
  23. formats.fast = {
  24. // date: http://tools.ietf.org/html/rfc3339#section-5.6
  25. date: /^\d\d\d\d-[0-1]\d-[0-3]\d$/,
  26. // date-time: http://tools.ietf.org/html/rfc3339#section-5.6
  27. time: /^[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:z|[+-]\d\d:\d\d)?$/i,
  28. 'date-time': /^\d\d\d\d-[0-1]\d-[0-3]\d[t\s][0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:z|[+-]\d\d:\d\d)$/i,
  29. // uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js
  30. uri: /^(?:[a-z][a-z0-9+-.]*)?(?:\:|\/)\/?[^\s]*$/i,
  31. // email (sources from jsen validator):
  32. // http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363
  33. // http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'willful violation')
  34. email: /^[a-z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i,
  35. hostname: HOSTNAME,
  36. // optimized https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html
  37. ipv4: /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/,
  38. // optimized http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
  39. ipv6: /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i,
  40. regex: regex,
  41. // uuid: http://tools.ietf.org/html/rfc4122
  42. uuid: UUID,
  43. // JSON-pointer: https://tools.ietf.org/html/rfc6901
  44. // uri fragment: https://tools.ietf.org/html/rfc3986#appendix-A
  45. 'json-pointer': JSON_POINTER,
  46. // relative JSON-pointer: http://tools.ietf.org/html/draft-luff-relative-json-pointer-00
  47. 'relative-json-pointer': RELATIVE_JSON_POINTER
  48. };
  49. formats.full = {
  50. date: date,
  51. time: time,
  52. 'date-time': date_time,
  53. uri: uri,
  54. email: /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!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){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i,
  58. regex: regex,
  59. uuid: UUID,
  60. 'json-pointer': JSON_POINTER,
  61. 'relative-json-pointer': RELATIVE_JSON_POINTER
  62. };
  63. formats.compare = {
  64. date: compareDate,
  65. time: compareTime,
  66. 'date-time': compareDateTime
  67. };
  68. function date(str) {
  69. // full-date from http://tools.ietf.org/html/rfc3339#section-5.6
  70. var matches = str.match(DATE);
  71. if (!matches) return false;
  72. var month = +matches[1];
  73. var day = +matches[2];
  74. return month >= 1 && month <= 12 && day >= 1 && day <= DAYS[month];
  75. }
  76. function time(str, full) {
  77. var matches = str.match(TIME);
  78. if (!matches) return false;
  79. var hour = matches[1];
  80. var minute = matches[2];
  81. var second = matches[3];
  82. var timeZone = matches[5];
  83. return hour <= 23 && minute <= 59 && second <= 59 && (!full || timeZone);
  84. }
  85. var DATE_TIME_SEPARATOR = /t|\s/i;
  86. function date_time(str) {
  87. // http://tools.ietf.org/html/rfc3339#section-5.6
  88. var dateTime = str.split(DATE_TIME_SEPARATOR);
  89. return dateTime.length == 2 && date(dateTime[0]) && time(dateTime[1], true);
  90. }
  91. function hostname(str) {
  92. // https://tools.ietf.org/html/rfc1034#section-3.5
  93. // https://tools.ietf.org/html/rfc1123#section-2
  94. return str.length <= 255 && HOSTNAME.test(str);
  95. }
  96. var NOT_URI_FRAGMENT = /\/|\:/;
  97. function uri(str) {
  98. // http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required "."
  99. return NOT_URI_FRAGMENT.test(str) && URI.test(str);
  100. }
  101. function regex(str) {
  102. try {
  103. new RegExp(str);
  104. return true;
  105. } catch(e) {
  106. return false;
  107. }
  108. }
  109. function compareDate(d1, d2) {
  110. if (!(d1 && d2)) return;
  111. if (d1 > d2) return 1;
  112. if (d1 < d2) return -1;
  113. if (d1 === d2) return 0;
  114. }
  115. function compareTime(t1, t2) {
  116. if (!(t1 && t2)) return;
  117. t1 = t1.match(TIME);
  118. t2 = t2.match(TIME);
  119. if (!(t1 && t2)) return;
  120. t1 = t1[1] + t1[2] + t1[3] + (t1[4]||'');
  121. t2 = t2[1] + t2[2] + t2[3] + (t2[4]||'');
  122. if (t1 > t2) return 1;
  123. if (t1 < t2) return -1;
  124. if (t1 === t2) return 0;
  125. }
  126. function compareDateTime(dt1, dt2) {
  127. if (!(dt1 && dt2)) return;
  128. dt1 = dt1.split(DATE_TIME_SEPARATOR);
  129. dt2 = dt2.split(DATE_TIME_SEPARATOR);
  130. var res = compareDate(dt1[0], dt2[0]);
  131. if (res === undefined) return;
  132. return res || compareTime(dt1[1], dt2[1]);
  133. }