Skip to content

Commit

Permalink
Run inputs through sb3fix if validate() fails
Browse files Browse the repository at this point in the history
Closes TurboWarp/scratch-vm#200
Some of the above tests that check for validate() rejecting invalid inputs would
ideally fail now, but sb3fix is still an experiment. We haven't seen projects that
are invalid in these ways in the wild so not a priority.
  • Loading branch information
GarboMuffin committed Mar 17, 2024
1 parent 70f6c9d commit 3a2d61e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var sprite2Schema = require('./sprite2_schema.json');
var sprite3Schema = require('./sprite3_schema.json');
ajv.addSchema(sb2Defs).addSchema(sb3Defs);

module.exports = function (isSprite, input, callback) {
var validate = function (isSprite, input, callback) {
var validateSb3 = ajv.compile(isSprite ? sprite3Schema : sb3Schema);
var isValidSb3 = validateSb3(input);
if (isValidSb3) {
Expand All @@ -30,3 +30,32 @@ module.exports = function (isSprite, input, callback) {

callback(validationErrors);
};

module.exports = function (isSprite, input, callback) {
// If validate fails, try using sb3fix to salvage the input
validate(isSprite, input, function (err, result) {
if (!err) {
callback(null, result);
return;
}

try {
// Load slightly lazily as this is an edge case
// eslint-disable-next-line global-require
var sb3fix = require('@turbowarp/sb3fix');

var fixed = sb3fix.fixJSON(input);
validate(isSprite, fixed, function (err2, result2) {
if (err2) {
// Original validation error will be most useful
callback(err);
} else {
callback(null, result2);
}
});
} catch (sb3fixError) {
// The original validation error will be most useful
callback(err);
}
});
};
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@turbowarp/json": "^0.1.1",
"@turbowarp/sb3fix": "^0.2.0",
"ajv": "6.3.0",
"jszip": "^3.10.1",
"pify": "4.0.1"
Expand Down
28 changes: 28 additions & 0 deletions test/unit/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,31 @@ test('sb3 errors listed before sb2 errors', function (t) {
t.end();
});
});

test('Uses sb3fix if validation fails', function (t) {
var invalidSprite = {
isStage: false,
name: 'Sprite1',
variables: {},
lists: {},
broadcasts: {},
blocks: {},
comments: {},
currentCostume: 0,
costumes: [], // should not be empty!
sounds: [],
volume: 100,
visible: true,
x: 0,
y: 0,
size: 100,
direction: 90,
draggable: false,
rotationStyle: 'all around'
};
validate(true, invalidSprite, function (err, res) {
t.equal(err, null);
t.equal(res.costumes.length, 1); // sb3fix will add a costume
t.end();
});
});

0 comments on commit 3a2d61e

Please sign in to comment.