Skip to content

Pass a parameter to a custom rule #3769

Discussion options

You must be logged in to vote

I posted this issue on Discord and I got this solution:

validator.rule(
    'hasValidProductQuantity',
    async (value, _, options) => {
        if (typeof value !== 'number') {
            return
        }

        const id = validator.helpers.getFieldValue('id', options.root, options.tip)

        const product = await Product.query().where('id', id).first()

        if (!product) {
            return
        }

        if (value > product!.quantity) {
            options.errorReporter.report(
                options.pointer,
                'hasValidProductQuantity',
                'hasValidProductQuantity validation failed',
                options.arrayExpressionPointer
            )

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by 3adel-bassiony
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
1 participant