23andMe / Yamale

A schema and validator for YAML.
MIT License
680 stars 89 forks source link

schema file in current directory overrules schemas in deeper directories #69

Closed dvandok closed 4 years ago

dvandok commented 5 years ago

When yamale is run on the command line in a file tree of yaml files and schema files, the program wrongly validates against the schema file in the current directory rather than matching the schema files deeper in the file tree.

Example:

cwd/
   +- schema.yaml
   +- data.yml
   +- subdir/
       +- schema.yaml
       +- data2.yml

When yamale is run from cwd, both data.yml and data2.yml are validated against cwd/schema.yaml; if the command is called from another directory (outside or higher in the tree) the file data2.yml is validated against cwd/subdir/schema.yaml.

The reason is that the glob in _find_schema will try to match the schema file name without a path prefix, which will match a schema file in the current directory.

https://github.com/23andMe/Yamale/blob/7a57de7f1cac4284567e8240ca1fe055bc030c1b/yamale/command_line.py#L55

This should probably be changed to match the directory name of the data file plus the name of the schema.

mildebrandt commented 5 years ago

Thanks for using Yamale and raising this issue. I'll try to find time to address this. If you think you have a good solution, we do welcome PRs.

PeterWhittaker commented 4 years ago

I cloned the repo and validated that adding the data_path solves the issue. I'll issue a PR shortly. To test this, I verified the failure with unmodified yamale, then modified the yamale/command_line.py and verified the fix.

To verify the failure,

  1. I created a testIt folder, containing a schema.yaml with a: str() and a sample.yml with a: 'string'
  2. In testIt I created a subfolder with schema.yaml with a: int() and a sample.yml with a: 12
  3. I ran yamale from the command line against testIt, and verified the failure: the sample in the subfolder failed because a was not a string.

After modifying the yamale/command_line.py, I ran three different tox tests and all three succeeded:

  1. The default from the unmodified tox.ini
  2. commands yamale <full path to testIt>
  3. changedir = <full path to testit>; commands = yamale
mildebrandt commented 4 years ago

Fixed in version 3.0.4