ajv
Advanced tools
Comparing version 7.0.3 to 7.0.4
@@ -28,2 +28,9 @@ import { Code, Name } from "./code"; | ||
}; | ||
export declare enum UsedValueState { | ||
Started = 0, | ||
Completed = 1 | ||
} | ||
export declare type UsedScopeValues = { | ||
[Prefix in string]?: Map<ValueScopeName, UsedValueState | undefined>; | ||
}; | ||
export declare const varKinds: { | ||
@@ -70,5 +77,5 @@ const: Name; | ||
scopeRefs(scopeName: Name, values?: ScopeValues | ScopeValueSets): Code; | ||
scopeCode(values?: ScopeValues | ScopeValueSets, usedValues?: ScopeValueSets, getCode?: (n: ValueScopeName) => Code | undefined): Code; | ||
scopeCode(values?: ScopeValues | ScopeValueSets, usedValues?: UsedScopeValues, getCode?: (n: ValueScopeName) => Code | undefined): Code; | ||
private _reduceValues; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ValueScope = exports.ValueScopeName = exports.Scope = exports.varKinds = void 0; | ||
exports.ValueScope = exports.ValueScopeName = exports.Scope = exports.varKinds = exports.UsedValueState = void 0; | ||
const code_1 = require("./code"); | ||
@@ -11,2 +11,7 @@ class ValueError extends Error { | ||
} | ||
var UsedValueState; | ||
(function (UsedValueState) { | ||
UsedValueState[UsedValueState["Started"] = 0] = "Started"; | ||
UsedValueState[UsedValueState["Completed"] = 1] = "Completed"; | ||
})(UsedValueState = exports.UsedValueState || (exports.UsedValueState = {})); | ||
exports.varKinds = { | ||
@@ -116,7 +121,7 @@ const: new code_1.Name("const"), | ||
continue; | ||
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Set()); | ||
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Map()); | ||
vs.forEach((name) => { | ||
if (nameSet.has(name)) | ||
return; | ||
nameSet.add(name); | ||
nameSet.set(name, UsedValueState.Started); | ||
let c = valueCode(name); | ||
@@ -133,2 +138,3 @@ if (c) { | ||
} | ||
nameSet.set(name, UsedValueState.Completed); | ||
}); | ||
@@ -135,0 +141,0 @@ } |
@@ -174,3 +174,3 @@ "use strict"; | ||
const refPath = resolve_1._getFullPath(p); | ||
const baseId = resolve_1.getFullPath(root.baseId); | ||
let baseId = resolve_1.getFullPath(root.baseId); | ||
// TODO `Object.keys(root.schema).length > 0` should not be needed - but removing breaks 2 tests | ||
@@ -192,4 +192,8 @@ if (Object.keys(root.schema).length > 0 && refPath === baseId) { | ||
compileSchema.call(this, schOrRef); | ||
if (id === resolve_1.normalizeId(ref)) | ||
return new SchemaEnv({ schema: schOrRef.schema, root, baseId }); | ||
if (id === resolve_1.normalizeId(ref)) { | ||
const { schema } = schOrRef; | ||
if (schema.$id) | ||
baseId = resolve_1.resolveUrl(baseId, schema.$id); | ||
return new SchemaEnv({ schema, root, baseId }); | ||
} | ||
return getJsonPointer.call(this, p, schOrRef); | ||
@@ -196,0 +200,0 @@ } |
@@ -43,5 +43,5 @@ "use strict"; | ||
throw new Error('moduleCode: function does not have "source" property'); | ||
const { prefix } = s.validateName; | ||
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Set()); | ||
nameSet.add(s.validateName); | ||
if (usedState(s.validateName) === scope_1.UsedValueState.Completed) | ||
return code_1.nil; | ||
setUsedState(s.validateName, scope_1.UsedValueState.Started); | ||
const scopeCode = ajv.scope.scopeCode(s.scopeValues, usedValues, refValidateCode); | ||
@@ -59,8 +59,22 @@ const code = new code_1._Code(`${scopeCode}${_n}${s.validateCode}`); | ||
const { validate, validateName } = vRef; | ||
if (!validateName) | ||
throw new Error("ajv internal error"); | ||
const def = ajv.opts.code.es5 ? scope_1.varKinds.var : scope_1.varKinds.const; | ||
const wrapper = code_1._ `${def} ${n} = {validate: ${validateName}};`; | ||
if (usedState(validateName) === scope_1.UsedValueState.Started) | ||
return wrapper; | ||
const vCode = validateCode(usedValues, validate === null || validate === void 0 ? void 0 : validate.source); | ||
const def = ajv.opts.code.es5 ? scope_1.varKinds.var : scope_1.varKinds.const; | ||
return code_1._ `${def} ${n} = {validate: ${validateName}};${_n}${vCode}`; | ||
return code_1._ `${wrapper}${_n}${vCode}`; | ||
} | ||
return undefined; | ||
} | ||
function usedState(name) { | ||
var _a; | ||
return (_a = usedValues[name.prefix]) === null || _a === void 0 ? void 0 : _a.get(name); | ||
} | ||
function setUsedState(name, state) { | ||
const { prefix } = name; | ||
const names = (usedValues[prefix] = usedValues[prefix] || new Map()); | ||
names.set(name, state); | ||
} | ||
} | ||
@@ -67,0 +81,0 @@ } |
@@ -45,2 +45,11 @@ import {_, nil, Code, Name} from "./code" | ||
export enum UsedValueState { | ||
Started, | ||
Completed, | ||
} | ||
export type UsedScopeValues = { | ||
[Prefix in string]?: Map<ValueScopeName, UsedValueState | undefined> | ||
} | ||
export const varKinds = { | ||
@@ -165,3 +174,3 @@ const: new Name("const"), | ||
values: ScopeValues | ScopeValueSets = this._values, | ||
usedValues?: ScopeValueSets, | ||
usedValues?: UsedScopeValues, | ||
getCode?: (n: ValueScopeName) => Code | undefined | ||
@@ -183,3 +192,3 @@ ): Code { | ||
valueCode: (n: ValueScopeName) => Code | undefined, | ||
usedValues: ScopeValueSets = {}, | ||
usedValues: UsedScopeValues = {}, | ||
getCode?: (n: ValueScopeName) => Code | undefined | ||
@@ -191,6 +200,6 @@ ): Code { | ||
if (!vs) continue | ||
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Set()) | ||
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Map()) | ||
vs.forEach((name: ValueScopeName) => { | ||
if (nameSet.has(name)) return | ||
nameSet.add(name) | ||
nameSet.set(name, UsedValueState.Started) | ||
let c = valueCode(name) | ||
@@ -205,2 +214,3 @@ if (c) { | ||
} | ||
nameSet.set(name, UsedValueState.Completed) | ||
}) | ||
@@ -207,0 +217,0 @@ } |
@@ -249,3 +249,3 @@ import type { | ||
const refPath = _getFullPath(p) | ||
const baseId = getFullPath(root.baseId) | ||
let baseId = getFullPath(root.baseId) | ||
// TODO `Object.keys(root.schema).length > 0` should not be needed - but removing breaks 2 tests | ||
@@ -266,3 +266,7 @@ if (Object.keys(root.schema).length > 0 && refPath === baseId) { | ||
if (!schOrRef.validate) compileSchema.call(this, schOrRef) | ||
if (id === normalizeId(ref)) return new SchemaEnv({schema: schOrRef.schema, root, baseId}) | ||
if (id === normalizeId(ref)) { | ||
const {schema} = schOrRef | ||
if (schema.$id) baseId = resolveUrl(baseId, schema.$id) | ||
return new SchemaEnv({schema, root, baseId}) | ||
} | ||
return getJsonPointer.call(this, p, schOrRef) | ||
@@ -269,0 +273,0 @@ } |
import type AjvCore from "../core" | ||
import type {AnyValidateFunction, SourceCode} from "../types" | ||
import type {SchemaEnv} from "../compile" | ||
import {ScopeValueSets, ValueScopeName, varKinds} from "../compile/codegen/scope" | ||
import {_, _Code, Code, getProperty} from "../compile/codegen/code" | ||
import {UsedScopeValues, UsedValueState, ValueScopeName, varKinds} from "../compile/codegen/scope" | ||
import {_, nil, _Code, Code, getProperty} from "../compile/codegen/code" | ||
@@ -30,3 +30,3 @@ export default function standaloneCode( | ||
function funcExportCode(source?: SourceCode): string { | ||
const usedValues: ScopeValueSets = {} | ||
const usedValues: UsedScopeValues = {} | ||
const n = source?.validateName | ||
@@ -41,3 +41,3 @@ const vCode = validateCode(usedValues, source) | ||
): string { | ||
const usedValues: ScopeValueSets = {} | ||
const usedValues: UsedScopeValues = {} | ||
let code = _`"use strict";` | ||
@@ -54,7 +54,6 @@ for (const name in schemas) { | ||
function validateCode(usedValues: ScopeValueSets, s?: SourceCode): Code { | ||
function validateCode(usedValues: UsedScopeValues, s?: SourceCode): Code { | ||
if (!s) throw new Error('moduleCode: function does not have "source" property') | ||
const {prefix} = s.validateName | ||
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Set()) | ||
nameSet.add(s.validateName) | ||
if (usedState(s.validateName) === UsedValueState.Completed) return nil | ||
setUsedState(s.validateName, UsedValueState.Started) | ||
@@ -72,9 +71,22 @@ const scopeCode = ajv.scope.scopeCode(s.scopeValues, usedValues, refValidateCode) | ||
const {validate, validateName} = vRef as SchemaEnv | ||
if (!validateName) throw new Error("ajv internal error") | ||
const def = ajv.opts.code.es5 ? varKinds.var : varKinds.const | ||
const wrapper = _`${def} ${n} = {validate: ${validateName}};` | ||
if (usedState(validateName) === UsedValueState.Started) return wrapper | ||
const vCode = validateCode(usedValues, validate?.source) | ||
const def = ajv.opts.code.es5 ? varKinds.var : varKinds.const | ||
return _`${def} ${n} = {validate: ${validateName}};${_n}${vCode}` | ||
return _`${wrapper}${_n}${vCode}` | ||
} | ||
return undefined | ||
} | ||
function usedState(name: ValueScopeName): UsedValueState | undefined { | ||
return usedValues[name.prefix]?.get(name) | ||
} | ||
function setUsedState(name: ValueScopeName, state: UsedValueState): void { | ||
const {prefix} = name | ||
const names = (usedValues[prefix] = usedValues[prefix] || new Map()) | ||
names.set(name, state) | ||
} | ||
} | ||
} |
{ | ||
"name": "ajv", | ||
"version": "7.0.3", | ||
"version": "7.0.4", | ||
"description": "Another JSON Schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "dist/ajv.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
878305
14174