scratch-parser/lib/validate.js
Karishma Chadha a74a97a906 feat($lib): Add sb3 schema for validation of Scratch 3.0 projects.
Added sb3 schema, renamed sb2 schema, and updated validate to check given input against both sb2 and
sb3 schemas. Validate appends a projectVersion to the given project with 2 or 3 if the given project
is valid against the corresponding schema. Validate now returns an object containing an error
message and both the sb2 and sb3 validation errors upon failure to validate against both schemas.

BREAKING CHANGE: Changes to validate API and what it returns (namely, error returned by validate is
no longer a string, but is now an object which contains an error message string as well as the sb2
errors and the sb3 errors). Users of this library function should expect to parse the given error to
figure out why validation failed.
2018-03-23 09:37:24 -04:00

29 lines
769 B
JavaScript

var ajv = require('ajv')();
var sb2schema = require('./sb2_schema.json');
var sb3schema = require('./sb3_schema.json');
module.exports = function (input, callback) {
var validateSb2 = ajv.compile(sb2schema);
var validateSb3 = ajv.compile(sb3schema);
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.',
sb2Errors: validateSb2.errors,
sb3Errors: validateSb3.errors
};
callback(validationErrors);
};