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