Skip to content

Commit

Permalink
Validate as sb3 first
Browse files Browse the repository at this point in the history
A vast majority of projects we get are sb3, so try that first
Technically this might change behavior if a project is both, but that seems like an edge case not worth worrying about
As part of this the sb2 schema is now compiled only if sb3 validation failed, which should slightly help loading performance
  • Loading branch information
GarboMuffin committed Jan 11, 2024
1 parent d651c39 commit d2505b9
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ var sprite3Schema = require('./sprite3_schema.json');
ajv.addSchema(sb2Defs).addSchema(sb3Defs);

module.exports = function (isSprite, input, callback) {
var validateSb2 = ajv.compile(isSprite ? sprite2Schema : sb2Schema);
var validateSb3 = ajv.compile(isSprite ? sprite3Schema : sb3Schema);
var isValidSb3 = validateSb3(input);
if (isValidSb3) {
input.projectVersion = 3;
return callback(null, input);
}

var validateSb2 = ajv.compile(isSprite ? sprite2Schema : sb2Schema);
var isValidSb2 = validateSb2(input);

if (isValidSb2) {
input.projectVersion = 2;
return callback(null, input);
}

var isValidSb3 = validateSb3(input);
if (isValidSb3) {
input.projectVersion = 3;
return callback(null, input);
}

var validationErrors = {
validationError: 'Could not parse as a valid SB2 or SB3 project.',
sb3Errors: validateSb3.errors,
Expand Down

0 comments on commit d2505b9

Please sign in to comment.