adonisjs / validator

Schema based validator for AdonisJS
MIT License
116 stars 39 forks source link

issue in rules.range in combination with refs and cacheKey #106

Closed mastermunj closed 3 years ago

mastermunj commented 3 years ago

Hi,

While using rules.range with refs and cacheKey, the validation does not work properly.

Package version

@adonisjs/validator@8.2.0

Node.js and npm version

Node: 14.15.1 NPM: 6.14.9

Sample Code (to reproduce the issue)

Run following commands in sequence and see the error message:

Command 1

curl --location --request POST 'localhost:9000/limits' \
--header 'Content-Type: application/json' \
--data-raw '{
    "maximumLimit": 60000,
    "availableLimit": 90000
}'

Expected Output

{
    "availableLimit": [
        "Available Limit should be between 0 to 60000"
    ]
}

Command 2

curl --location --request POST 'localhost:9000/limits' \
--header 'Content-Type: application/json' \
--data-raw '{
    "maximumLimit": 80000,
    "availableLimit": 90000
}'

Expected Output

{
    "availableLimit": [
        "Available Limit should be between 0 to 80000"
    ]
}

Actual Output

{
    "availableLimit": [
        "Available Limit should be between 0 to 60000"
    ]
}

Issue

The validation still runs with maximumLimit as 60000 rather than 80000 which is the update value passed.

Cache

When cache is disabled, by commenting this line, the validation works as expected.

BONUS (a sample repo to reproduce the issue)

https://github.com/mastermunj/adonis-range-issue

thetutlage commented 3 years ago

The refs are used incorrectly. You don't have to read the value property from it. It should be this.refs.maximumLimit

thetutlage commented 3 years ago

Also, I see that the range method doesn't accept the range at all. Is range dynamic in your case, meaning it changes depending upon the request?

mastermunj commented 3 years ago

The refs are used incorrectly. You don't have to read the value property from it. It should be this.refs.maximumLimit

If I use this.refs.maximumLimit, I get following typescript error.

Argument of type 'SchemaRef<unknown>' is not assignable to parameter of type 'number'.

Also, I see that the range method doesn't accept the range at all. Is range dynamic in your case, meaning it changes depending upon the request?

Yes, the range changes depending on the current value passed for maximumLimit.

mastermunj commented 3 years ago

Hi @thetutlage I am facing similar issue with maxLength rule as well. There also I need to pass length based on the value of another parameter.

Any suggestions for a workaround for both range and maxLength?

thetutlage commented 3 years ago

For now turning of the schema caching is the way to go. I will add support for using refs for the maxLength argument as well.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

thetutlage commented 3 years ago

Will work on it

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

mastermunj commented 3 years ago

Hi @thetutlage Is this still part of plan? This one is closed of no-activity so might get missed.