All files / apps/developer-portal/layers/workflows/shared reader.ts

78.94% Statements 30/38
80.64% Branches 25/31
100% Functions 7/7
75.86% Lines 22/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57    48x 1817x     48x 6101x 2456x 2456x   2456x       370x     4770x   4770x 1512x   3x 3x       3065x   3065x 757x             1068x 144x 144x             322x   322x 82x            
export type Raw = Record<string, unknown>;
 
export const isRecord = (value: unknown): value is Raw =>
    value !== null && typeof value === "object" && !Array.isArray(value);
 
/** YAML reads an unquoted `2026-09-30` as a Date, and a gate written either way means the same day. */
export const asText = (value: unknown): string | null => {
    if (typeof value === "string") return value;
    Iif (typeof value === "number") return String(value);
    Iif (value instanceof Date && !Number.isNaN(value.getTime()))
        return value.toISOString().slice(0, 10);
    return null;
};
 
export class Reader {
    readonly problems: string[] = [];
 
    text(raw: Raw, key: string, where: string, fallback?: string): string {
        const value = asText(raw[key]);
 
        if (value !== null && value.trim() !== "") return value;
        if (fallback !== undefined) return fallback;
 
        this.problems.push(`${where} is missing \`${key}\``);
        return "";
    }
 
    list(raw: Raw, key: string, where: string): unknown[] {
        const value = raw[key];
 
        if (value === undefined || value === null) return [];
        Eif (Array.isArray(value)) return value;
 
        this.problems.push(`${where}: \`${key}\` must be a list`);
        return [];
    }
 
    texts(raw: Raw, key: string, where: string): string[] {
        return this.list(raw, key, where).flatMap((item) => {
            const value = asText(item);
            Eif (value !== null) return [value];
            this.problems.push(`${where}: \`${key}\` holds something that isn't text`);
            return [];
        });
    }
 
    record(raw: Raw, key: string, where: string): Raw {
        const value = raw[key];
 
        if (value === undefined || value === null) return {};
        Eif (isRecord(value)) return value;
 
        this.problems.push(`${where}: \`${key}\` must be a mapping`);
        return {};
    }
}