Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"resize-observer-polyfill": "^1.5.1",
"roboto-fontface": "^0.10.0",
"sortablejs": "^1.10.2",
"superstruct": "^0.10.13",
"superstruct": "^0.15.2",
"tinykeys": "^1.1.1",
"tsparticles": "^1.19.2",
"unfetch": "^4.1.0",
Expand Down
16 changes: 15 additions & 1 deletion src/common/structs/handle-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,28 @@ export const handleStructError = (
failure.path.join(".")
)
);
} else if (failure.type === "union") {
continue;
} else if (failure.type === "enums") {
warnings.push(
hass.localize(
"ui.errors.config.key_wrong_type",
"key",
failure.path.join("."),
"type_correct",
failure.message.replace("Expected ", "").split(", ")[0],
"type_wrong",
JSON.stringify(failure.value)
)
);
} else {
warnings.push(
hass.localize(
"ui.errors.config.key_wrong_type",
"key",
failure.path.join("."),
"type_correct",
failure.type,
failure.refinement || failure.type,
"type_wrong",
JSON.stringify(failure.value)
)
Expand Down
28 changes: 10 additions & 18 deletions src/common/structs/is-entity-id.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import { struct, StructContext, StructResult } from "superstruct";
import { refine, string } from "superstruct";

const isEntityId = (value: unknown, context: StructContext): StructResult => {
if (typeof value !== "string") {
return [context.fail({ type: "string" })];
}
const isEntityId = (value: string): boolean => {
if (!value.includes(".")) {
return [
context.fail({
type: "Entity ID should be in the format 'domain.entity'",
}),
];
return false;
}
return true;
};

export const EntityId = struct("entity-id", isEntityId);
export const entityId = () =>
refine(string(), "entity ID (domain.entity)", isEntityId);

const isEntityIdOrAll = (
value: unknown,
context: StructContext
): StructResult => {
if (typeof value === "string" && value === "all") {
const isEntityIdOrAll = (value: string): boolean => {
if (value === "all") {
return true;
}

return isEntityId(value, context);
return isEntityId(value);
};

export const EntityIdOrAll = struct("entity-id-all", isEntityIdOrAll);
export const entityIdOrAll = () =>
refine(string(), "entity ID (domain.entity or all)", isEntityIdOrAll);
15 changes: 4 additions & 11 deletions src/common/structs/is-icon.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { struct, StructContext, StructResult } from "superstruct";
import { refine, string } from "superstruct";

const isIcon = (value: unknown, context: StructContext): StructResult => {
if (typeof value !== "string") {
return [context.fail({ type: "string" })];
}
const isIcon = (value: string) => {
if (!value.includes(":")) {
return [
context.fail({
type: "icon should be in the format 'mdi:icon'",
}),
];
return false;
}
return true;
};

export const Icon = struct("icon", isIcon);
export const icon = () => refine(string(), "icon (mdi:icon-name)", isIcon);
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { any, assert, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../../common/dom/fire_event";
import { ServiceAction } from "../../../../../data/script";
import type { HomeAssistant } from "../../../../../types";
import { EntityIdOrAll } from "../../../../../common/structs/is-entity-id";
import { entityIdOrAll } from "../../../../../common/structs/is-entity-id";
import { ActionElement } from "../ha-automation-action-row";
import "../../../../../components/ha-service-control";
import { hasTemplate } from "../../../../../common/string/has-template";

const actionStruct = object({
service: optional(string()),
entity_id: optional(EntityIdOrAll),
entity_id: optional(entityIdOrAll()),
target: optional(any()),
data: optional(any()),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
union,
} from "superstruct";
import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
import "../../../../components/entity/state-badge";
import "../../../../components/ha-card";
Expand Down Expand Up @@ -49,6 +50,7 @@ import { configElementStyle } from "./config-elements-style";
const cardConfigStruct = object({
type: string(),
title: optional(union([string(), boolean()])),
entity: optional(entityId()),
theme: optional(string()),
show_header_toggle: optional(boolean()),
state_color: optional(boolean()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { fireEvent } from "../../../../common/dom/fire_event";
import { HomeAssistant } from "../../../../types";
import { HistoryGraphCardConfig } from "../../cards/types";
import { EntityId } from "../../../../common/structs/is-entity-id";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../components/hui-entity-editor";
import { EntityConfig } from "../../entity-rows/types";
import { LovelaceCardEditor } from "../../types";
Expand All @@ -30,10 +30,10 @@ import { configElementStyle } from "./config-elements-style";

const entitiesConfigStruct = union([
object({
entity: EntityId,
entity: entityId(),
name: optional(string()),
}),
EntityId,
entityId(),
]);

const cardConfigStruct = object({
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12220,10 +12220,10 @@ subarg@^1.0.0:
dependencies:
minimist "^1.1.0"

superstruct@^0.10.13:
version "0.10.13"
resolved "https://registry.yarnpkg.com/superstruct/-/superstruct-0.10.13.tgz#705535a5598ff231bd976601a7b6b534a71a821b"
integrity sha512-W4SitSZ9MOyMPbHreoZVEneSZyPEeNGbdfJo/7FkJyRs/M3wQRFzq+t3S/NBwlrFSWdx1ONLjLb9pB+UKe4IqQ==
superstruct@^0.15.2:
version "0.15.2"
resolved "https://registry.yarnpkg.com/superstruct/-/superstruct-0.15.2.tgz#ab7fb84c455a9d7da84c11cfd82c85a4fee9dfff"
integrity sha512-OsJI8lv6/PsInwCf4ultejmsJYseYshKhkcbDendqNwj3MeGXENajl3ujMGMU4FDDSSeJTOwFn9T6pnfsS9DYA==

supports-color@6.0.0:
version "6.0.0"
Expand Down