Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow parsing multi-doc yaml files #498

Merged
merged 4 commits into from
Aug 25, 2024
Merged

allow parsing multi-doc yaml files #498

merged 4 commits into from
Aug 25, 2024

Conversation

chris48s
Copy link
Owner

@chris48s chris48s commented Aug 25, 2024

Closes #378

Changelog:

  • Allow parsing multi-doc yaml
  • parseInputFile() may now return Document[]
  • Adds documentIndex property to ValidationResult type

@chris48s
Copy link
Owner Author

Here's an issue I didn't think of.

I need to output something sensible in getSingleResultLogMessage()

One way I could deal with this would be:

   getSingleResultLogMessage(result, fileLocation, format) {
     if (result.valid === false && format === "text") {
-      return formatErrors(fileLocation, result.errors);
+      return formatErrors(`${fileLocation}[${result.documentIndex}]`, result.errors);
     }
   }
 }

That is simple and easy, but it means I'll switch from saying

./package.json#/version must be string

to

./package.json[0]#/version must be string

in all cases. It would be nice to avoid that given multiple documents in one file is quite an uncommon case.

I could do this:

     let results = [];
     for (const filename of filenames) {
       const fileResults = await validateFile(filename, config, plugins, cache);
+      const multiDoc = fileResults.length > 1;
       results = results.concat(fileResults);
 
       for (const result of results) {
         for (const plugin of plugins) {
+          const fileLocation = multiDoc
+            ? `${filename}[${result.documentIndex}]`
+            : filename;
           const message = plugin.getSingleResultLogMessage(
             result,
-            filename,
+            fileLocation,
             config.format,
           );
           if (message != null) {

but it feels like a bit of a hack and means the content of fileLocation is inconsistent in different places.

I could make some kind of breaking change to getSingleResultLogMessage() that allows me to pass in a variable telling me if the file is a multi-doc.

Finally, I could indicate this in some way in the ValidationResult object. One way would be to add a specific property. The other thing I could do is say that documentIndex is null if the file only contains a single document and only populate it with a number when it contains >1. I think on balance that is probably the nicest approach.

@chris48s
Copy link
Owner Author

reckon this is probably good. Needs another read over with fresh eyes though

Copy link

codecov bot commented Aug 25, 2024

Codecov Report

Attention: Patch coverage is 98.40000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 96.60%. Comparing base (3a9c9ed) to head (6342e8c).
Report is 8 commits behind head on main.

Files Patch % Lines
src/cli.js 98.87% 0 Missing and 1 partial ⚠️
src/plugins/parser-yaml.js 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #498      +/-   ##
==========================================
+ Coverage   96.40%   96.60%   +0.19%     
==========================================
  Files          20       20              
  Lines        1281     1354      +73     
  Branches      268      283      +15     
==========================================
+ Hits         1235     1308      +73     
  Misses         45       45              
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/cli.js Outdated Show resolved Hide resolved
@chris48s chris48s marked this pull request as ready for review August 25, 2024 17:57
@chris48s chris48s changed the title WIP allow parsing multi-doc yaml files allow parsing multi-doc yaml files Aug 25, 2024
@chris48s chris48s merged commit e2df1b2 into main Aug 25, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFC: Multidoc YAML support
1 participant