Updated the files.
This commit is contained in:
parent
1553e6b971
commit
753967d4f5
23418 changed files with 3784666 additions and 0 deletions
232
my-app/node_modules/ajv-formats/src/formats.ts
generated
vendored
Executable file
232
my-app/node_modules/ajv-formats/src/formats.ts
generated
vendored
Executable file
|
@ -0,0 +1,232 @@
|
|||
import type {Format, FormatDefinition} from "ajv"
|
||||
import type {FormatValidator, FormatCompare} from "ajv/dist/types"
|
||||
|
||||
export type FormatMode = "fast" | "full"
|
||||
|
||||
export type FormatName =
|
||||
| "date"
|
||||
| "time"
|
||||
| "date-time"
|
||||
| "duration"
|
||||
| "uri"
|
||||
| "uri-reference"
|
||||
| "uri-template"
|
||||
| "url"
|
||||
| "email"
|
||||
| "hostname"
|
||||
| "ipv4"
|
||||
| "ipv6"
|
||||
| "regex"
|
||||
| "uuid"
|
||||
| "json-pointer"
|
||||
| "json-pointer-uri-fragment"
|
||||
| "relative-json-pointer"
|
||||
| "byte"
|
||||
| "int32"
|
||||
| "int64"
|
||||
| "float"
|
||||
| "double"
|
||||
| "password"
|
||||
| "binary"
|
||||
|
||||
export type DefinedFormats = {
|
||||
[key in FormatName]: Format
|
||||
}
|
||||
|
||||
function fmtDef(
|
||||
validate: RegExp | FormatValidator<string>,
|
||||
compare: FormatCompare<string>
|
||||
): FormatDefinition<string> {
|
||||
return {validate, compare}
|
||||
}
|
||||
|
||||
export const fullFormats: DefinedFormats = {
|
||||
// date: http://tools.ietf.org/html/rfc3339#section-5.6
|
||||
date: fmtDef(date, compareDate),
|
||||
// date-time: http://tools.ietf.org/html/rfc3339#section-5.6
|
||||
time: fmtDef(time, compareTime),
|
||||
"date-time": fmtDef(date_time, compareDateTime),
|
||||
// duration: https://tools.ietf.org/html/rfc3339#appendix-A
|
||||
duration: /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/,
|
||||
uri,
|
||||
"uri-reference":
|
||||
/^(?:[a-z][a-z0-9+\-.]*:)?(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0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i,
|
||||
// uri-template: https://tools.ietf.org/html/rfc6570
|
||||
"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,
|
||||
// For the source: https://gist.github.com/dperini/729294
|
||||
// For test cases: https://mathiasbynens.be/demo/url-regex
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
// optimized https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html
|
||||
ipv4: /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/,
|
||||
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,
|
||||
regex,
|
||||
// uuid: http://tools.ietf.org/html/rfc4122
|
||||
uuid: /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
|
||||
// JSON-pointer: https://tools.ietf.org/html/rfc6901
|
||||
// uri fragment: https://tools.ietf.org/html/rfc3986#appendix-A
|
||||
"json-pointer": /^(?:\/(?:[^~/]|~0|~1)*)*$/,
|
||||
"json-pointer-uri-fragment": /^#(?:\/(?:[a-z0-9_\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i,
|
||||
// relative JSON-pointer: http://tools.ietf.org/html/draft-luff-relative-json-pointer-00
|
||||
"relative-json-pointer": /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/,
|
||||
// the following formats are used by the openapi specification: https://spec.openapis.org/oas/v3.0.0#data-types
|
||||
// byte: https://github.com/miguelmota/is-base64
|
||||
byte,
|
||||
// signed 32 bit integer
|
||||
int32: {type: "number", validate: validateInt32},
|
||||
// signed 64 bit integer
|
||||
int64: {type: "number", validate: validateInt64},
|
||||
// C-type float
|
||||
float: {type: "number", validate: validateNumber},
|
||||
// C-type double
|
||||
double: {type: "number", validate: validateNumber},
|
||||
// hint to the UI to hide input strings
|
||||
password: true,
|
||||
// unchecked string payload
|
||||
binary: true,
|
||||
}
|
||||
|
||||
export const fastFormats: DefinedFormats = {
|
||||
...fullFormats,
|
||||
date: fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d$/, compareDate),
|
||||
time: fmtDef(
|
||||
/^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i,
|
||||
compareTime
|
||||
),
|
||||
"date-time": fmtDef(
|
||||
/^\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,
|
||||
compareDateTime
|
||||
),
|
||||
// uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js
|
||||
uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i,
|
||||
"uri-reference": /^(?:(?:[a-z][a-z0-9+\-.]*:)?\/?\/)?(?:[^\\\s#][^\s#]*)?(?:#[^\\\s]*)?$/i,
|
||||
// email (sources from jsen validator):
|
||||
// http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363
|
||||
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'wilful violation')
|
||||
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,
|
||||
}
|
||||
|
||||
export const formatNames = Object.keys(fullFormats) as FormatName[]
|
||||
|
||||
function isLeapYear(year: number): boolean {
|
||||
// https://tools.ietf.org/html/rfc3339#appendix-C
|
||||
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)
|
||||
}
|
||||
|
||||
const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
|
||||
const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
|
||||
function date(str: string): boolean {
|
||||
// full-date from http://tools.ietf.org/html/rfc3339#section-5.6
|
||||
const matches: string[] | null = DATE.exec(str)
|
||||
if (!matches) return false
|
||||
const year: number = +matches[1]
|
||||
const month: number = +matches[2]
|
||||
const day: number = +matches[3]
|
||||
return (
|
||||
month >= 1 &&
|
||||
month <= 12 &&
|
||||
day >= 1 &&
|
||||
day <= (month === 2 && isLeapYear(year) ? 29 : DAYS[month])
|
||||
)
|
||||
}
|
||||
|
||||
function compareDate(d1: string, d2: string): number | undefined {
|
||||
if (!(d1 && d2)) return undefined
|
||||
if (d1 > d2) return 1
|
||||
if (d1 < d2) return -1
|
||||
return 0
|
||||
}
|
||||
|
||||
const TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d(?::?\d\d)?)?$/i
|
||||
|
||||
function time(str: string, withTimeZone?: boolean): boolean {
|
||||
const matches: string[] | null = TIME.exec(str)
|
||||
if (!matches) return false
|
||||
|
||||
const hour: number = +matches[1]
|
||||
const minute: number = +matches[2]
|
||||
const second: number = +matches[3]
|
||||
const timeZone: string = matches[5]
|
||||
return (
|
||||
((hour <= 23 && minute <= 59 && second <= 59) ||
|
||||
(hour === 23 && minute === 59 && second === 60)) &&
|
||||
(!withTimeZone || timeZone !== "")
|
||||
)
|
||||
}
|
||||
|
||||
function compareTime(t1: string, t2: string): number | undefined {
|
||||
if (!(t1 && t2)) return undefined
|
||||
const a1 = TIME.exec(t1)
|
||||
const a2 = TIME.exec(t2)
|
||||
if (!(a1 && a2)) return undefined
|
||||
t1 = a1[1] + a1[2] + a1[3] + (a1[4] || "")
|
||||
t2 = a2[1] + a2[2] + a2[3] + (a2[4] || "")
|
||||
if (t1 > t2) return 1
|
||||
if (t1 < t2) return -1
|
||||
return 0
|
||||
}
|
||||
|
||||
const DATE_TIME_SEPARATOR = /t|\s/i
|
||||
function date_time(str: string): boolean {
|
||||
// http://tools.ietf.org/html/rfc3339#section-5.6
|
||||
const dateTime: string[] = str.split(DATE_TIME_SEPARATOR)
|
||||
return dateTime.length === 2 && date(dateTime[0]) && time(dateTime[1], true)
|
||||
}
|
||||
|
||||
function compareDateTime(dt1: string, dt2: string): number | undefined {
|
||||
if (!(dt1 && dt2)) return undefined
|
||||
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR)
|
||||
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR)
|
||||
const res = compareDate(d1, d2)
|
||||
if (res === undefined) return undefined
|
||||
return res || compareTime(t1, t2)
|
||||
}
|
||||
|
||||
const NOT_URI_FRAGMENT = /\/|:/
|
||||
const URI =
|
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-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i
|
||||
|
||||
function uri(str: string): boolean {
|
||||
// http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required "."
|
||||
return NOT_URI_FRAGMENT.test(str) && URI.test(str)
|
||||
}
|
||||
|
||||
const BYTE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm
|
||||
|
||||
function byte(str: string): boolean {
|
||||
BYTE.lastIndex = 0
|
||||
return BYTE.test(str)
|
||||
}
|
||||
|
||||
const MIN_INT32 = -(2 ** 31)
|
||||
const MAX_INT32 = 2 ** 31 - 1
|
||||
|
||||
function validateInt32(value: number): boolean {
|
||||
return Number.isInteger(value) && value <= MAX_INT32 && value >= MIN_INT32
|
||||
}
|
||||
|
||||
function validateInt64(value: number): boolean {
|
||||
// JSON and javascript max Int is 2**53, so any int that passes isInteger is valid for Int64
|
||||
return Number.isInteger(value)
|
||||
}
|
||||
|
||||
function validateNumber(): boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
const Z_ANCHOR = /[^\\]\\Z/
|
||||
function regex(str: string): boolean {
|
||||
if (Z_ANCHOR.test(str)) return false
|
||||
try {
|
||||
new RegExp(str)
|
||||
return true
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
62
my-app/node_modules/ajv-formats/src/index.ts
generated
vendored
Executable file
62
my-app/node_modules/ajv-formats/src/index.ts
generated
vendored
Executable file
|
@ -0,0 +1,62 @@
|
|||
import {
|
||||
DefinedFormats,
|
||||
FormatMode,
|
||||
FormatName,
|
||||
formatNames,
|
||||
fastFormats,
|
||||
fullFormats,
|
||||
} from "./formats"
|
||||
import formatLimit from "./limit"
|
||||
import type Ajv from "ajv"
|
||||
import type {Plugin, Format} from "ajv"
|
||||
import {_, Name} from "ajv/dist/compile/codegen"
|
||||
|
||||
export {FormatMode, FormatName} from "./formats"
|
||||
export {LimitFormatError} from "./limit"
|
||||
export interface FormatOptions {
|
||||
mode?: FormatMode
|
||||
formats?: FormatName[]
|
||||
keywords?: boolean
|
||||
}
|
||||
|
||||
export type FormatsPluginOptions = FormatName[] | FormatOptions
|
||||
|
||||
export interface FormatsPlugin extends Plugin<FormatsPluginOptions> {
|
||||
get: (format: FormatName, mode?: FormatMode) => Format
|
||||
}
|
||||
|
||||
const fullName = new Name("fullFormats")
|
||||
const fastName = new Name("fastFormats")
|
||||
|
||||
const formatsPlugin: FormatsPlugin = (
|
||||
ajv: Ajv,
|
||||
opts: FormatsPluginOptions = {keywords: true}
|
||||
): Ajv => {
|
||||
if (Array.isArray(opts)) {
|
||||
addFormats(ajv, opts, fullFormats, fullName)
|
||||
return ajv
|
||||
}
|
||||
const [formats, exportName] =
|
||||
opts.mode === "fast" ? [fastFormats, fastName] : [fullFormats, fullName]
|
||||
const list = opts.formats || formatNames
|
||||
addFormats(ajv, list, formats, exportName)
|
||||
if (opts.keywords) formatLimit(ajv)
|
||||
return ajv
|
||||
}
|
||||
|
||||
formatsPlugin.get = (name: FormatName, mode: FormatMode = "full"): Format => {
|
||||
const formats = mode === "fast" ? fastFormats : fullFormats
|
||||
const f = formats[name]
|
||||
if (!f) throw new Error(`Unknown format "${name}"`)
|
||||
return f
|
||||
}
|
||||
|
||||
function addFormats(ajv: Ajv, list: FormatName[], fs: DefinedFormats, exportName: Name): void {
|
||||
ajv.opts.code.formats ??= _`require("ajv-formats/dist/formats").${exportName}`
|
||||
for (const f of list) ajv.addFormat(f, fs[f])
|
||||
}
|
||||
|
||||
module.exports = exports = formatsPlugin
|
||||
Object.defineProperty(exports, "__esModule", {value: true})
|
||||
|
||||
export default formatsPlugin
|
99
my-app/node_modules/ajv-formats/src/limit.ts
generated
vendored
Executable file
99
my-app/node_modules/ajv-formats/src/limit.ts
generated
vendored
Executable file
|
@ -0,0 +1,99 @@
|
|||
import type Ajv from "ajv"
|
||||
import type {
|
||||
Plugin,
|
||||
CodeKeywordDefinition,
|
||||
KeywordErrorDefinition,
|
||||
Code,
|
||||
Name,
|
||||
ErrorObject,
|
||||
} from "ajv"
|
||||
import type {AddedFormat} from "ajv/dist/types"
|
||||
import type {Rule} from "ajv/dist/compile/rules"
|
||||
import {KeywordCxt} from "ajv"
|
||||
import {_, str, or, getProperty, operators} from "ajv/dist/compile/codegen"
|
||||
|
||||
type Kwd = "formatMaximum" | "formatMinimum" | "formatExclusiveMaximum" | "formatExclusiveMinimum"
|
||||
|
||||
type Comparison = "<=" | ">=" | "<" | ">"
|
||||
|
||||
const ops = operators
|
||||
|
||||
const KWDs: {[K in Kwd]: {okStr: Comparison; ok: Code; fail: Code}} = {
|
||||
formatMaximum: {okStr: "<=", ok: ops.LTE, fail: ops.GT},
|
||||
formatMinimum: {okStr: ">=", ok: ops.GTE, fail: ops.LT},
|
||||
formatExclusiveMaximum: {okStr: "<", ok: ops.LT, fail: ops.GTE},
|
||||
formatExclusiveMinimum: {okStr: ">", ok: ops.GT, fail: ops.LTE},
|
||||
}
|
||||
|
||||
export type LimitFormatError = ErrorObject<Kwd, {limit: string; comparison: Comparison}>
|
||||
|
||||
const error: KeywordErrorDefinition = {
|
||||
message: ({keyword, schemaCode}) => str`should be ${KWDs[keyword as Kwd].okStr} ${schemaCode}`,
|
||||
params: ({keyword, schemaCode}) =>
|
||||
_`{comparison: ${KWDs[keyword as Kwd].okStr}, limit: ${schemaCode}}`,
|
||||
}
|
||||
|
||||
export const formatLimitDefinition: CodeKeywordDefinition = {
|
||||
keyword: Object.keys(KWDs),
|
||||
type: "string",
|
||||
schemaType: "string",
|
||||
$data: true,
|
||||
error,
|
||||
code(cxt) {
|
||||
const {gen, data, schemaCode, keyword, it} = cxt
|
||||
const {opts, self} = it
|
||||
if (!opts.validateFormats) return
|
||||
|
||||
const fCxt = new KeywordCxt(it, (self.RULES.all.format as Rule).definition, "format")
|
||||
if (fCxt.$data) validate$DataFormat()
|
||||
else validateFormat()
|
||||
|
||||
function validate$DataFormat(): void {
|
||||
const fmts = gen.scopeValue("formats", {
|
||||
ref: self.formats,
|
||||
code: opts.code.formats,
|
||||
})
|
||||
const fmt = gen.const("fmt", _`${fmts}[${fCxt.schemaCode}]`)
|
||||
cxt.fail$data(
|
||||
or(
|
||||
_`typeof ${fmt} != "object"`,
|
||||
_`${fmt} instanceof RegExp`,
|
||||
_`typeof ${fmt}.compare != "function"`,
|
||||
compareCode(fmt)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
function validateFormat(): void {
|
||||
const format = fCxt.schema as string
|
||||
const fmtDef: AddedFormat | undefined = self.formats[format]
|
||||
if (!fmtDef || fmtDef === true) return
|
||||
if (
|
||||
typeof fmtDef != "object" ||
|
||||
fmtDef instanceof RegExp ||
|
||||
typeof fmtDef.compare != "function"
|
||||
) {
|
||||
throw new Error(`"${keyword}": format "${format}" does not define "compare" function`)
|
||||
}
|
||||
const fmt = gen.scopeValue("formats", {
|
||||
key: format,
|
||||
ref: fmtDef,
|
||||
code: opts.code.formats ? _`${opts.code.formats}${getProperty(format)}` : undefined,
|
||||
})
|
||||
|
||||
cxt.fail$data(compareCode(fmt))
|
||||
}
|
||||
|
||||
function compareCode(fmt: Name): Code {
|
||||
return _`${fmt}.compare(${data}, ${schemaCode}) ${KWDs[keyword as Kwd].fail} 0`
|
||||
}
|
||||
},
|
||||
dependencies: ["format"],
|
||||
}
|
||||
|
||||
const formatLimitPlugin: Plugin<undefined> = (ajv: Ajv): Ajv => {
|
||||
ajv.addKeyword(formatLimitDefinition)
|
||||
return ajv
|
||||
}
|
||||
|
||||
export default formatLimitPlugin
|
Loading…
Add table
Add a link
Reference in a new issue