Allow syntactic sugar in config.

This commit is contained in:
Dermot Duffy
2021-11-10 21:09:17 -08:00
parent 19291ae184
commit f84cbcc50d
2 changed files with 11 additions and 9 deletions
+9 -9
View File
@@ -377,7 +377,7 @@ export class FrigateCard extends LitElement {
* @param error The ZodError object from parsing. * @param error The ZodError object from parsing.
* @returns An array of string error paths. * @returns An array of string error paths.
*/ */
protected _getParseErrorPaths<T>(error: z.ZodError<T>): string[] { protected _getParseErrorPaths<T>(error: z.ZodError<T>): Set<string> | null {
/* Zod errors involving unions are complex, as Zod may not be able to tell /* Zod errors involving unions are complex, as Zod may not be able to tell
* where the 'real' error is vs simply a union option not matching. This * where the 'real' error is vs simply a union option not matching. This
* function finds all ZodError "issues" that don't have an error with 'type' * function finds all ZodError "issues" that don't have an error with 'type'
@@ -388,7 +388,7 @@ export class FrigateCard extends LitElement {
* exactly why (or rather Zod simply says it doesn't match any of the * exactly why (or rather Zod simply says it doesn't match any of the
* available unions). This usually suggests the user specified an incorrect * available unions). This usually suggests the user specified an incorrect
* type name entirely. */ * type name entirely. */
let contenders: string[] = []; const contenders = new Set<string>();
if (error && error.issues) { if (error && error.issues) {
for (let i = 0; i < error.issues.length; i++) { for (let i = 0; i < error.issues.length; i++) {
const issue = error.issues[i]; const issue = error.issues[i];
@@ -396,17 +396,17 @@ export class FrigateCard extends LitElement {
const unionErrors = (issue as z.ZodInvalidUnionIssue).unionErrors; const unionErrors = (issue as z.ZodInvalidUnionIssue).unionErrors;
for (let j = 0; j < unionErrors.length; j++) { for (let j = 0; j < unionErrors.length; j++) {
const nestedErrors = this._getParseErrorPaths(unionErrors[j]); const nestedErrors = this._getParseErrorPaths(unionErrors[j]);
if (nestedErrors.length) { if (nestedErrors && nestedErrors.size) {
contenders = contenders.concat(nestedErrors); nestedErrors.forEach(contenders.add, contenders);
} }
} }
} else if (issue.code == 'invalid_type') { } else if (issue.code == 'invalid_type') {
if (issue.path[issue.path.length - 1] == 'type') { if (issue.path[issue.path.length - 1] == 'type') {
return []; return null;
} }
contenders.push(this._getParseErrorPathString(issue.path)); contenders.add(this._getParseErrorPathString(issue.path));
} else if (issue.code != 'custom') { } else if (issue.code != 'custom') {
contenders.push(this._getParseErrorPathString(issue.path)); contenders.add(this._getParseErrorPathString(issue.path));
} }
} }
} }
@@ -454,8 +454,8 @@ export class FrigateCard extends LitElement {
throw new Error( throw new Error(
upgradeMessage + upgradeMessage +
`${localize('error.invalid_configuration')}: ` + `${localize('error.invalid_configuration')}: ` +
(hint.length (hint && hint.size
? JSON.stringify(hint, null, ' ') ? JSON.stringify([...hint], null, ' ')
: localize('error.invalid_configuration_no_hint')), : localize('error.invalid_configuration_no_hint')),
); );
} }
+2
View File
@@ -103,6 +103,8 @@ const customActionSchema = z.object({
action: z.literal('fire-dom-event'), action: z.literal('fire-dom-event'),
}) })
export const frigateCardCustomActionSchema = customActionSchema.merge(z.object({ export const frigateCardCustomActionSchema = customActionSchema.merge(z.object({
// Syntactic sugar to avoid 'fire-dom-event' as part of an external API.
action: z.literal('fire-dom-event').or(z.literal('custom:frigate-card-action').transform(() => 'fire-dom-event')),
frigate_card_action: z.string(), frigate_card_action: z.string(),
})) }))
export type FrigateCardCustomAction = z.infer<typeof frigateCardCustomActionSchema>; export type FrigateCardCustomAction = z.infer<typeof frigateCardCustomActionSchema>;