Allow to validate yaml file against its modeline schema
Hello 👋
This is a quick and dirty attempt at implementing #340
I submit it to gather some feedback before eventually going further... Am i going in the right way? What would your suggestions and improvements?
Regards.
Thanks for the idea and the PR!
I'm still thinking about this, but I'm actually pretty well inclined towards this solution. I've wanted a generic flag for picking up on the schema from files for YAML modelines and from JSON files which use $schema to specify their schemas, but I think doing YAML modeline support and limiting it to a single file is a very reasonable starting point.
My big questions right now are about how we parse the modeline. And we'll need some tests to validate the behavior. Is the modeline required to be the first line in the file? I would expect that it could show up several lines later. And I think maybe it should be supported if it's indented or has irregular whitespace, so there's probably some room for testing and refinement of the parsing.
I need to give this more attention to get the details right, but this is a great start!
I'm still thinking about this, but I'm actually pretty well inclined towards this solution. I've wanted a generic flag for picking up on the schema from files for YAML modelines and from JSON files which use
$schemato specify their schemas, but I think doing YAML modeline support and limiting it to a single file is a very reasonable starting point.
As an user of check-jsonschema through pre-commit hooks, I would find very convenient to enable this for all yaml files using a dedicated hook or an additional argument in one of the existing hooks.
My big questions right now are about how we parse the modeline. And we'll need some tests to validate the behavior. Is the modeline required to be the first line in the file? I would expect that it could show up several lines later. And I think maybe it should be supported if it's indented or has irregular whitespace, so there's probably some room for testing and refinement of the parsing.
As this feature request is inspired from a vscode extension, I have looked for the way they handle it. Here is the piece of code they use to find the modeline: if I am not mistaken, it basically checks every yaml comments of the source file.