diff --git a/index.html b/index.html index 35f497a..09cb627 100644 --- a/index.html +++ b/index.html @@ -108,6 +108,7 @@
Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"enum","dataPath":".targets[0].name","schemaPath":"sb3_definitions.json#/definitions/stage/properties/name/enum","params":{"allowedValues":["Stage"]},"message":"should be equal to one of the allowed values"}],"sb2Errors":[{"keyword":"required","dataPath":"","schemaPath":"#/required","params":{"missingProperty":"objName"},"message":"should have required property 'objName'"}]}
Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"minItems","dataPath":".targets[0].costumes","schemaPath":"#/properties/costumes/minItems","params":{"limit":1},"message":"should NOT have less than 1 items"}],"sb2Errors":[{"keyword":"required","dataPath":"","schemaPath":"#/required","params":{"missingProperty":"objName"},"message":"should have required property 'objName'"}]}
Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"required","dataPath":".targets[0].costumes[0]","schemaPath":"#/required","params":{"missingProperty":"assetId"},"message":"should have required property 'assetId'"}],"sb2Errors":[{"keyword":"required","dataPath":"","schemaPath":"#/required","params":{"missingProperty":"objName"},"message":"should have required property 'objName'"}]}
Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"type","dataPath":".targets[0].variables['`jEk@4|i[#Fk?(8x)AV.-my variable'][1]","schemaPath":"#/definitions/stringOrNumber/oneOf/0/type","params":{"type":"string"},"message":"should be string"},{"keyword":"type","dataPath":".targets[0].variables['`jEk@4|i[#Fk?(8x)AV.-my variable'][1]","schemaPath":"#/definitions/stringOrNumber/oneOf/1/type","params":{"type":"number"},"message":"should be number"},{"keyword":"oneOf","dataPath":".targets[0].variables['`jEk@4|i[#Fk?(8x)AV.-my variable'][1]","schemaPath":"#/definitions/stringOrNumber/oneOf","params":{"passingSchemas":null},"message":"should match exactly one schema in oneOf"}...}
The best way to see if you're in luck is to just try it.
diff --git a/samples/invalid-variable-values.sb3 b/samples/invalid-variable-values.sb3 new file mode 100644 index 0000000..3ff11fc Binary files /dev/null and b/samples/invalid-variable-values.sb3 differ diff --git a/sb3fix.js b/sb3fix.js index 6ec5415..9acbd7e 100644 --- a/sb3fix.js +++ b/sb3fix.js @@ -237,7 +237,7 @@ var sb3fix = (function() { throw new Error('lists is not an object'); } for (const [listId, list] of Object.entries(lists)) { - fixVariableInPlace(listId, list); + fixListInPlace(listId, list); } }; @@ -311,13 +311,50 @@ var sb3fix = (function() { */ const fixVariableInPlace = (id, variable) => { if (!Array.isArray(variable)) { - throw new Error(`variable or list ${id} is not an array`); + throw new Error(`variable object ${id} is not an array`); } + const name = variable[0]; if (typeof name !== 'string') { log(`variable or list ${id} name was not a string`); variable[0] = String(variable[0]); } + + const value = variable[1]; + if (typeof value !== 'number' && typeof value !== 'string' && typeof value !== 'boolean') { + log(`variable ${id} value was not a Scratch-compatible value`); + variable[1] = String(variable[1]); + } + }; + + /** + * @param {string} id + * @param {unknown} list + */ + const fixListInPlace = (id, list) => { + if (!Array.isArray(list)) { + throw new Error(`list object ${id} is not an array`); + } + + const name = list[0]; + if (typeof name !== 'string') { + log(`list ${id} name was not a string`); + list[0] = String(list[0]); + } + + if (!Array.isArray(list[1])) { + log(`list ${id} value was not an array`); + list[1] = []; + } + + const listValue = list[1]; + for (let i = 0; i < listValue.length; i++) { + const value = listValue[i]; + if (typeof value !== 'number' && typeof value !== 'string' && typeof value !== 'boolean') { + log(`list ${id} index ${i} was not a Scratch-compatible value`); + listValue[i] = String(value); + } + } }; /**