Load a sample schema
JSON definition · shared/definitions/creator-load-save.json
View on GitHub
{
"title": "Support request",
"elements": [
{
"type": "dropdown",
"name": "product",
"title": "Product",
"choices": [
"Form Library",
"Survey Creator",
"Dashboard",
"PDF Generator"
]
},
{
"type": "comment",
"name": "issue",
"title": "Describe the issue",
"isRequired": true
},
{
"type": "radiogroup",
"name": "urgency",
"title": "How urgent is it?",
"choices": [
"Low",
"Normal",
"Blocking"
]
},
{
"type": "boolean",
"name": "callback",
"title": "Call me back"
}
]
}
Client · shared/client/lint-definition.js · sjs:lint-definition.client
View on GitHub
// Display the server's errors and warnings and return the actual save result to Creator
async function saveDefinition(definition) {
const response = await fetch(`/api/forms/${formId}`, {
method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(definition)
});
const report = response.status === 204 ? {} : await response.json();
reportFindings(report);
if (response.status === 422 && Array.isArray(report.errors) && report.errors.length) {
note("Form schema not saved. Fix the reported errors and retry.", "error");
return false;
}
if (!response.ok) throw new Error(report.error || "Couldn't save the form schema. Please try again.");
const count = report.warnings?.length ?? 0;
note(count ? `Schema saved with ${count} warning${count === 1 ? "" : "s"}. Review the findings above.` : "Form schema saved successfully.", "ok");
return true;
}
creator.saveSurveyFunc = async (saveNo, callback) => callback(saveNo, await checkAndSave(creator.JSON));
Server · app/routes/lint_definition.py · sjs:lint-definition.server
View on GitHub
# PUT /api/forms/{id} — lint the definition from Creator before storing it
@router.put("/api/forms/{id}", status_code=204, response_model=None)
async def save_linted_form(id: str, definition: JsonBody, user: CurrentUser, db: Db, surveyjs: SurveyJs) -> Response | None:
if not can(user, "edit-forms"):
raise HTTPException(403, "You don't have editor rights: only editors can change forms")
# The raw body keeps {} as {} (see Load and Save Form JSON Schemas)
lint = await surveyjs.post("/schema", definition, timeout=10)
answer: Any = None
with suppress(ValueError): # not JSON: fails closed below
answer = json.loads(lint.content)
if lint.status_code == 422 and isinstance(answer, dict) and {"errors", "warnings"} <= answer.keys():
return JSONResponse(answer, status_code=422) # errors: refuse the save, report them as they are
warnings_only = lint.status_code == 200 and isinstance(answer, dict) and list(answer) == ["warnings"]
if not warnings_only and (lint.status_code != 200 or answer != {}): # fail closed: nothing else is clean
surveyjs.unexpected("/schema", lint)
put(db, "forms", id, definition) # stored as it arrived: 1.0 stays 1.0
return JSONResponse({"warnings": answer["warnings"]}) if warnings_only else None # warnings don't block it