Updated the files.
This commit is contained in:
parent
1553e6b971
commit
753967d4f5
23418 changed files with 3784666 additions and 0 deletions
30
my-app/node_modules/ajv-keywords/src/definitions/_range.ts
generated
vendored
Executable file
30
my-app/node_modules/ajv-keywords/src/definitions/_range.ts
generated
vendored
Executable file
|
@ -0,0 +1,30 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
import type {GetDefinition} from "./_types"
|
||||
|
||||
type RangeKwd = "range" | "exclusiveRange"
|
||||
|
||||
export default function getRangeDef(keyword: RangeKwd): GetDefinition<MacroKeywordDefinition> {
|
||||
return () => ({
|
||||
keyword,
|
||||
type: "number",
|
||||
schemaType: "array",
|
||||
macro: function ([min, max]: [number, number]) {
|
||||
validateRangeSchema(min, max)
|
||||
return keyword === "range"
|
||||
? {minimum: min, maximum: max}
|
||||
: {exclusiveMinimum: min, exclusiveMaximum: max}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "array",
|
||||
minItems: 2,
|
||||
maxItems: 2,
|
||||
items: {type: "number"},
|
||||
},
|
||||
})
|
||||
|
||||
function validateRangeSchema(min: number, max: number): void {
|
||||
if (min > max || (keyword === "exclusiveRange" && min === max)) {
|
||||
throw new Error("There are no numbers in range")
|
||||
}
|
||||
}
|
||||
}
|
24
my-app/node_modules/ajv-keywords/src/definitions/_required.ts
generated
vendored
Executable file
24
my-app/node_modules/ajv-keywords/src/definitions/_required.ts
generated
vendored
Executable file
|
@ -0,0 +1,24 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
import type {GetDefinition} from "./_types"
|
||||
|
||||
type RequiredKwd = "anyRequired" | "oneRequired"
|
||||
|
||||
export default function getRequiredDef(
|
||||
keyword: RequiredKwd
|
||||
): GetDefinition<MacroKeywordDefinition> {
|
||||
return () => ({
|
||||
keyword,
|
||||
type: "object",
|
||||
schemaType: "array",
|
||||
macro(schema: string[]) {
|
||||
if (schema.length === 0) return true
|
||||
if (schema.length === 1) return {required: schema}
|
||||
const comb = keyword === "anyRequired" ? "anyOf" : "oneOf"
|
||||
return {[comb]: schema.map((p) => ({required: [p]}))}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "array",
|
||||
items: {type: "string"},
|
||||
},
|
||||
})
|
||||
}
|
7
my-app/node_modules/ajv-keywords/src/definitions/_types.ts
generated
vendored
Executable file
7
my-app/node_modules/ajv-keywords/src/definitions/_types.ts
generated
vendored
Executable file
|
@ -0,0 +1,7 @@
|
|||
import type {KeywordDefinition} from "ajv"
|
||||
|
||||
export interface DefinitionOptions {
|
||||
defaultMeta?: string | boolean
|
||||
}
|
||||
|
||||
export type GetDefinition<T extends KeywordDefinition> = (opts?: DefinitionOptions) => T
|
22
my-app/node_modules/ajv-keywords/src/definitions/_util.ts
generated
vendored
Executable file
22
my-app/node_modules/ajv-keywords/src/definitions/_util.ts
generated
vendored
Executable file
|
@ -0,0 +1,22 @@
|
|||
import type {DefinitionOptions} from "./_types"
|
||||
import type {SchemaObject, KeywordCxt, Name} from "ajv"
|
||||
import {_} from "ajv/dist/compile/codegen"
|
||||
|
||||
const META_SCHEMA_ID = "http://json-schema.org/schema"
|
||||
|
||||
export function metaSchemaRef({defaultMeta}: DefinitionOptions = {}): SchemaObject {
|
||||
return defaultMeta === false ? {} : {$ref: defaultMeta || META_SCHEMA_ID}
|
||||
}
|
||||
|
||||
export function usePattern(
|
||||
{gen, it: {opts}}: KeywordCxt,
|
||||
pattern: string,
|
||||
flags = opts.unicodeRegExp ? "u" : ""
|
||||
): Name {
|
||||
const rx = new RegExp(pattern, flags)
|
||||
return gen.scopeValue("pattern", {
|
||||
key: rx.toString(),
|
||||
ref: rx,
|
||||
code: _`new RegExp(${pattern}, ${flags})`,
|
||||
})
|
||||
}
|
18
my-app/node_modules/ajv-keywords/src/definitions/allRequired.ts
generated
vendored
Executable file
18
my-app/node_modules/ajv-keywords/src/definitions/allRequired.ts
generated
vendored
Executable file
|
@ -0,0 +1,18 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
|
||||
export default function getDef(): MacroKeywordDefinition {
|
||||
return {
|
||||
keyword: "allRequired",
|
||||
type: "object",
|
||||
schemaType: "boolean",
|
||||
macro(schema: boolean, parentSchema) {
|
||||
if (!schema) return true
|
||||
const required = Object.keys(parentSchema.properties)
|
||||
if (required.length === 0) return true
|
||||
return {required}
|
||||
},
|
||||
dependencies: ["properties"],
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getDef
|
8
my-app/node_modules/ajv-keywords/src/definitions/anyRequired.ts
generated
vendored
Executable file
8
my-app/node_modules/ajv-keywords/src/definitions/anyRequired.ts
generated
vendored
Executable file
|
@ -0,0 +1,8 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
import type {GetDefinition} from "./_types"
|
||||
import getRequiredDef from "./_required"
|
||||
|
||||
const getDef: GetDefinition<MacroKeywordDefinition> = getRequiredDef("anyRequired")
|
||||
|
||||
export default getDef
|
||||
module.exports = getDef
|
52
my-app/node_modules/ajv-keywords/src/definitions/deepProperties.ts
generated
vendored
Executable file
52
my-app/node_modules/ajv-keywords/src/definitions/deepProperties.ts
generated
vendored
Executable file
|
@ -0,0 +1,52 @@
|
|||
import type {MacroKeywordDefinition, SchemaObject, Schema} from "ajv"
|
||||
import type {DefinitionOptions} from "./_types"
|
||||
import {metaSchemaRef} from "./_util"
|
||||
|
||||
export default function getDef(opts?: DefinitionOptions): MacroKeywordDefinition {
|
||||
return {
|
||||
keyword: "deepProperties",
|
||||
type: "object",
|
||||
schemaType: "object",
|
||||
macro: function (schema: Record<string, SchemaObject>) {
|
||||
const allOf = []
|
||||
for (const pointer in schema) allOf.push(getSchema(pointer, schema[pointer]))
|
||||
return {allOf}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "object",
|
||||
propertyNames: {type: "string", format: "json-pointer"},
|
||||
additionalProperties: metaSchemaRef(opts),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function getSchema(jsonPointer: string, schema: SchemaObject): SchemaObject {
|
||||
const segments = jsonPointer.split("/")
|
||||
const rootSchema: SchemaObject = {}
|
||||
let pointerSchema: SchemaObject = rootSchema
|
||||
for (let i = 1; i < segments.length; i++) {
|
||||
let segment: string = segments[i]
|
||||
const isLast = i === segments.length - 1
|
||||
segment = unescapeJsonPointer(segment)
|
||||
const properties: Record<string, Schema> = (pointerSchema.properties = {})
|
||||
let items: SchemaObject[] | undefined
|
||||
if (/[0-9]+/.test(segment)) {
|
||||
let count = +segment
|
||||
items = pointerSchema.items = []
|
||||
pointerSchema.type = ["object", "array"]
|
||||
while (count--) items.push({})
|
||||
} else {
|
||||
pointerSchema.type = "object"
|
||||
}
|
||||
pointerSchema = isLast ? schema : {}
|
||||
properties[segment] = pointerSchema
|
||||
if (items) items.push(pointerSchema)
|
||||
}
|
||||
return rootSchema
|
||||
}
|
||||
|
||||
function unescapeJsonPointer(str: string): string {
|
||||
return str.replace(/~1/g, "/").replace(/~0/g, "~")
|
||||
}
|
||||
|
||||
module.exports = getDef
|
35
my-app/node_modules/ajv-keywords/src/definitions/deepRequired.ts
generated
vendored
Executable file
35
my-app/node_modules/ajv-keywords/src/definitions/deepRequired.ts
generated
vendored
Executable file
|
@ -0,0 +1,35 @@
|
|||
import type {CodeKeywordDefinition, KeywordCxt} from "ajv"
|
||||
import {_, or, and, getProperty, Code} from "ajv/dist/compile/codegen"
|
||||
|
||||
export default function getDef(): CodeKeywordDefinition {
|
||||
return {
|
||||
keyword: "deepRequired",
|
||||
type: "object",
|
||||
schemaType: "array",
|
||||
code(ctx: KeywordCxt) {
|
||||
const {schema, data} = ctx
|
||||
const props = (schema as string[]).map((jp: string) => _`(${getData(jp)}) === undefined`)
|
||||
ctx.fail(or(...props))
|
||||
|
||||
function getData(jsonPointer: string): Code {
|
||||
if (jsonPointer === "") throw new Error("empty JSON pointer not allowed")
|
||||
const segments = jsonPointer.split("/")
|
||||
let x: Code = data
|
||||
const xs = segments.map((s, i) =>
|
||||
i ? (x = _`${x}${getProperty(unescapeJPSegment(s))}`) : x
|
||||
)
|
||||
return and(...xs)
|
||||
}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "array",
|
||||
items: {type: "string", format: "json-pointer"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function unescapeJPSegment(s: string): string {
|
||||
return s.replace(/~1/g, "/").replace(/~0/g, "~")
|
||||
}
|
||||
|
||||
module.exports = getDef
|
98
my-app/node_modules/ajv-keywords/src/definitions/dynamicDefaults.ts
generated
vendored
Executable file
98
my-app/node_modules/ajv-keywords/src/definitions/dynamicDefaults.ts
generated
vendored
Executable file
|
@ -0,0 +1,98 @@
|
|||
import type {FuncKeywordDefinition, SchemaCxt} from "ajv"
|
||||
|
||||
const sequences: Record<string, number | undefined> = {}
|
||||
|
||||
export type DynamicDefaultFunc = (args?: Record<string, any>) => () => any
|
||||
|
||||
const DEFAULTS: Record<string, DynamicDefaultFunc | undefined> = {
|
||||
timestamp: () => () => Date.now(),
|
||||
datetime: () => () => new Date().toISOString(),
|
||||
date: () => () => new Date().toISOString().slice(0, 10),
|
||||
time: () => () => new Date().toISOString().slice(11),
|
||||
random: () => () => Math.random(),
|
||||
randomint: (args?: {max?: number}) => {
|
||||
const max = args?.max ?? 2
|
||||
return () => Math.floor(Math.random() * max)
|
||||
},
|
||||
seq: (args?: {name?: string}) => {
|
||||
const name = args?.name ?? ""
|
||||
sequences[name] ||= 0
|
||||
return () => (sequences[name] as number)++
|
||||
},
|
||||
}
|
||||
|
||||
interface PropertyDefaultSchema {
|
||||
func: string
|
||||
args: Record<string, any>
|
||||
}
|
||||
|
||||
type DefaultSchema = Record<string, string | PropertyDefaultSchema | undefined>
|
||||
|
||||
const getDef: (() => FuncKeywordDefinition) & {
|
||||
DEFAULTS: typeof DEFAULTS
|
||||
} = Object.assign(_getDef, {DEFAULTS})
|
||||
|
||||
function _getDef(): FuncKeywordDefinition {
|
||||
return {
|
||||
keyword: "dynamicDefaults",
|
||||
type: "object",
|
||||
schemaType: ["string", "object"],
|
||||
modifying: true,
|
||||
valid: true,
|
||||
compile(schema: DefaultSchema, _parentSchema, it: SchemaCxt) {
|
||||
if (!it.opts.useDefaults || it.compositeRule) return () => true
|
||||
const fs: Record<string, () => any> = {}
|
||||
for (const key in schema) fs[key] = getDefault(schema[key])
|
||||
const empty = it.opts.useDefaults === "empty"
|
||||
|
||||
return (data: Record<string, any>) => {
|
||||
for (const prop in schema) {
|
||||
if (data[prop] === undefined || (empty && (data[prop] === null || data[prop] === ""))) {
|
||||
data[prop] = fs[prop]()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "object",
|
||||
additionalProperties: {
|
||||
anyOf: [
|
||||
{type: "string"},
|
||||
{
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["func", "args"],
|
||||
properties: {
|
||||
func: {type: "string"},
|
||||
args: {type: "object"},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function getDefault(d: string | PropertyDefaultSchema | undefined): () => any {
|
||||
return typeof d == "object" ? getObjDefault(d) : getStrDefault(d)
|
||||
}
|
||||
|
||||
function getObjDefault({func, args}: PropertyDefaultSchema): () => any {
|
||||
const def = DEFAULTS[func]
|
||||
assertDefined(func, def)
|
||||
return def(args)
|
||||
}
|
||||
|
||||
function getStrDefault(d = ""): () => any {
|
||||
const def = DEFAULTS[d]
|
||||
assertDefined(d, def)
|
||||
return def()
|
||||
}
|
||||
|
||||
function assertDefined(name: string, def?: DynamicDefaultFunc): asserts def is DynamicDefaultFunc {
|
||||
if (!def) throw new Error(`invalid "dynamicDefaults" keyword property value: ${name}`)
|
||||
}
|
||||
|
||||
export default getDef
|
||||
module.exports = getDef
|
8
my-app/node_modules/ajv-keywords/src/definitions/exclusiveRange.ts
generated
vendored
Executable file
8
my-app/node_modules/ajv-keywords/src/definitions/exclusiveRange.ts
generated
vendored
Executable file
|
@ -0,0 +1,8 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
import type {GetDefinition} from "./_types"
|
||||
import getRangeDef from "./_range"
|
||||
|
||||
const getDef: GetDefinition<MacroKeywordDefinition> = getRangeDef("exclusiveRange")
|
||||
|
||||
export default getDef
|
||||
module.exports = getDef
|
61
my-app/node_modules/ajv-keywords/src/definitions/index.ts
generated
vendored
Executable file
61
my-app/node_modules/ajv-keywords/src/definitions/index.ts
generated
vendored
Executable file
|
@ -0,0 +1,61 @@
|
|||
import type {Vocabulary, KeywordDefinition, ErrorNoParams} from "ajv"
|
||||
import type {DefinitionOptions, GetDefinition} from "./_types"
|
||||
import typeofDef from "./typeof"
|
||||
import instanceofDef from "./instanceof"
|
||||
import range from "./range"
|
||||
import exclusiveRange from "./exclusiveRange"
|
||||
import regexp from "./regexp"
|
||||
import transform from "./transform"
|
||||
import uniqueItemProperties from "./uniqueItemProperties"
|
||||
import allRequired from "./allRequired"
|
||||
import anyRequired from "./anyRequired"
|
||||
import oneRequired from "./oneRequired"
|
||||
import patternRequired, {PatternRequiredError} from "./patternRequired"
|
||||
import prohibited from "./prohibited"
|
||||
import deepProperties from "./deepProperties"
|
||||
import deepRequired from "./deepRequired"
|
||||
import dynamicDefaults from "./dynamicDefaults"
|
||||
import selectDef, {SelectError} from "./select"
|
||||
|
||||
const definitions: GetDefinition<KeywordDefinition>[] = [
|
||||
typeofDef,
|
||||
instanceofDef,
|
||||
range,
|
||||
exclusiveRange,
|
||||
regexp,
|
||||
transform,
|
||||
uniqueItemProperties,
|
||||
allRequired,
|
||||
anyRequired,
|
||||
oneRequired,
|
||||
patternRequired,
|
||||
prohibited,
|
||||
deepProperties,
|
||||
deepRequired,
|
||||
dynamicDefaults,
|
||||
]
|
||||
|
||||
export default function ajvKeywords(opts?: DefinitionOptions): Vocabulary {
|
||||
return definitions.map((d) => d(opts)).concat(selectDef(opts))
|
||||
}
|
||||
|
||||
export type AjvKeywordsError =
|
||||
| PatternRequiredError
|
||||
| SelectError
|
||||
| ErrorNoParams<
|
||||
| "range"
|
||||
| "exclusiveRange"
|
||||
| "anyRequired"
|
||||
| "oneRequired"
|
||||
| "allRequired"
|
||||
| "deepProperties"
|
||||
| "deepRequired"
|
||||
| "dynamicDefaults"
|
||||
| "instanceof"
|
||||
| "prohibited"
|
||||
| "regexp"
|
||||
| "transform"
|
||||
| "uniqueItemProperties"
|
||||
>
|
||||
|
||||
module.exports = ajvKeywords
|
61
my-app/node_modules/ajv-keywords/src/definitions/instanceof.ts
generated
vendored
Executable file
61
my-app/node_modules/ajv-keywords/src/definitions/instanceof.ts
generated
vendored
Executable file
|
@ -0,0 +1,61 @@
|
|||
import type {FuncKeywordDefinition} from "ajv"
|
||||
|
||||
type Constructor = new (...args: any[]) => any
|
||||
|
||||
const CONSTRUCTORS: Record<string, Constructor | undefined> = {
|
||||
Object,
|
||||
Array,
|
||||
Function,
|
||||
Number,
|
||||
String,
|
||||
Date,
|
||||
RegExp,
|
||||
}
|
||||
|
||||
/* istanbul ignore else */
|
||||
if (typeof Buffer != "undefined") CONSTRUCTORS.Buffer = Buffer
|
||||
|
||||
/* istanbul ignore else */
|
||||
if (typeof Promise != "undefined") CONSTRUCTORS.Promise = Promise
|
||||
|
||||
const getDef: (() => FuncKeywordDefinition) & {
|
||||
CONSTRUCTORS: typeof CONSTRUCTORS
|
||||
} = Object.assign(_getDef, {CONSTRUCTORS})
|
||||
|
||||
function _getDef(): FuncKeywordDefinition {
|
||||
return {
|
||||
keyword: "instanceof",
|
||||
schemaType: ["string", "array"],
|
||||
compile(schema: string | string[]) {
|
||||
if (typeof schema == "string") {
|
||||
const C = getConstructor(schema)
|
||||
return (data) => data instanceof C
|
||||
}
|
||||
|
||||
if (Array.isArray(schema)) {
|
||||
const constructors = schema.map(getConstructor)
|
||||
return (data) => {
|
||||
for (const C of constructors) {
|
||||
if (data instanceof C) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
throw new Error("ajv implementation error")
|
||||
},
|
||||
metaSchema: {
|
||||
anyOf: [{type: "string"}, {type: "array", items: {type: "string"}}],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function getConstructor(c: string): Constructor {
|
||||
const C = CONSTRUCTORS[c]
|
||||
if (C) return C
|
||||
throw new Error(`invalid "instanceof" keyword value ${c}`)
|
||||
}
|
||||
|
||||
export default getDef
|
||||
module.exports = getDef
|
8
my-app/node_modules/ajv-keywords/src/definitions/oneRequired.ts
generated
vendored
Executable file
8
my-app/node_modules/ajv-keywords/src/definitions/oneRequired.ts
generated
vendored
Executable file
|
@ -0,0 +1,8 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
import type {GetDefinition} from "./_types"
|
||||
import getRequiredDef from "./_required"
|
||||
|
||||
const getDef: GetDefinition<MacroKeywordDefinition> = getRequiredDef("oneRequired")
|
||||
|
||||
export default getDef
|
||||
module.exports = getDef
|
46
my-app/node_modules/ajv-keywords/src/definitions/patternRequired.ts
generated
vendored
Executable file
46
my-app/node_modules/ajv-keywords/src/definitions/patternRequired.ts
generated
vendored
Executable file
|
@ -0,0 +1,46 @@
|
|||
import type {CodeKeywordDefinition, KeywordCxt, KeywordErrorDefinition, ErrorObject} from "ajv"
|
||||
import {_, str, and} from "ajv/dist/compile/codegen"
|
||||
import {usePattern} from "./_util"
|
||||
|
||||
export type PatternRequiredError = ErrorObject<"patternRequired", {missingPattern: string}>
|
||||
|
||||
const error: KeywordErrorDefinition = {
|
||||
message: ({params: {missingPattern}}) =>
|
||||
str`should have property matching pattern '${missingPattern}'`,
|
||||
params: ({params: {missingPattern}}) => _`{missingPattern: ${missingPattern}}`,
|
||||
}
|
||||
|
||||
export default function getDef(): CodeKeywordDefinition {
|
||||
return {
|
||||
keyword: "patternRequired",
|
||||
type: "object",
|
||||
schemaType: "array",
|
||||
error,
|
||||
code(cxt: KeywordCxt) {
|
||||
const {gen, schema, data} = cxt
|
||||
if (schema.length === 0) return
|
||||
const valid = gen.let("valid", true)
|
||||
for (const pat of schema) validateProperties(pat)
|
||||
|
||||
function validateProperties(pattern: string): void {
|
||||
const matched = gen.let("matched", false)
|
||||
|
||||
gen.forIn("key", data, (key) => {
|
||||
gen.assign(matched, _`${usePattern(cxt, pattern)}.test(${key})`)
|
||||
gen.if(matched, () => gen.break())
|
||||
})
|
||||
|
||||
cxt.setParams({missingPattern: pattern})
|
||||
gen.assign(valid, and(valid, matched))
|
||||
cxt.pass(valid)
|
||||
}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "array",
|
||||
items: {type: "string", format: "regex"},
|
||||
uniqueItems: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getDef
|
20
my-app/node_modules/ajv-keywords/src/definitions/prohibited.ts
generated
vendored
Executable file
20
my-app/node_modules/ajv-keywords/src/definitions/prohibited.ts
generated
vendored
Executable file
|
@ -0,0 +1,20 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
|
||||
export default function getDef(): MacroKeywordDefinition {
|
||||
return {
|
||||
keyword: "prohibited",
|
||||
type: "object",
|
||||
schemaType: "array",
|
||||
macro: function (schema: string[]) {
|
||||
if (schema.length === 0) return true
|
||||
if (schema.length === 1) return {not: {required: schema}}
|
||||
return {not: {anyOf: schema.map((p) => ({required: [p]}))}}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "array",
|
||||
items: {type: "string"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getDef
|
8
my-app/node_modules/ajv-keywords/src/definitions/range.ts
generated
vendored
Executable file
8
my-app/node_modules/ajv-keywords/src/definitions/range.ts
generated
vendored
Executable file
|
@ -0,0 +1,8 @@
|
|||
import type {MacroKeywordDefinition} from "ajv"
|
||||
import type {GetDefinition} from "./_types"
|
||||
import getRangeDef from "./_range"
|
||||
|
||||
const getDef: GetDefinition<MacroKeywordDefinition> = getRangeDef("range")
|
||||
|
||||
export default getDef
|
||||
module.exports = getDef
|
45
my-app/node_modules/ajv-keywords/src/definitions/regexp.ts
generated
vendored
Executable file
45
my-app/node_modules/ajv-keywords/src/definitions/regexp.ts
generated
vendored
Executable file
|
@ -0,0 +1,45 @@
|
|||
import type {CodeKeywordDefinition, KeywordCxt, JSONSchemaType, Name} from "ajv"
|
||||
import {_} from "ajv/dist/compile/codegen"
|
||||
import {usePattern} from "./_util"
|
||||
|
||||
interface RegexpSchema {
|
||||
pattern: string
|
||||
flags?: string
|
||||
}
|
||||
|
||||
const regexpMetaSchema: JSONSchemaType<RegexpSchema> = {
|
||||
type: "object",
|
||||
properties: {
|
||||
pattern: {type: "string"},
|
||||
flags: {type: "string", nullable: true},
|
||||
},
|
||||
required: ["pattern"],
|
||||
additionalProperties: false,
|
||||
}
|
||||
|
||||
const metaRegexp = /^\/(.*)\/([gimuy]*)$/
|
||||
|
||||
export default function getDef(): CodeKeywordDefinition {
|
||||
return {
|
||||
keyword: "regexp",
|
||||
type: "string",
|
||||
schemaType: ["string", "object"],
|
||||
code(cxt: KeywordCxt) {
|
||||
const {data, schema} = cxt
|
||||
const regx = getRegExp(schema)
|
||||
cxt.pass(_`${regx}.test(${data})`)
|
||||
|
||||
function getRegExp(sch: string | RegexpSchema): Name {
|
||||
if (typeof sch == "object") return usePattern(cxt, sch.pattern, sch.flags)
|
||||
const rx = metaRegexp.exec(sch)
|
||||
if (rx) return usePattern(cxt, rx[1], rx[2])
|
||||
throw new Error("cannot parse string into RegExp")
|
||||
}
|
||||
},
|
||||
metaSchema: {
|
||||
anyOf: [{type: "string"}, regexpMetaSchema],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getDef
|
69
my-app/node_modules/ajv-keywords/src/definitions/select.ts
generated
vendored
Executable file
69
my-app/node_modules/ajv-keywords/src/definitions/select.ts
generated
vendored
Executable file
|
@ -0,0 +1,69 @@
|
|||
import type {KeywordDefinition, KeywordErrorDefinition, KeywordCxt, ErrorObject} from "ajv"
|
||||
import {_, str, nil, Name} from "ajv/dist/compile/codegen"
|
||||
import type {DefinitionOptions} from "./_types"
|
||||
import {metaSchemaRef} from "./_util"
|
||||
|
||||
export type SelectError = ErrorObject<"select", {failingCase?: string; failingDefault?: true}>
|
||||
|
||||
const error: KeywordErrorDefinition = {
|
||||
message: ({params: {schemaProp}}) =>
|
||||
schemaProp
|
||||
? str`should match case "${schemaProp}" schema`
|
||||
: str`should match default case schema`,
|
||||
params: ({params: {schemaProp}}) =>
|
||||
schemaProp ? _`{failingCase: ${schemaProp}}` : _`{failingDefault: true}`,
|
||||
}
|
||||
|
||||
export default function getDef(opts?: DefinitionOptions): KeywordDefinition[] {
|
||||
const metaSchema = metaSchemaRef(opts)
|
||||
|
||||
return [
|
||||
{
|
||||
keyword: "select",
|
||||
schemaType: ["string", "number", "boolean", "null"],
|
||||
$data: true,
|
||||
error,
|
||||
dependencies: ["selectCases"],
|
||||
code(cxt: KeywordCxt) {
|
||||
const {gen, schemaCode, parentSchema} = cxt
|
||||
cxt.block$data(nil, () => {
|
||||
const valid = gen.let("valid", true)
|
||||
const schValid = gen.name("_valid")
|
||||
const value = gen.const("value", _`${schemaCode} === null ? "null" : ${schemaCode}`)
|
||||
gen.if(false) // optimizer should remove it from generated code
|
||||
for (const schemaProp in parentSchema.selectCases) {
|
||||
cxt.setParams({schemaProp})
|
||||
gen.elseIf(_`"" + ${value} == ${schemaProp}`) // intentional ==, to match numbers and booleans
|
||||
const schCxt = cxt.subschema({keyword: "selectCases", schemaProp}, schValid)
|
||||
cxt.mergeEvaluated(schCxt, Name)
|
||||
gen.assign(valid, schValid)
|
||||
}
|
||||
gen.else()
|
||||
if (parentSchema.selectDefault !== undefined) {
|
||||
cxt.setParams({schemaProp: undefined})
|
||||
const schCxt = cxt.subschema({keyword: "selectDefault"}, schValid)
|
||||
cxt.mergeEvaluated(schCxt, Name)
|
||||
gen.assign(valid, schValid)
|
||||
}
|
||||
gen.endIf()
|
||||
cxt.pass(valid)
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
keyword: "selectCases",
|
||||
dependencies: ["select"],
|
||||
metaSchema: {
|
||||
type: "object",
|
||||
additionalProperties: metaSchema,
|
||||
},
|
||||
},
|
||||
{
|
||||
keyword: "selectDefault",
|
||||
dependencies: ["select", "selectCases"],
|
||||
metaSchema,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
module.exports = getDef
|
98
my-app/node_modules/ajv-keywords/src/definitions/transform.ts
generated
vendored
Executable file
98
my-app/node_modules/ajv-keywords/src/definitions/transform.ts
generated
vendored
Executable file
|
@ -0,0 +1,98 @@
|
|||
import type {CodeKeywordDefinition, AnySchemaObject, KeywordCxt, Code, Name} from "ajv"
|
||||
import {_, stringify, getProperty} from "ajv/dist/compile/codegen"
|
||||
|
||||
type TransformName =
|
||||
| "trimStart"
|
||||
| "trimEnd"
|
||||
| "trimLeft"
|
||||
| "trimRight"
|
||||
| "trim"
|
||||
| "toLowerCase"
|
||||
| "toUpperCase"
|
||||
| "toEnumCase"
|
||||
|
||||
interface TransformConfig {
|
||||
hash: Record<string, string | undefined>
|
||||
}
|
||||
|
||||
type Transform = (s: string, cfg?: TransformConfig) => string
|
||||
|
||||
const transform: {[key in TransformName]: Transform} = {
|
||||
trimStart: (s) => s.trimStart(),
|
||||
trimEnd: (s) => s.trimEnd(),
|
||||
trimLeft: (s) => s.trimStart(),
|
||||
trimRight: (s) => s.trimEnd(),
|
||||
trim: (s) => s.trim(),
|
||||
toLowerCase: (s) => s.toLowerCase(),
|
||||
toUpperCase: (s) => s.toUpperCase(),
|
||||
toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s,
|
||||
}
|
||||
|
||||
const getDef: (() => CodeKeywordDefinition) & {
|
||||
transform: typeof transform
|
||||
} = Object.assign(_getDef, {transform})
|
||||
|
||||
function _getDef(): CodeKeywordDefinition {
|
||||
return {
|
||||
keyword: "transform",
|
||||
schemaType: "array",
|
||||
before: "enum",
|
||||
code(cxt: KeywordCxt) {
|
||||
const {gen, data, schema, parentSchema, it} = cxt
|
||||
const {parentData, parentDataProperty} = it
|
||||
const tNames: string[] = schema
|
||||
if (!tNames.length) return
|
||||
let cfg: Name | undefined
|
||||
if (tNames.includes("toEnumCase")) {
|
||||
const config = getEnumCaseCfg(parentSchema)
|
||||
cfg = gen.scopeValue("obj", {ref: config, code: stringify(config)})
|
||||
}
|
||||
gen.if(_`typeof ${data} == "string" && ${parentData} !== undefined`, () => {
|
||||
gen.assign(data, transformExpr(tNames.slice()))
|
||||
gen.assign(_`${parentData}[${parentDataProperty}]`, data)
|
||||
})
|
||||
|
||||
function transformExpr(ts: string[]): Code {
|
||||
if (!ts.length) return data
|
||||
const t = ts.pop() as string
|
||||
if (!(t in transform)) throw new Error(`transform: unknown transformation ${t}`)
|
||||
const func = gen.scopeValue("func", {
|
||||
ref: transform[t as TransformName],
|
||||
code: _`require("ajv-keywords/dist/definitions/transform").transform${getProperty(t)}`,
|
||||
})
|
||||
const arg = transformExpr(ts)
|
||||
return cfg && t === "toEnumCase" ? _`${func}(${arg}, ${cfg})` : _`${func}(${arg})`
|
||||
}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "array",
|
||||
items: {type: "string", enum: Object.keys(transform)},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function getEnumCaseCfg(parentSchema: AnySchemaObject): TransformConfig {
|
||||
// build hash table to enum values
|
||||
const cfg: TransformConfig = {hash: {}}
|
||||
|
||||
// requires `enum` in the same schema as transform
|
||||
if (!parentSchema.enum) throw new Error('transform: "toEnumCase" requires "enum"')
|
||||
for (const v of parentSchema.enum) {
|
||||
if (typeof v !== "string") continue
|
||||
const k = configKey(v)
|
||||
// requires all `enum` values have unique keys
|
||||
if (cfg.hash[k]) {
|
||||
throw new Error('transform: "toEnumCase" requires all lowercased "enum" values to be unique')
|
||||
}
|
||||
cfg.hash[k] = v
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
function configKey(s: string): string {
|
||||
return s.toLowerCase()
|
||||
}
|
||||
|
||||
export default getDef
|
||||
module.exports = getDef
|
27
my-app/node_modules/ajv-keywords/src/definitions/typeof.ts
generated
vendored
Executable file
27
my-app/node_modules/ajv-keywords/src/definitions/typeof.ts
generated
vendored
Executable file
|
@ -0,0 +1,27 @@
|
|||
import type {CodeKeywordDefinition, KeywordCxt} from "ajv"
|
||||
import {_} from "ajv/dist/compile/codegen"
|
||||
|
||||
const TYPES = ["undefined", "string", "number", "object", "function", "boolean", "symbol"]
|
||||
|
||||
export default function getDef(): CodeKeywordDefinition {
|
||||
return {
|
||||
keyword: "typeof",
|
||||
schemaType: ["string", "array"],
|
||||
code(cxt: KeywordCxt) {
|
||||
const {data, schema, schemaValue} = cxt
|
||||
cxt.fail(
|
||||
typeof schema == "string"
|
||||
? _`typeof ${data} != ${schema}`
|
||||
: _`${schemaValue}.indexOf(typeof ${data}) < 0`
|
||||
)
|
||||
},
|
||||
metaSchema: {
|
||||
anyOf: [
|
||||
{type: "string", enum: TYPES},
|
||||
{type: "array", items: {type: "string", enum: TYPES}},
|
||||
],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getDef
|
58
my-app/node_modules/ajv-keywords/src/definitions/uniqueItemProperties.ts
generated
vendored
Executable file
58
my-app/node_modules/ajv-keywords/src/definitions/uniqueItemProperties.ts
generated
vendored
Executable file
|
@ -0,0 +1,58 @@
|
|||
import type {FuncKeywordDefinition, AnySchemaObject} from "ajv"
|
||||
import equal = require("fast-deep-equal")
|
||||
|
||||
const SCALAR_TYPES = ["number", "integer", "string", "boolean", "null"]
|
||||
|
||||
export default function getDef(): FuncKeywordDefinition {
|
||||
return {
|
||||
keyword: "uniqueItemProperties",
|
||||
type: "array",
|
||||
schemaType: "array",
|
||||
compile(keys: string[], parentSchema: AnySchemaObject) {
|
||||
const scalar = getScalarKeys(keys, parentSchema)
|
||||
|
||||
return (data) => {
|
||||
if (data.length <= 1) return true
|
||||
for (let k = 0; k < keys.length; k++) {
|
||||
const key = keys[k]
|
||||
if (scalar[k]) {
|
||||
const hash: Record<string, any> = {}
|
||||
for (const x of data) {
|
||||
if (!x || typeof x != "object") continue
|
||||
let p = x[key]
|
||||
if (p && typeof p == "object") continue
|
||||
if (typeof p == "string") p = '"' + p
|
||||
if (hash[p]) return false
|
||||
hash[p] = true
|
||||
}
|
||||
} else {
|
||||
for (let i = data.length; i--; ) {
|
||||
const x = data[i]
|
||||
if (!x || typeof x != "object") continue
|
||||
for (let j = i; j--; ) {
|
||||
const y = data[j]
|
||||
if (y && typeof y == "object" && equal(x[key], y[key])) return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
},
|
||||
metaSchema: {
|
||||
type: "array",
|
||||
items: {type: "string"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function getScalarKeys(keys: string[], schema: AnySchemaObject): boolean[] {
|
||||
return keys.map((key) => {
|
||||
const t = schema.items?.properties?.[key]?.type
|
||||
return Array.isArray(t)
|
||||
? !t.includes("object") && !t.includes("array")
|
||||
: SCALAR_TYPES.includes(t)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = getDef
|
Loading…
Add table
Add a link
Reference in a new issue