poppinss / indicative

Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
https://indicative.adonisjs.com/
MIT License
417 stars 52 forks source link

Nested field does not respect validation rules #260

Open soubhikchatterjee opened 4 years ago

soubhikchatterjee commented 4 years ago

There are 2 scenarios (Example2 and Example 3) in the code below where I expect the validator to behave in a certain way but it doesn't.

For eg,

Can you please check and let me know if I am missing something?

Package version

7.2.1

Node.js and npm version

node: v12.4.0 npm - 6.14.4

Sample Code (to reproduce the issue)

 import { validate } from "indicative/validator";

/**
 * Example 1:
 *
 * Expected Behaviour: Should Pass
 *
 * Actual Behaviour: Passes
 *
 * */
const rules1 = {
  hasPassword: "required|boolean",
  password: "required_when:hasPassword,1"
};

const data1 = {
  hasPassword: 1,
  password: "test"
};

validate(data1, rules1)
  .then(() => console.log("Example 1"))
  .catch(() => console.error("Example 1"));

/**
 * Example 2: (using true instead of 1)
 *
 * Expected Behaviour: Should Fail
 *
 * Actual Behaviour: Does not fail
 *
 * */

const rules2 = {
  hasPassword: "required|boolean",
  password: "required_when:hasPassword,true"
};

const data2 = {
  hasPassword: true
};

validate(data2, rules2)
  .then(() => console.log("Example 2"))
  .catch(() => console.error("Example 2"));

/**
 * Example 3: Nested fields
 *
 * Expected Behaviour: Should fail (since password is missing)
 *
 * Actual Behaviour: Does not fail
 *
 * */

const rules3 = {
  "fields.*.hasPassword": "required|boolean",
  "fields.*.password": "required_when:fields.*.hasPassword,1"
};

const data3 = [
  {
    fields: {
      hasPassword: 1
    }
  }
];

validate(data3, rules3)
  .then(() => console.log("Example 3"))
  .catch(() => console.error("Example 3"));

BONUS (a sample repo to reproduce the issue)

https://codesandbox.io/s/cold-dawn-4fr7g?file=/src/index.js:631-635

soubhikchatterjee commented 4 years ago

@thetutlage Did you get a chance to look into the code samples?

Thanks