question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

safeDump automatically reformat yaml strings

See original GitHub issue

I have question regarding the behavior of js-yaml with safeDump, we have a yaml editor which use the following method to generate yaml from js object:

self.generateYAML = function (graph) {
                if (graph === undefined) {
                    return '';
                }
                try {
                    return jsyaml.safeDump(graph, {
                        noCompatMode: true // fix auto quoting 'y'.
                    }).trim();
                } catch (e) {
                    var error = 'YAML validation error: ' + e.message;
                    console.log(error);
                    return '';
                }
            };

graph is passed with an object that has the following content:

'Fn::Base64':

  'Fn::Join':

    - "\n"

    -

      - scriptFile=~/createSchema.sh

      - scriptLog=~/createSchema.log

      - rm -f ${scriptFile}

      - touch ${scriptFile}

      - touch ${scriptLog}

      - echo "sqlplus / as sysdba << EOF" >> ${scriptFile}

      - echo "drop user c##myappuser cascade;" >> ${scriptFile}

      - echo "create user c##myappuser identified by ${schemaPassword};" >> ${scriptFile}

      - echo "grant create session, create table, unlimited tablespace to c##myappuser;" >> ${scriptFile}

      - echo "quit" >> ${scriptFile}

      - echo "EOF" >> ${scriptFile}

      - sh ${scriptFile} >> ${scriptLog}

Somehow after safedump, the line break was reformated (though it’s still correct) and yaml becomes the following image

The version we used is 3.10.0. Please let me know if there’s anything I can do to disable this automatic transformation. Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
puzrincommented, Sep 11, 2018
0reactions
guanwcommented, Sep 17, 2018

I just realized it’s not an issue with safeDump but probably the way safeLoad works. For template that contains "\n", it will be converted to enter key, but for '\n' it will be treated as "\\n", and I just took a look here http://docs.octoprint.org/en/master/configuration/yaml.html. Seems like the behavior is correct on js-yaml. Thanks for helping. The template user provides is invalid in our case here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

yaml.dump adding unwanted newlines in multiline strings
My real problem is that if humans read this YAML file they will probably assume, as I did, that my program has broken...
Read more >
nodeca/js-yaml: JavaScript YAML parser and dumper. Very fast.
Parses string as single YAML document. Returns either a plain object, a string, a number, null or undefined , or throws YAMLException on...
Read more >
PyYAML Documentation
# A Python object corresponding to the document. If a string or a file contains several documents, you may load them all with...
Read more >
How to use safeDump function in js-yaml - Javascript - Tabnine
Writes a configuration file in YAML format. * @param {Object} config The configuration object to write. * @param {string} filePath The filename to...
Read more >
The YAML Format (Symfony Docs)
Strings in YAML can be wrapped both in single and double quotes. ... 2014-12-31 ) (otherwise it would be automatically converted into a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found