You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
is this issue currently blocking your project? (yes/no): yes
is this issue affecting a production system? (yes/no): yes
Context
node version: 16.16.0
module version with issue: 17.7.0
last module version without issue: unknown
environment (e.g. node, browser, native): node
used with (e.g. hapi application, another framework, standalone, ...): standalone
any other relevant information:
What are you trying to achieve or the steps to reproduce?
In case of any.custom you can use helpers.error() to create validation error. In the TypeScript file (.d.ts), the definition says that error takes 2 parameters, code and optional local context.
To have localContext as a parameter it would be useful for example in case if you are doing some custom validation on root level (root object), and you would like to get correct path and label in final error. My case was limiting the duration between from and to dates to maximum of 90 days. See the following example:
constjoi=require('joi')constschema=joi.object({from: joi.date().iso().required(),to: joi.date().iso().required().greater(joi.ref('from')),}).custom((value,helpers)=>{const{ from, to }=value;constdiff=(to.getTime()-from.getTime())/86400000;if(diff>90){returnhelpers.error(`err.invalidDateRange`);}returnvalue;}).message({'err.invalidDateRange': `Invalid date range...`,});const{ error }=schema.validate({from: newDate(2022,1,1),to: newDate(2023,1,1),});console.log(error.details[0].context.label);// <- Problem: we get "value"console.log(error.details[0].path);// <- Problem: we get [] (empty array)
What result did you expect?
In case if it would be allowed to specify localState we could provide local state with path (parameter that we are validating).
...
helpers.error(`err.invalidDateRange`,undefined,{
...helpers.state,path: ['toDate'],});
...
console.log(error.details[0].context.label);// <- we get "toDate"console.log(error.details[0].path);// <- we get ["toDate"]
The text was updated successfully, but these errors were encountered:
Support plan
Context
What are you trying to achieve or the steps to reproduce?
In case of
any.custom
you can usehelpers.error()
to create validation error. In the TypeScript file (.d.ts), the definition says thaterror
takes 2 parameters,code
and optionallocal
context.From the code (
lib/validator.js
file) we can see that error method actually takes 3 parameters (code
,local
andlocalState
):What was the result you got?
To have
localContext
as a parameter it would be useful for example in case if you are doing some custom validation on root level (root object), and you would like to get correctpath
andlabel
in final error. My case was limiting the duration betweenfrom
andto
dates to maximum of 90 days. See the following example:What result did you expect?
In case if it would be allowed to specify
localState
we could provide local state with path (parameter that we are validating).The text was updated successfully, but these errors were encountered: