Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Nested field does not respect validation rules #260

Open
soubhikchatterjee opened this issue May 15, 2020 · 1 comment
Open

Nested field does not respect validation rules #260

soubhikchatterjee opened this issue May 15, 2020 · 1 comment

Comments

@soubhikchatterjee
Copy link

soubhikchatterjee commented May 15, 2020

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
Copy link
Author

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

Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant