Save a JSON schema with
Source on GitHub
Definition: shared/definitions/creator-load-save.json
shared/client/lint-definition.js · sjs:lint-definition.client
View on GitHub
// Creator: surface the server's lint report instead of a silent failure
async function saveDefinition(definition) {
const res = await fetch(`/api/forms/${formId}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(definition)
});
const report = res.status === 204 ? {} : await res.json(); // 422 { errors, warnings } · 200 { warnings } · { error }
if (report.errors) creator.notify(report.errors.map(e => e.message).join("\n"), "error");
else if (report.warnings) creator.notify(report.warnings.map(e => e.message).join("\n"), "info");
else if (report.error) creator.notify(report.error, "error");
return res.ok;
}
creator.saveSurveyFunc = async (saveNo, callback) => callback(saveNo, await saveDefinition(creator.JSON));
routes/examples/lint-definition.php · sjs:lint-definition.server
View on GitHub
// PUT /api/forms/{id} — lint the definition from Creator before storing it
Route::put('/api/forms/{id}', function (Request $request, string $id) {
Gate::authorize('edit-forms');
$definition = json_decode($request->getContent(), flags: JSON_THROW_ON_ERROR); // the raw body keeps {} as {} (see Load and Save Form JSON Schemas)
$lint = SurveyJsService::post('/schema', $definition, timeout: 10);
if ($lint->status() === 422 && $lint->json('errors') !== null) { // errors: refuse the save, report them as they are
return response($lint->body(), 422)->header('Content-Type', 'application/json');
}
$warningsOnly = $lint->status() === 200 && array_keys((array) $lint->json()) === ['warnings'];
if (! $warningsOnly && ($lint->status() !== 200 || $lint->body() !== '{}')) { // fail closed: nothing else counts as clean
SurveyJsService::unexpected('/schema', $lint);
}
StoredJson::put('forms', $id, $definition); // stored as it arrived: 1.0 stays 1.0
return $warningsOnly ? response()->json(['warnings' => $lint->object()->warnings]) : response()->noContent(); // warnings don't block the save
});