How to check the response from an API as YAML but also check if the response is not JSON or Plain Text using javascript?

519 Views Asked by At

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.

1

There are 1 best solutions below

2
reakt On

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);
 }