Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
if (!isPlainObject(value)) {
return value
}
// Apply missing keys is the parent is an array
const initial: {[key: string]: any} = parentIsArray && !value._key ? {_key: randomKey()} : {}
// Ensure non-root objects have _type
if (path.length > 0 && !value._type) {
if (value._ref) {
// In the case of references, we know what the type should be, so apply it
initial._type = 'reference'
} else {
throw new Error(`missing "_type" property at path "${pathToString(path)}"`)
}
}
if (value._ref) {
validateReference(value, path)
}
// Validate deeply
return Object.keys(value).reduce((acc, key) => {
acc[key] = validateValue(value[key], path.concat([key]))
return acc
}, initial)
}
function validateReference(value, path: (string | number)[] = []) {
if (!value._type && value.type) {
throw new Error(
`Reference is missing "_type", but has a "type" property at path "${pathToString(path)}"`
)
}
const disallowed = Object.keys(value).filter(key => !ALLOWED_REF_PROPS.includes(key))
if (disallowed.length > 0) {
const plural = disallowed.length > 1 ? 'properties' : 'property'
throw new Error(
`Disallowed ${plural} found in reference: ${disallowed
.map(quote)
.join(', ')} at path "${pathToString(path)}"`
)
}
}
return value.map((item, i) => {
if (Array.isArray(item)) {
throw new Error(
`multidimensional arrays are not supported (at path "${pathToString(path)}")`
)
}
return validateValue(item, path.concat(i), true)
})
}
function validateReference(value, path: (string | number)[] = []) {
if (!value._type && value.type) {
throw new Error(
`Reference is missing "_type", but has a "type" property at path "${pathToString(path)}"`
)
}
const disallowed = Object.keys(value).filter(key => !ALLOWED_REF_PROPS.includes(key))
if (disallowed.length > 0) {
const plural = disallowed.length > 1 ? 'properties' : 'property'
throw new Error(
`Disallowed ${plural} found in reference: ${disallowed
.map(quote)
.join(', ')} at path "${pathToString(path)}"`
)
}
}