Cross validation in objects involving arrays: how to get the exact item that failed #1368
-
|
Hello! Thanks in advance for considering my question :) I have a doubt about cross field validation in an object schema with arrays inside. const schema = object({
basePath: string(),
assets: array(string())
});My goal is to check if the given assets (that I assume are all relative paths) exist when resolved against the Since I do not have access to the const schema = pipe(
object({
basePath: string(),
assets: array(string())
}),
check(
({basePath, assets}) => assets.every(asset => fs.existsSync(path.resolve(basePath, asset))),
"One of the assets does not exist."
)
);While this works as a validation, in case of error I am missing which one of the So I'm wondering: is it possible to somehow intercept the exact path that gave the error? Or even better: having a validation pipeline directly on the For example something like this, with a sort of const schema = object({
basePath: string(),
assets: array(
pipe(
string(),
custom(
(input, cxt) => fs.existsSync(path.resolve(cxt.basePath, input)),
// Here would be the callback returning the error msg with the exact invalid path
)
)
)
});Does anyone know a better approach to face this use case? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hey, sorry for the late reply. Accessing the "parent" object is not possible but you can use |
Beta Was this translation helpful? Give feedback.
Hey, sorry for the late reply. Accessing the "parent" object is not possible but you can use
rawCheckinstead ofcheckto have full control over the validation and the returned issues.