fix: Match state trigger/condition semantics to HA (#2565)
* Closes #2530
This commit is contained in:
+14
-1
@@ -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)
|
||||
|
||||
@@ -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] });
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user