Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit 8d2e94d

Browse files
committed
Merge pull request #83 from danielb2/lab
change to use lab. validate input options
2 parents d02893c + 34a4870 commit 8d2e94d

File tree

4 files changed

+167
-88
lines changed

4 files changed

+167
-88
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
test:
2-
node test/test.js
2+
@node node_modules/.bin/lab -v test/test.js
33

44
deps:
55
npm install .
66

7-
.PHONY: test deps
7+
.PHONY: test deps

lib/xml2json.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var expat = require('node-expat');
22
var sanitizer = require('./sanitize.js')
3+
var joi = require('joi');
4+
var hoek = require('hoek');
35

46
// This object will hold the final result.
57
var obj = {};
@@ -137,17 +139,17 @@ module.exports = function(xml, _options) {
137139
ancestors = [];
138140
currentElementName = null;
139141

140-
options = {
141-
object: false,
142-
reversible: false,
143-
coerce: true,
144-
sanitize: true,
145-
trim: true
142+
var schema = {
143+
object: joi.boolean().default(false),
144+
reversible: joi.boolean().default(false),
145+
coerce: joi.boolean().default(true),
146+
sanitize: joi.boolean().default(true),
147+
trim: joi.boolean().default(true),
148+
arrayNotation: joi.boolean().default(false)
146149
};
147-
148-
for (var opt in _options) {
149-
options[opt] = _options[opt];
150-
}
150+
var validation = joi.validate(_options, schema);
151+
hoek.assert(validation.error === null, validation.error);
152+
options = validation.value;
151153

152154
if (!parser.parse(xml)) {
153155
throw new Error('There are errors in your xml file: ' + parser.getError());

package.json

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
{ "name" : "xml2json",
2-
"version": "0.7.1",
3-
"author": "Andrew Turley",
4-
"email": "[email protected]",
5-
"description" : "Converts xml to json and vice-versa, using node-expat.",
6-
"repository": "git://github.com/buglabs/node-xml2json.git",
7-
"license": "MIT",
8-
"main": "index",
9-
"contributors":[
10-
{"name": "Camilo Aguilar", "email": "[email protected]"}
11-
],
12-
"dependencies": {
13-
"node-expat": "^2.3.7"
14-
},
15-
"bin": {
16-
"xml2json": "bin/xml2json"
1+
{
2+
"name": "xml2json",
3+
"version": "0.7.1",
4+
"author": "Andrew Turley",
5+
"email": "[email protected]",
6+
"description": "Converts xml to json and vice-versa, using node-expat.",
7+
"repository": "git://github.com/buglabs/node-xml2json.git",
8+
"license": "MIT",
9+
"main": "index",
10+
"scripts": {
11+
"test": "make test"
12+
},
13+
"contributors": [
14+
{
15+
"name": "Camilo Aguilar",
16+
"email": "[email protected]"
1717
}
18+
],
19+
"dependencies": {
20+
"hoek": "^2.14.0",
21+
"joi": "^6.4.3",
22+
"node-expat": "^2.3.7"
23+
},
24+
"bin": {
25+
"xml2json": "bin/xml2json"
26+
},
27+
"devDependencies": {
28+
"code": "^1.4.1",
29+
"lab": "5.x.x"
30+
}
1831
}
19-

test/test.js

Lines changed: 124 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,129 @@ var path = require('path');
33
var parser = require(__dirname + '/../lib');
44
var assert = require('assert');
55

6-
var fixturesPath = __dirname + '/fixtures';
7-
8-
fs.readdir(fixturesPath, function(err, files) {
9-
for (var i in files) {
10-
var file = files[i];
11-
var ext = path.extname(file);
12-
13-
if (ext == '.xml') {
14-
var basename = path.basename(file, '.xml');
15-
16-
var data = fs.readFileSync(fixturesPath + '/' + file);
17-
var result = parser.toJson(data, {reversible: true});
18-
19-
var data2 = fs.readFileSync(fixturesPath + '/' + file);
20-
if (file.indexOf('spacetext') >= 0) {
21-
result = parser.toJson(data2, {trim: false, coerce: false});
22-
} else if (file.indexOf('coerce') >= 0) {
23-
result = parser.toJson(data2, {coerce: false});
24-
} else if (file.indexOf('domain') >= 0) {
25-
result = parser.toJson(data2, {coerce: false});
26-
} else if (file.indexOf('large') >= 0) {
27-
result = parser.toJson(data2, {coerce: false, trim: true, sanitize: false});
28-
} else if (file.indexOf('array-notation') >= 0) {
29-
result = parser.toJson(data2, {arrayNotation: true});
30-
} else {
31-
result = parser.toJson(data2, {trim: false});
32-
}
33-
34-
var jsonFile = basename + '.json';
35-
var expected = fs.readFileSync(fixturesPath + '/' + jsonFile) + '';
36-
37-
if (expected) {
38-
expected = expected.trim();
39-
}
40-
// console.log('============ Got ===============');
41-
// console.log(result);
42-
// console.log('============ Expected ===============');
43-
// console.log(expected)
44-
// console.log('=====================================');
45-
assert.deepEqual(result, expected, jsonFile + ' and ' + file + ' are different');
46-
console.log('[xml2json: ' + file + '->' + jsonFile + '] passed!');
47-
} else if( ext == '.json') {
48-
var basename = path.basename(file, '.json');
49-
if (basename.match('reversible')) {
50-
var data = fs.readFileSync(fixturesPath + '/' + file);
51-
var result = parser.toXml(data);
52-
53-
var xmlFile = basename.split('-')[0] + '.xml';
54-
var expected = fs.readFileSync(fixturesPath + '/' + xmlFile) + '';
55-
56-
if (expected) {
57-
expected = expected.trim();
58-
}
59-
//console.log(result + '<---');
60-
assert.deepEqual(result, expected, xmlFile + ' and ' + file + ' are different');
61-
console.log('[json2xml: ' + file + '->' + xmlFile + '] passed!');
62-
}
63-
}
64-
}
6+
var Code = require('code');
7+
var Lab = require('lab');
8+
9+
10+
// Test shortcuts
11+
12+
var lab = exports.lab = Lab.script();
13+
var expect = Code.expect;
14+
var describe = lab.describe;
15+
var it = lab.test;
16+
17+
var internals = {};
18+
19+
20+
describe('xml2json', function () {
21+
22+
it('converts with array-notation', function (done) {
23+
24+
var xml = internals.readFixture('array-notation.xml');
25+
var result = parser.toJson(xml, { arrayNotation: true });
26+
var json = internals.readFixture('array-notation.json');
27+
28+
expect(result).to.deep.equal(json);
29+
30+
done();
31+
});
32+
33+
it('coerces', function (done) {
34+
35+
var xml = internals.readFixture('coerce.xml');
36+
var result = parser.toJson(xml, { coerce: false });
37+
var json = internals.readFixture('coerce.json');
38+
39+
expect(result + '\n').to.deep.equal(json);
40+
41+
done();
42+
});
43+
44+
it('handles domain', function (done) {
45+
46+
var xml = internals.readFixture('domain.xml');
47+
var result = parser.toJson(xml, { coerce: false });
48+
var json = internals.readFixture('domain.json');
49+
50+
expect(result + '\n').to.deep.equal(json);
51+
52+
done();
53+
});
54+
55+
it('does large file', function (done) {
56+
57+
var xml = internals.readFixture('large.xml');
58+
var result = parser.toJson(xml, { coerce: false, trim: true, sanitize: false });
59+
var json = internals.readFixture('large.json');
60+
61+
expect(result + '\n').to.deep.equal(json);
62+
63+
done();
64+
});
65+
66+
it('handles reorder', function (done) {
67+
68+
var xml = internals.readFixture('reorder.xml');
69+
var result = parser.toJson(xml, {});
70+
var json = internals.readFixture('reorder.json');
71+
72+
expect(result).to.deep.equal(json);
73+
74+
done();
75+
});
76+
77+
it('handles text with space', function (done) {
78+
79+
var xml = internals.readFixture('spacetext.xml');
80+
var result = parser.toJson(xml, { coerce: false, trim: false });
81+
var json = internals.readFixture('spacetext.json');
82+
83+
expect(result).to.deep.equal(json);
84+
85+
done();
86+
});
87+
88+
it('does xmlsanitize', function (done) {
89+
90+
var xml = internals.readFixture('xmlsanitize.xml');
91+
var result = parser.toJson(xml, {});
92+
var json = internals.readFixture('xmlsanitize.json');
93+
94+
expect(result).to.deep.equal(json);
95+
96+
done();
97+
});
98+
99+
it('throws error on bad options', function (done) {
100+
101+
var throws = function() {
102+
103+
var result = parser.toJson(xml, { derp: true});
104+
};
105+
106+
expect(throws).to.throw();
107+
done();
108+
});
109+
});
110+
111+
112+
describe('json2xml', function () {
113+
114+
it('converts domain to json', function (done) {
115+
116+
var json = internals.readFixture('domain-reversible.json');
117+
var result = parser.toXml(json);
118+
var xml = internals.readFixture('domain.xml');
119+
120+
expect(result+'\n').to.deep.equal(xml);
121+
122+
done();
123+
});
124+
65125
});
66126

127+
128+
internals.readFixture = function (file) {
129+
130+
return fs.readFileSync(__dirname + '/fixtures/' + file, { encoding: 'utf-8' });
131+
};

0 commit comments

Comments
 (0)