vine icon indicating copy to clipboard operation
vine copied to clipboard

[Question] Creating rules on vine "VineArray"

Open benjibenoit opened this issue 2 years ago • 7 comments

Package version

"@adonisjs/core": "^6.2.2", "@vinejs/vine": "^1.7.0"

Describe the bug

Hey, I have an array that I want to make conditional depending on whether or not an object is present in my database. On the doc it's explained for VineString, but I can't figure out how to do it with VineArray. Does anyone have any ideas?

benjibenoit avatar Jan 29 '24 08:01 benjibenoit

Its same for all the schema classes, be it VineString, VineObject, or VineArray.

thetutlage avatar Jan 29 '24 09:01 thetutlage

Its same for all the schema classes, be it VineString, VineObject, or VineArray.

Here's the problem I'm having with VineArray: image

benjibenoit avatar Jan 29 '24 09:01 benjibenoit

@thetutlage I can't seem to add a custom rule to the Array type. When I copy the doc for the unique() rule, and remove the string checks, it isn't executed, but it is on a string() entry. None of my rules are executed on array(), is this normal?

benjibenoit avatar Jan 29 '24 15:01 benjibenoit

Hello, can you please share a reproduction repo with for the same and I will try to debug and see if I can provide some helpful info

thetutlage avatar Jan 30 '24 05:01 thetutlage

Hello, can you please share a reproduction repo with for the same and I will try to debug and see if I can provide some helpful info

You can find a repo here: https://github.com/shankiflang/repo-repro. As far as I can see, the implicit : true doesn't work.

If I set the data for a message to:

{
    "postId": 1
}

the isRequiredIfPostPublishedRule() method is not called. In contrast, if I put :

{
    "postId": 1,
    "config": [
        {
            "hi": "foo"
        }
    ]
}

The method is called.

benjibenoit avatar Jan 30 '24 09:01 benjibenoit

Thanks for the repro. I will look into it once I get some time and see if rules on an array respect the implict flag or not.

thetutlage avatar Jan 31 '24 04:01 thetutlage

Hello @thetutlage , I'm facing the same error with VineObject.

sinh117801 avatar Sep 07 '24 06:09 sinh117801

I see the source of the issue lies within the compiler. The compiler won't execute rules for an Array or object when the original value is not an Array or Object.

I am not sure if changing this behavior is going to be super helpful, because that might make the codebase a bit more complicated.

So for now, I will pass on this feature. However, if you think that rules are not executed even when you pass an array or object, then I am happy to look into it 👍

thetutlage avatar Nov 29 '24 02:11 thetutlage

Added support for it in https://github.com/vinejs/vine/commit/890228e6b76c30ce39dc281f52c5e5014d62c183

thetutlage avatar Nov 30 '24 07:11 thetutlage