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