formats.ts 11 KB

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