pass run-time context to custom-validator and pass the results to the message
See original GitHub issueHi I created a custom decorator validator, and I’m facing 2 issues
- I need to pass context in run-time (that contains session details). something like
validate(myClass, context) - After I validate and found issues, I want to send the details in the message. can I pass a variable between “validate” and “defaultMessage”?
This is what I currently did, but it doesn’t work:
type _ValidationOptions = ValidationOptions & {ctx: Context}
export function IsRuleValid(validationOptions?: _ValidationOptions) {
const _errors: string[] = []
return function(object: Object, propertyName: string) {
registerDecorator({
name: 'IsRuleValid',
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
validate: async (value: any, args: ValidationArguments) => {
const errors = await validate(value, validationOptions?.ctx)
_errors.concat(errors)
return errors.length === 0
},
defaultMessage(args: ValidationArguments): string {
return _errors.join()
},
},
})
}
}
and call it like this:
const errors = await validate(data, {forbidUnknownValues: true, ctx: ctx})
The actual value and args are from the decorator and not from the original call. I need it somehow to pass globally.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:6
Top Results From Across the Web
Pass parameter from validate to custom validator in hibernate ...
I'm trying to validate a bean using custom validator. But the validator needs info that is to be passed ...
Read more >Spring MVC Custom Validation - Baeldung
In this tutorial, we'll do just that; we'll create a custom validator to validate a form with a phone number field, and then...
Read more >JSF Validation Example Tutorial - validator tag, Custom ...
Here we are setting the required attribute to true which makes the field mandatory and fires the custom message “value is required” for...
Read more >CustomValidator Class (System.Web.UI.WebControls)
When using validator controls, you should always check the results of ... In this case, the Value property of the arguments parameter passed...
Read more >Overriding the Message — FluentValidation documentation
As shown in the example above, the message can contain placeholders for special values such as {PropertyName} - which will be replaced at...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

https://github.com/typestack/class-transformer/issues/912
Any updates on this? Possible work arounds?