MCPcopy
hub / github.com/ajv-validator/ajv / validateCode

Function validateCode

lib/standalone/index.ts:59–94  ·  view source on GitHub ↗
(usedValues: UsedScopeValues, s?: SourceCode)

Source from the content-addressed store, hash-verified

57 }
58
59 function validateCode(usedValues: UsedScopeValues, s?: SourceCode): Code {
60 if (!s) throw new Error('moduleCode: function does not have "source" property')
61 if (usedState(s.validateName) === UsedValueState.Completed) return nil
62 setUsedState(s.validateName, UsedValueState.Started)
63
64 const scopeCode = ajv.scope.scopeCode(s.scopeValues, usedValues, refValidateCode)
65 const code = new _Code(`${scopeCode}${_n}${s.validateCode}`)
66 return s.evaluated ? _`${code}${s.validateName}.evaluated = ${s.evaluated};${_n}` : code
67
68 function refValidateCode(n: ValueScopeName): Code | undefined {
69 const vRef = n.value?.ref
70 if (n.prefix === "validate" && typeof vRef == "function") {
71 const v = vRef as AnyValidateFunction
72 return validateCode(usedValues, v.source)
73 } else if ((n.prefix === "root" || n.prefix === "wrapper") && typeof vRef == "object") {
74 const {validate, validateName} = vRef as SchemaEnv
75 if (!validateName) throw new Error("ajv internal error")
76 const def = ajv.opts.code.es5 ? varKinds.var : varKinds.const
77 const wrapper = _`${def} ${n} = {validate: ${validateName}};`
78 if (usedState(validateName) === UsedValueState.Started) return wrapper
79 const vCode = validateCode(usedValues, validate?.source)
80 return _`${wrapper}${_n}${vCode}`
81 }
82 return undefined
83 }
84
85 function usedState(name: ValueScopeName): UsedValueState | undefined {
86 return usedValues[name.prefix]?.get(name)
87 }
88
89 function setUsedState(name: ValueScopeName, state: UsedValueState): void {
90 const {prefix} = name
91 const names = (usedValues[prefix] = usedValues[prefix] || new Map())
92 names.set(name, state)
93 }
94 }
95}
96
97module.exports = exports = standaloneCode

Callers 3

funcExportCodeFunction · 0.85
multiExportsCodeFunction · 0.85
refValidateCodeFunction · 0.85

Calls 4

_Function · 0.90
usedStateFunction · 0.85
setUsedStateFunction · 0.85
scopeCodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…