indicative icon indicating copy to clipboard operation
indicative copied to clipboard

Nested field does not respect validation rules

Open soubhikchatterjee opened this issue 4 years ago • 1 comments

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,

  • in Example 2, i expect the validation to fail but it doesn't (since i am using true instead of 1)
  • in example 3, I expect it to fail, but it does not as I am using nested fields.

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 avatar May 15 '20 09:05 soubhikchatterjee

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

Thanks

soubhikchatterjee avatar Jun 08 '20 04:06 soubhikchatterjee