From c48a60cce6b9497269d0361582517fb513f20e7f Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 7 May 2021 22:10:35 +0200 Subject: [PATCH] Bump superstruct (#9119) Co-authored-by: Philip Allgaier --- package.json | 2 +- src/common/structs/handle-errors.ts | 16 ++++++++++- src/common/structs/is-entity-id.ts | 28 +++++++------------ src/common/structs/is-icon.ts | 15 +++------- .../types/ha-automation-action-service.ts | 4 +-- .../hui-entities-card-editor.ts | 2 ++ .../hui-history-graph-card-editor.ts | 6 ++-- yarn.lock | 8 +++--- 8 files changed, 41 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 13524421fe..e281fc882d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/common/structs/handle-errors.ts b/src/common/structs/handle-errors.ts index 7aa8889045..96c82081c5 100644 --- a/src/common/structs/handle-errors.ts +++ b/src/common/structs/handle-errors.ts @@ -27,6 +27,20 @@ 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( @@ -34,7 +48,7 @@ export const handleStructError = ( "key", failure.path.join("."), "type_correct", - failure.type, + failure.refinement || failure.type, "type_wrong", JSON.stringify(failure.value) ) diff --git a/src/common/structs/is-entity-id.ts b/src/common/structs/is-entity-id.ts index c408187e84..8d71f01694 100644 --- a/src/common/structs/is-entity-id.ts +++ b/src/common/structs/is-entity-id.ts @@ -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); diff --git a/src/common/structs/is-icon.ts b/src/common/structs/is-icon.ts index 7ca4ff9ed5..db97d69ec2 100644 --- a/src/common/structs/is-icon.ts +++ b/src/common/structs/is-icon.ts @@ -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); diff --git a/src/panels/config/automation/action/types/ha-automation-action-service.ts b/src/panels/config/automation/action/types/ha-automation-action-service.ts index 4930c86b9b..175c11f9a5 100644 --- a/src/panels/config/automation/action/types/ha-automation-action-service.ts +++ b/src/panels/config/automation/action/types/ha-automation-action-service.ts @@ -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()), }); diff --git a/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts index f85a1e4530..3beb16952a 100644 --- a/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts @@ -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"; @@ -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()), diff --git a/src/panels/lovelace/editor/config-elements/hui-history-graph-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-history-graph-card-editor.ts index 4f2ee981e1..96bd9a4d5f 100644 --- a/src/panels/lovelace/editor/config-elements/hui-history-graph-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-history-graph-card-editor.ts @@ -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"; @@ -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({ diff --git a/yarn.lock b/yarn.lock index 1e0ff5bb52..5591afd29b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"