question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pass run-time context to custom-validator and pass the results to the message

See original GitHub issue

Hi I created a custom decorator validator, and I’m facing 2 issues

  1. I need to pass context in run-time (that contains session details). something like validate(myClass, context)
  2. 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:open
  • Created 3 years ago
  • Reactions:9
  • Comments:6

github_iconTop GitHub Comments

0reactions
miqdadamiralicommented, Sep 11, 2022

Any updates on this? Possible work arounds?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found