0

Is there any way to validate YAML but not JSON? I specifically wanted to check if the response is in YAML but not in JSON or Plain Text. Is there any way to do this? I do not want to do this checking the content-type

I tried using YAML.load() available in the js-yaml library but this is parsing any type of file format to YAML but does not fail in any case. I check to test if the response is YAML. Is there any way to validate the structure of YAML? Or any other way? The output should fail if we pass any file format other than yaml.

anonymous
  • 1
  • 1
  • don't you want some specific yaml? check for that; it should have certain properties, lengths, features, etc after parsing. Like you said, almost anything _can_ be yaml. You could also eliminate other related formats, like if it JSON.parse()s, it's aint yaml... – dandavis Nov 08 '22 at 06:28

1 Answers1

0

Check to see if it is YAML:

 yaml = require('js-yaml');
 fs   = require('fs');


 try {
  var doc = yaml.safeLoad(fs.readFileSync('../yourYAML.yml', 'utf8'));
   console.log(doc);
 } catch (e) {
   console.log(e);
 }
reakt
  • 500
  • 8
  • 25
  • I was not reading from a file, but the response is from an API call. Then how can I perform the same? – anonymous Nov 08 '22 at 06:58
  • @anonymous https://stackoverflow.com/questions/42025088/validation-of-express-request-based-on-swagger-yaml – reakt Nov 08 '22 at 07:21