Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit e90832b

Browse files
committed
fix: loosen validation such that valid feeds don't fail
1 parent a0f7509 commit e90832b

File tree

5 files changed

+47
-93
lines changed

5 files changed

+47
-93
lines changed

lib/feed-schema.js

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,23 @@
22

33
const Joi = require('joi');
44

5-
module.exports = Joi.array()
6-
.label('feeds')
7-
.items([
8-
Joi.object()
9-
.keys({
10-
entry: Joi.boolean().required(),
11-
id: Joi.alternatives()
12-
.try(Joi.number(), Joi.string())
13-
.required(),
14-
file: Joi.string().required(),
15-
source: Joi.string().required(),
16-
deps: Joi.object().required(),
17-
expose: Joi.any().optional(),
18-
order: Joi.any().optional(),
19-
index: Joi.any().optional(),
20-
indexDeps: Joi.any().optional(),
21-
})
22-
.label('feed')
23-
.required(),
24-
Joi.object()
25-
.keys({
26-
id: Joi.alternatives()
27-
.try(Joi.number(), Joi.string())
28-
.required(),
29-
file: Joi.string().required(),
30-
source: Joi.string().required(),
31-
deps: Joi.object().required(),
32-
expose: Joi.any().optional(),
33-
order: Joi.any().optional(),
34-
index: Joi.any().optional(),
35-
indexDeps: Joi.any().optional(),
36-
})
37-
.label('feed')
38-
.optional(),
39-
])
5+
module.exports = Joi.object()
6+
.keys({
7+
entry: Joi.boolean().label('entry'),
8+
id: Joi.alternatives()
9+
.try(Joi.number(), Joi.string())
10+
.required()
11+
.label('id'),
12+
file: Joi.string()
13+
.required()
14+
.label('file'),
15+
source: Joi.string()
16+
.required()
17+
.label('source'),
18+
deps: Joi.object()
19+
.required()
20+
.label('deps'),
21+
})
22+
.unknown()
23+
.label('feed item')
4024
.required();

lib/reader.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const { directory } = require('tempy');
1010
const Joi = require('joi');
1111
const feedSchema = require('./feed-schema');
1212
const uglify = require('uglify-es');
13+
const Boom = require('boom');
14+
const assert = require('assert');
1315

1416
/**
1517
* Unpacks given feeds into directory structure before
@@ -20,13 +22,25 @@ const uglify = require('uglify-es');
2022
* @returns {Promise<string>} - promise resolves to a javascript bundle
2123
*/
2224
module.exports = async (feeds, options) => {
23-
Joi.assert(
24-
feeds,
25-
Joi.array()
26-
.items(feedSchema)
27-
.required(),
28-
`Expected argument 'feeds' to be an array of feeds.`
25+
assert(
26+
Array.isArray(feeds),
27+
`Expected argument 'feeds' to be an array (of feed arrays).`
2928
);
29+
assert(
30+
feeds.length > 0,
31+
`Expected argument 'feeds' (array) to be contain at least 1 feed (array).`
32+
);
33+
assert(
34+
feeds[0].length > 0,
35+
'Expected at least 1 feed (array) to have more than one entry (object).'
36+
);
37+
[].concat(...feeds).forEach(feedItem => {
38+
const { error } = Joi.validate(feedItem, feedSchema);
39+
if (error)
40+
throw Boom.boomify(error, {
41+
message: `Expected every item in argument 'feeds' (array) to be a valid feed (object).`,
42+
});
43+
});
3044

3145
const opts = { sourceMaps: false, minify: false, ...options };
3246

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"license": "MIT",
2828
"dependencies": {
2929
"asset-pipe-common": "^1.0.0-beta.6",
30+
"boom": "^7.1.1",
3031
"browserify": "^15.2.0",
3132
"fs-extra": "^5.0.0",
3233
"joi": "^13.1.1",

test/__snapshots__/reader.test.js.snap

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -58949,63 +58949,19 @@ exports[`should dedupe 1`] = `
5894958949
"
5895058950
`;
5895158951

58952-
exports[`should error if any feed content keys are missing 1`] = `
58953-
"Expected argument 'feeds' to be an array of feeds. [
58954-
{}
58955-
]
58956-

58957-
[1] \\"feeds\\" must be an array"
58958-
`;
58952+
exports[`should error if any feed content keys are missing 1`] = `"Expected at least 1 feed (array) to have more than one entry (object)."`;
5895958953

58960-
exports[`should error if any feed content keys are missing 2`] = `
58961-
"Expected argument 'feeds' to be an array of feeds. [
58962-
{
58963-
\\"file\\": \\"asd\\",
58964-
\\"deps\\": {},
58965-
\\"id\\": \\"a\\",
58966-
\\"entry\\": true
58967-
}
58968-
]
58969-

58970-
[1] \\"feeds\\" must be an array"
58971-
`;
58954+
exports[`should error if any feed content keys are missing 2`] = `"Expected at least 1 feed (array) to have more than one entry (object)."`;
5897258955

58973-
exports[`should error if feed content does not contain at least 1 entrypoint 1`] = `
58974-
"Expected argument 'feeds' to be an array of feeds. [
58975-
{
58976-
\\"id\\": \\"a\\",
58977-
\\"file\\": \\"asdas.js\\",
58978-
\\"source\\": \\"spy(1234);\\",
58979-
\\"deps\\": {}
58980-
}
58981-
]
58982-

58983-
[1] \\"feeds\\" must be an array"
58984-
`;
58956+
exports[`should error if feed content does not contain at least 1 entrypoint 1`] = `"Expected at least 1 feed (array) to have more than one entry (object)."`;
5898558957

58986-
exports[`should error if feed content is non object 1`] = `
58987-
"Expected argument 'feeds' to be an array of feeds. [
58988-
\\"s1df3s1f2d3s1d2f3s.json\\"
58989-
]
58990-

58991-
[1] \\"feeds\\" must be an array"
58992-
`;
58958+
exports[`should error if feed content is non object 1`] = `"Expected every item in argument 'feeds' (array) to be a valid feed (object).: \\"feed item\\" must be an object"`;
5899358959

58994-
exports[`should error if no feed content 1`] = `"Expected argument 'feeds' to be an array of feeds. \\"value\\" is required"`;
58960+
exports[`should error if no feed content 1`] = `"Expected argument 'feeds' to be an array (of feed arrays)."`;
5899558961

58996-
exports[`should error if no feed content 2`] = `
58997-
"Expected argument 'feeds' to be an array of feeds. []
58998-

58999-
[1] \\"value\\" does not contain [feeds]"
59000-
`;
58962+
exports[`should error if no feed content 2`] = `"Expected argument 'feeds' (array) to be contain at least 1 feed (array)."`;
5900158963

59002-
exports[`should error if no feed content 3`] = `
59003-
"Expected argument 'feeds' to be an array of feeds. [
59004-
[]
59005-
]
59006-

59007-
[1] \\"feeds\\" does not contain [feed]"
59008-
`;
58964+
exports[`should error if no feed content 3`] = `"Expected at least 1 feed (array) to have more than one entry (object)."`;
5900958965

5901058966
exports[`should handle node_modules dependencies 1`] = `
5901158967
"(function e(t, n, r) {

0 commit comments

Comments
 (0)