Bump superstruct (#9119)

Co-authored-by: Philip Allgaier <mail@spacegaier.de>
This commit is contained in:
Bram Kragten 2021-05-07 22:10:35 +02:00 committed by GitHub
parent cd3ffceeff
commit c48a60cce6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 40 deletions

View File

@ -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",

View File

@ -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)
)

View File

@ -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);

View File

@ -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);

View File

@ -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()),
});

View File

@ -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()),

View File

@ -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({

View File

@ -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"