formats.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.formatNames = exports.fastFormats = exports.fullFormats = void 0;
  4. function fmtDef(validate, compare) {
  5. return { validate, compare };
  6. }
  7. exports.fullFormats = {
  8. // date: http://tools.ietf.org/html/rfc3339#section-5.6
  9. date: fmtDef(date, compareDate),
  10. // date-time: http://tools.ietf.org/html/rfc3339#section-5.6
  11. time: fmtDef(getTime(true), compareTime),
  12. "date-time": fmtDef(getDateTime(true), compareDateTime),
  13. "iso-time": fmtDef(getTime(), compareIsoTime),
  14. "iso-date-time": fmtDef(getDateTime(), compareIsoDateTime),
  15. // duration: https://tools.ietf.org/html/rfc3339#appendix-A
  16. duration: /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/,
  17. uri,
  18. "uri-reference": /^(?:[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,
  19. // uri-template: https://tools.ietf.org/html/rfc6570
  20. "uri-template": /^(?:(?:[^\x00-\x20"'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*$/i,
  21. // For the source: https://gist.github.com/dperini/729294
  22. // For test cases: https://mathiasbynens.be/demo/url-regex
  23. url: /^(?:https?|ftp):\/\/(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)(?:\.(?:[a-z0-9\u{00a1}-\u{ffff}]+-)*[a-z0-9\u{00a1}-\u{ffff}]+)*(?:\.(?:[a-z\u{00a1}-\u{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/iu,
  24. email: /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
  25. hostname: /^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i,
  26. // optimized https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html
  27. ipv4: /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/,
  28. ipv6: /^((([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}))|:)))$/i,
  29. regex,
  30. // uuid: http://tools.ietf.org/html/rfc4122
  31. uuid: /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
  32. // JSON-pointer: https://tools.ietf.org/html/rfc6901
  33. // uri fragment: https://tools.ietf.org/html/rfc3986#appendix-A
  34. "json-pointer": /^(?:\/(?:[^~/]|~0|~1)*)*$/,
  35. "json-pointer-uri-fragment": /^#(?:\/(?:[a-z0-9_\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i,
  36. // relative JSON-pointer: http://tools.ietf.org/html/draft-luff-relative-json-pointer-00
  37. "relative-json-pointer": /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/,
  38. // the following formats are used by the openapi specification: https://spec.openapis.org/oas/v3.0.0#data-types
  39. // byte: https://github.com/miguelmota/is-base64
  40. byte,
  41. // signed 32 bit integer
  42. int32: { type: "number", validate: validateInt32 },
  43. // signed 64 bit integer
  44. int64: { type: "number", validate: validateInt64 },
  45. // C-type float
  46. float: { type: "number", validate: validateNumber },
  47. // C-type double
  48. double: { type: "number", validate: validateNumber },
  49. // hint to the UI to hide input strings
  50. password: true,
  51. // unchecked string payload
  52. binary: true,
  53. };
  54. exports.fastFormats = {
  55. ...exports.fullFormats,
  56. date: fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d$/, compareDate),
  57. time: fmtDef(/^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, compareTime),
  58. "date-time": fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, compareDateTime),
  59. "iso-time": fmtDef(/^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, compareIsoTime),
  60. "iso-date-time": fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, compareIsoDateTime),
  61. // uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js
  62. uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i,
  63. "uri-reference": /^(?:(?:[a-z][a-z0-9+\-.]*:)?\/?\/)?(?:[^\\\s#][^\s#]*)?(?:#[^\\\s]*)?$/i,
  64. // email (sources from jsen validator):
  65. // http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363
  66. // http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'wilful violation')
  67. 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,
  68. };
  69. exports.formatNames = Object.keys(exports.fullFormats);
  70. function isLeapYear(year) {
  71. // https://tools.ietf.org/html/rfc3339#appendix-C
  72. return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
  73. }
  74. const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
  75. const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  76. function date(str) {
  77. // full-date from http://tools.ietf.org/html/rfc3339#section-5.6
  78. const matches = DATE.exec(str);
  79. if (!matches)
  80. return false;
  81. const year = +matches[1];
  82. const month = +matches[2];
  83. const day = +matches[3];
  84. return (month >= 1 &&
  85. month <= 12 &&
  86. day >= 1 &&
  87. day <= (month === 2 && isLeapYear(year) ? 29 : DAYS[month]));
  88. }
  89. function compareDate(d1, d2) {
  90. if (!(d1 && d2))
  91. return undefined;
  92. if (d1 > d2)
  93. return 1;
  94. if (d1 < d2)
  95. return -1;
  96. return 0;
  97. }
  98. const TIME = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(z|([+-])(\d\d)(?::?(\d\d))?)?$/i;
  99. function getTime(strictTimeZone) {
  100. return function time(str) {
  101. const matches = TIME.exec(str);
  102. if (!matches)
  103. return false;
  104. const hr = +matches[1];
  105. const min = +matches[2];
  106. const sec = +matches[3];
  107. const tz = matches[4];
  108. const tzSign = matches[5] === "-" ? -1 : 1;
  109. const tzH = +(matches[6] || 0);
  110. const tzM = +(matches[7] || 0);
  111. if (tzH > 23 || tzM > 59 || (strictTimeZone && !tz))
  112. return false;
  113. if (hr <= 23 && min <= 59 && sec < 60)
  114. return true;
  115. // leap second
  116. const utcMin = min - tzM * tzSign;
  117. const utcHr = hr - tzH * tzSign - (utcMin < 0 ? 1 : 0);
  118. return (utcHr === 23 || utcHr === -1) && (utcMin === 59 || utcMin === -1) && sec < 61;
  119. };
  120. }
  121. function compareTime(s1, s2) {
  122. if (!(s1 && s2))
  123. return undefined;
  124. const t1 = new Date("2020-01-01T" + s1).valueOf();
  125. const t2 = new Date("2020-01-01T" + s2).valueOf();
  126. if (!(t1 && t2))
  127. return undefined;
  128. return t1 - t2;
  129. }
  130. function compareIsoTime(t1, t2) {
  131. if (!(t1 && t2))
  132. return undefined;
  133. const a1 = TIME.exec(t1);
  134. const a2 = TIME.exec(t2);
  135. if (!(a1 && a2))
  136. return undefined;
  137. t1 = a1[1] + a1[2] + a1[3];
  138. t2 = a2[1] + a2[2] + a2[3];
  139. if (t1 > t2)
  140. return 1;
  141. if (t1 < t2)
  142. return -1;
  143. return 0;
  144. }
  145. const DATE_TIME_SEPARATOR = /t|\s/i;
  146. function getDateTime(strictTimeZone) {
  147. const time = getTime(strictTimeZone);
  148. return function date_time(str) {
  149. // http://tools.ietf.org/html/rfc3339#section-5.6
  150. const dateTime = str.split(DATE_TIME_SEPARATOR);
  151. return dateTime.length === 2 && date(dateTime[0]) && time(dateTime[1]);
  152. };
  153. }
  154. function compareDateTime(dt1, dt2) {
  155. if (!(dt1 && dt2))
  156. return undefined;
  157. const d1 = new Date(dt1).valueOf();
  158. const d2 = new Date(dt2).valueOf();
  159. if (!(d1 && d2))
  160. return undefined;
  161. return d1 - d2;
  162. }
  163. function compareIsoDateTime(dt1, dt2) {
  164. if (!(dt1 && dt2))
  165. return undefined;
  166. const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR);
  167. const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR);
  168. const res = compareDate(d1, d2);
  169. if (res === undefined)
  170. return undefined;
  171. return res || compareTime(t1, t2);
  172. }
  173. const NOT_URI_FRAGMENT = /\/|:/;
  174. const URI = /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/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;=:@/?]|%[0-9a-f]{2})*)?$/i;
  175. function uri(str) {
  176. // http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required "."
  177. return NOT_URI_FRAGMENT.test(str) && URI.test(str);
  178. }
  179. const BYTE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm;
  180. function byte(str) {
  181. BYTE.lastIndex = 0;
  182. return BYTE.test(str);
  183. }
  184. const MIN_INT32 = -(2 ** 31);
  185. const MAX_INT32 = 2 ** 31 - 1;
  186. function validateInt32(value) {
  187. return Number.isInteger(value) && value <= MAX_INT32 && value >= MIN_INT32;
  188. }
  189. function validateInt64(value) {
  190. // JSON and javascript max Int is 2**53, so any int that passes isInteger is valid for Int64
  191. return Number.isInteger(value);
  192. }
  193. function validateNumber() {
  194. return true;
  195. }
  196. const Z_ANCHOR = /[^\\]\\Z/;
  197. function regex(str) {
  198. if (Z_ANCHOR.test(str))
  199. return false;
  200. try {
  201. new RegExp(str);
  202. return true;
  203. }
  204. catch (e) {
  205. return false;
  206. }
  207. }
  208. //# sourceMappingURL=formats.js.map