formats.js 11 KB

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