Skip to content

Commit

Permalink
Fix errors caused by variable or list values that are not Scratch-com…
Browse files Browse the repository at this point in the history
…patible

#8
  • Loading branch information
GarboMuffin committed Aug 23, 2023
1 parent 099d574 commit 761d119
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ <h2>Which errors does this fix?</h2>
<li><code>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'"}]}</code></li>
<li><code>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'"}]}</code></li>
<li><code>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'"}]}</code></li>
<li><code>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"}...}</code></li>
</ul>
<p>The best way to see if you're in luck is to just try it.</p>

Expand Down
Binary file added samples/invalid-variable-values.sb3
Binary file not shown.
41 changes: 39 additions & 2 deletions sb3fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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);
}
}
};

/**
Expand Down

1 comment on commit 761d119

@NguyennbVietNam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am new to here. Please guide me step by step

Please sign in to comment.