Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unknown Item Type Specification #656

Merged
merged 18 commits into from
Mar 12, 2024
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/renderer/src/stories/JSONSchemaInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { JSONSchemaForm, getIgnore } from "./JSONSchemaForm";
import { Search } from "./Search";
import tippy from "tippy.js";
import { merge } from "./pages/utils";
import { InspectorListItem } from "./preview/inspector/InspectorList";

const isDevelopment = !!import.meta.env;

const dateTimeRegex = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/;

Expand Down Expand Up @@ -502,6 +505,27 @@ export class JSONSchemaInput extends LitElement {
required = false;
validateOnChange = true;

// Print the default value of the schema if not caught
onUncaughtSchema = (schema) => {
// In development, show uncaught schemas
if (!isDevelopment) {
if (this.form) {
const inputContainer = this.form.shadowRoot.querySelector(`#${this.path.slice(-1)[0]}`);
inputContainer.style.display = "none";
}
}

if (schema.default) return `<pre>${JSON.stringify(schema.default, null, 2)}</pre>`;

const error = new InspectorListItem({
message:
"<h3 style='margin: 0'>Internal GUIDE Error</h3><span>Cannot render this property because of a misformatted schema.</span>",
});
error.style.width = "100%";

return error;
};

constructor(props) {
super();
Object.assign(this, props);
Expand Down Expand Up @@ -870,7 +894,16 @@ export class JSONSchemaInput extends LitElement {
if (isArray) {
const hasItemsRef = "items" in schema && "$ref" in schema.items;
if (!("items" in schema)) schema.items = {};
if (!("type" in schema.items) && !hasItemsRef) schema.items.type = this.#getType(this.value?.[0]);
if (!("type" in schema.items) && !hasItemsRef) {
// Guess the type of the first item
if (this.value) {
const itemToCheck = this.value[0];
schema.items.type = itemToCheck ? this.#getType(itemToCheck) : "string";
}

// If no value, handle uncaught schema
else return this.onUncaughtSchema(schema);
}
}

const itemSchema = this.form?.getSchema ? this.form.getSchema("items", schema) : schema["items"];
Expand Down Expand Up @@ -1134,8 +1167,7 @@ export class JSONSchemaInput extends LitElement {
}
}

// Print out the immutable default value
return html`<pre>${schema.default ? JSON.stringify(schema.default, null, 2) : "No default value"}</pre>`;
return this.onUncaughtSchema(schema);
}
}

Expand Down
Loading