23andMe / Yamale

A schema and validator for YAML.
MIT License
666 stars 88 forks source link

Schema not found by CLI if path to document starts with ".." #238

Open AndreyNautilus opened 5 months ago

AndreyNautilus commented 5 months ago

First of all: thanks for a great tool!

Description yamale CLI can't find schema file if path to schema file doesn't form a valid path with any parent folder of the path to the document file. For example (see "how to reproduce" below):

How to reproduce Let's assume the following folder structure:

temp/
├─ a/
│  ├─ b/
│  │  ├─ my_schema.yaml
├─ c/
│  ├─ document.yaml

with document.yaml:

name: abc

and my_schema.yaml:

name: str()

If the current working directory is temp, everything works as expected:

temp$ yamale c/document.yaml --schema=a/b/my_schema.yaml
Validating .../temp/c/document.yaml...
Validation success! 👍

but if we change current working directory to temp/a, CLI suddenly fails, because the schema file is not found:

temp$ cd a
temp/a$ yamale ../c/document.yaml --schema=b/my_schema.yaml
Validating .../temp/c/document.yaml...
Validation failed!
Invalid schema name for 'b/my_schema.yaml' or schema not found.

This looks like a bug in CLI.

Root-cause analysis It seems like intended behaviour in _find_data_path_schema function of yamale/command_line.py file: traverse all parent folders of the absolute path to the document, append the path to schema as suffix and try to read the schema by that path. So, if such iteration never returns a path to schema, the schema is not found.

Suggested solution Take current working directory into account when looking for schema. Before iterating parent directories of the path to the document, try to find the schema starting from the current working directory.

I can create a PR with the fix if you prefer.

cblakkan commented 5 months ago

I can create a PR with the fix if you prefer.

Will gladly accept contributions if you feel so inclined

AndreyNautilus commented 5 months ago

@cblakkan , sorry it took longer than expected to create a proper PR, but here it is: https://github.com/23andMe/Yamale/pull/241