Skip to content

Commit

Permalink
Add skipInvalid dumper option. nodeca#76
Browse files Browse the repository at this point in the history
Dervus Grim committed Jun 4, 2013
1 parent 2c42198 commit 68cbb29
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
* Rename `DEFAULT_SCHEMA` to `DEFAULT_FULL_SCHEMA`
and `SAFE_SCHEMA` to `DEFAULT_SAFE_SCHEMA`.
* Bug fix: export `NIL` constant from the public interface.
* Add `skipInvalid` dumper option.


2.0.5 / 2013-04-26
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -164,6 +164,8 @@ Serializes `object` as YAML document.
options:

- `indent` _(default: 2)_ - indentation width to use (in spaces).
- `skipInvalid` _(default: false)_ - do not throw on invalid types (like function
in the safe schema) and write `null` instead of values of such types.
- `flowLevel` (default: -1) - specifies level of nesting, when to switch from
block to flow style for collections. -1 means block style everwhere
- `styles` - "tag" => "style" map. Each tag may have own set of styles.
11 changes: 7 additions & 4 deletions lib/js-yaml/dumper.js
Original file line number Diff line number Diff line change
@@ -128,10 +128,11 @@ function encodeHex(character) {
function dump(input, options) {
options = options || {};

var schema = options['schema'] || DEFAULT_FULL_SCHEMA,
indent = Math.max(1, (options['indent'] || 2)),
flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']),
styleMap = compileStyleMap(schema, options['styles'] || null),
var schema = options['schema'] || DEFAULT_FULL_SCHEMA,
indent = Math.max(1, (options['indent'] || 2)),
skipInvalid = options['skipInvalid'] || false,
flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']),
styleMap = compileStyleMap(schema, options['styles'] || null),

implicitTypes = schema.compiledImplicit,
explicitTypes = schema.compiledExplicit,
@@ -414,6 +415,8 @@ function dump(input, options) {
if ('?' !== tag) {
writeScalar(result);
}
} else if (skipInvalid) {
writeNode(level, null, block, compact);
} else {
throw new YAMLException('unacceptabe kind of an object to dump (' + kind + ')');
}
1 change: 1 addition & 0 deletions test/issues.js
Original file line number Diff line number Diff line change
@@ -12,4 +12,5 @@ describe('Issues.', function () {
require('./issues/issue-54.js');
require('./issues/issue-64.js');
require('./issues/issue-parse-function-security.js');
require('./issues/issue-skip-invalid.js');
});
38 changes: 38 additions & 0 deletions test/issues/issue-skip-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
/*global it */


var assert = require('assert');
var yaml = require('../../lib/js-yaml');


var sample = {
number: 42,
undef: undefined,
string: 'hello',
func: function (a, b) { return a + b; },
regexp: /^hel+o/,
array: [1, 2, 3]
};


var expected = {
number: 42,
undef: null,
string: 'hello',
func: null,
regexp: {},
array: [1, 2, 3]
};


it('Dumper must throw an exception on invalid type when option `skipInvalid` is false.', function () {
assert.throws(function () {
yaml.safeDump(sample, { skipInvalid: false });
}, yaml.YAMLException);
});


it('Dumper must write `null` for invalid types when option `skipInvalid` is true.', function () {
assert.deepEqual(yaml.load(yaml.safeDump(sample, { skipInvalid: true })), expected);
});

0 comments on commit 68cbb29

Please sign in to comment.