source: imaps-frontend/node_modules/ajv-formats/src/formats.ts

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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