fix: Match state trigger/condition semantics to HA (#2565)

* Closes #2530
This commit is contained in:
Dermot Duffy
2026-07-04 10:23:51 -07:00
committed by GitHub
parent 3494706225
commit 9f956fe89f
15 changed files with 825 additions and 69 deletions
+14 -1
View File
@@ -42,7 +42,10 @@ export function arrayMove(target: unknown[], from: number, to: number): unknown[
}
/**
* Convert a value to an array if it is not already one.
* Convert a value to an array if it is not already one, dropping falsy inputs
* (`undefined`/`null`/`0`/`false`/`''`) to an empty array. Use when an absent or
* empty value should become `[]`; use `arrayifyWithFalsy` when falsy values are
* significant and must be preserved.
* @param value: A value (which may be an array).
* @returns An array.
*/
@@ -50,6 +53,16 @@ export const arrayify = <T>(value?: T | T[]): T[] => {
return value ? (Array.isArray(value) ? value : [value]) : [];
};
/**
* Wrap a value in an array if it is not already one, preserving the value --
* including falsy ones like `0`, `false`, `''` and `null`. Contrast with
* `arrayify`, which instead drops all falsy inputs to `[]`.
* @param value A value (which may be an array).
* @returns An array.
*/
export const arrayifyWithFalsy = <T>(value: T | T[]): T[] =>
Array.isArray(value) ? value : [value];
/**
* Convert a value to an set if it is not already one.
* @param value: A value (which may be a set, an array or a T)
+27
View File
@@ -0,0 +1,27 @@
import type { z } from 'zod';
/**
* Validate `value` against `schema` and copy any issues it produces into a
* refinement context, prefixing each issue's path with `path`. Use inside a
* `.superRefine` to delegate a value to another schema while preserving Zod's
* own error messages -- e.g. when the outer schema widened a field (to
* `z.unknown()`) and needs to re-apply the original, narrower schema
* conditionally. Adds nothing when `value` satisfies `schema`.
* @param ctx The refinement context to add issues to.
* @param value The value to validate.
* @param schema The schema to validate `value` against.
* @param path A path prefix prepended to each forwarded issue's path.
*/
export const forwardIssues = (
ctx: z.RefinementCtx,
value: unknown,
schema: z.ZodType,
path: readonly PropertyKey[] = [],
): void => {
const result = schema.safeParse(value);
if (!result.success) {
for (const issue of result.error.issues) {
ctx.addIssue({ ...issue, path: [...path, ...issue.path] });
}
}
};