basant307/AI_Governance_Project
048
1import type {Format, FormatDefinition} from "ajv"2import type {FormatValidator, FormatCompare} from "ajv/dist/types"3 4export type FormatMode = "fast" | "full"5 6export 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 34export type DefinedFormats = {35 [key in FormatName]: Format36}37 38function fmtDef(39 validate: RegExp | FormatValidator<string>,40 compare: FormatCompare<string>41): FormatDefinition<string> {42 return {validate, compare}43}44 45export const fullFormats: DefinedFormats = {46 // date: http://tools.ietf.org/html/rfc3339#section-5.647 date: fmtDef(date, compareDate),48 // date-time: http://tools.ietf.org/html/rfc3339#section-5.649 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-A54 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][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,58 // uri-template: https://tools.ietf.org/html/rfc657059 "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/72929462 // For test cases: https://mathiasbynens.be/demo/url-regex63 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.html69 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/rfc412273 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/rfc690175 // uri fragment: https://tools.ietf.org/html/rfc3986#appendix-A76 "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-0079 "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-types81 // byte: https://github.com/miguelmota/is-base6482 byte,83 // signed 32 bit integer84 int32: {type: "number", validate: validateInt32},85 // signed 64 bit integer86 int64: {type: "number", validate: validateInt64},87 // C-type float88 float: {type: "number", validate: validateNumber},89 // C-type double90 double: {type: "number", validate: validateNumber},91 // hint to the UI to hide input strings92 password: true,93 // unchecked string payload94 binary: true,95}96 97export 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 compareTime103 ),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 compareDateTime107 ),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 compareIsoTime111 ),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 compareIsoDateTime115 ),116 // uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js117 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-8829363121 // 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 126export const formatNames = Object.keys(fullFormats) as FormatName[]127 128function isLeapYear(year: number): boolean {129 // https://tools.ietf.org/html/rfc3339#appendix-C130 return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)131}132 133const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/134const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]135 136function date(str: string): boolean {137 // full-date from http://tools.ietf.org/html/rfc3339#section-5.6138 const matches: string[] | null = DATE.exec(str)139 if (!matches) return false140 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 151function compareDate(d1: string, d2: string): number | undefined {152 if (!(d1 && d2)) return undefined153 if (d1 > d2) return 1154 if (d1 < d2) return -1155 return 0156}157 158const TIME = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(z|([+-])(\d\d)(?::?(\d\d))?)?$/i159 160function 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 false164 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 : 1169 const tzH: number = +(matches[6] || 0)170 const tzM: number = +(matches[7] || 0)171 if (tzH > 23 || tzM > 59 || (strictTimeZone && !tz)) return false172 if (hr <= 23 && min <= 59 && sec < 60) return true173 // leap second174 const utcMin = min - tzM * tzSign175 const utcHr = hr - tzH * tzSign - (utcMin < 0 ? 1 : 0)176 return (utcHr === 23 || utcHr === -1) && (utcMin === 59 || utcMin === -1) && sec < 61177 }178}179 180function compareTime(s1: string, s2: string): number | undefined {181 if (!(s1 && s2)) return undefined182 const t1 = new Date("2020-01-01T" + s1).valueOf()183 const t2 = new Date("2020-01-01T" + s2).valueOf()184 if (!(t1 && t2)) return undefined185 return t1 - t2186}187 188function compareIsoTime(t1: string, t2: string): number | undefined {189 if (!(t1 && t2)) return undefined190 const a1 = TIME.exec(t1)191 const a2 = TIME.exec(t2)192 if (!(a1 && a2)) return undefined193 t1 = a1[1] + a1[2] + a1[3]194 t2 = a2[1] + a2[2] + a2[3]195 if (t1 > t2) return 1196 if (t1 < t2) return -1197 return 0198}199 200const DATE_TIME_SEPARATOR = /t|\s/i201function 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.6206 const dateTime: string[] = str.split(DATE_TIME_SEPARATOR)207 return dateTime.length === 2 && date(dateTime[0]) && time(dateTime[1])208 }209}210 211function compareDateTime(dt1: string, dt2: string): number | undefined {212 if (!(dt1 && dt2)) return undefined213 const d1 = new Date(dt1).valueOf()214 const d2 = new Date(dt2).valueOf()215 if (!(d1 && d2)) return undefined216 return d1 - d2217}218 219function compareIsoDateTime(dt1: string, dt2: string): number | undefined {220 if (!(dt1 && dt2)) return undefined221 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 undefined225 return res || compareTime(t1, t2)226}227 228const NOT_URI_FRAGMENT = /\/|:/229const URI =230 /^(?:[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})*)?$/i231 232function 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 237const BYTE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm238 239function byte(str: string): boolean {240 BYTE.lastIndex = 0241 return BYTE.test(str)242}243 244const MIN_INT32 = -(2 ** 31)245const MAX_INT32 = 2 ** 31 - 1246 247function validateInt32(value: number): boolean {248 return Number.isInteger(value) && value <= MAX_INT32 && value >= MIN_INT32249}250 251function validateInt64(value: number): boolean {252 // JSON and javascript max Int is 2**53, so any int that passes isInteger is valid for Int64253 return Number.isInteger(value)254}255 256function validateNumber(): boolean {257 return true258}259 260const Z_ANCHOR = /[^\\]\\Z/261function regex(str: string): boolean {262 if (Z_ANCHOR.test(str)) return false263 try {264 new RegExp(str)265 return true266 } catch (e) {267 return false268 }269}270 