# core/validators/panel.validator.ts

> 54 lines of code and 8 definitions.

Tree: Build tree
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/build#file-build-core-validators-panel-validator-ts
Source text: https://banes-lab.com/assets/sources/source.02ab4b91acc61860e6328b7a021411accab6323c0cbe9445b3dfc7e83d82695d.generated.txt

## Definitions

- `tagFindings` (lexical_declaration, line 17)
- `wordCount` (lexical_declaration, line 13)
- `duplicateTags` (lexical_declaration, line 34)
- `validatePanels` (lexical_declaration, line 48, exported)
- `messages` (lexical_declaration, line 21)
- `seen` (lexical_declaration, line 35)
- `twice` (lexical_declaration, line 36)
- `panelCount` (lexical_declaration, line 57, exported)

## Source

```typescript
import { CITE_CLOSE, CITE_OPEN, TAG_SEPARATOR, TAG_WORD_LIMIT } from "#configuration/constants/panel.constants";
import {
    PANEL_TAG_SEPARATED,
    PANEL_TAG_TOO_LONG,
    PANEL_TAG_TWICE,
    PANEL_UNCITED,
    PANEL_UNTAGGED,
} from "#configuration/strings/panel.strings";
import type { PanelSection, PayloadPanel } from "#types/panel.types";
import type { GraphFinding } from "#types/coverage.types";
import { WORD_SEPARATOR } from "#configuration/constants/leak.constants";

const wordCount = function wordCount(tag: string): number {
    return tag.split(WORD_SEPARATOR).filter((word) => word.length > 0).length;
};

const tagFindings = function tagFindings(section: PanelSection, panel: PayloadPanel): string[] {
    if (panel.tag === null) {
        return [PANEL_UNTAGGED];
    }
    const messages: string[] = [];
    if (wordCount(panel.tag) > TAG_WORD_LIMIT) {
        messages.push(PANEL_TAG_TOO_LONG + panel.tag);
    }
    if (panel.tag.includes(TAG_SEPARATOR)) {
        messages.push(PANEL_TAG_SEPARATED + panel.tag);
    }
    if (!section.prose.includes(CITE_OPEN + panel.tag + CITE_CLOSE)) {
        messages.push(PANEL_UNCITED + panel.tag);
    }
    return messages;
};

const duplicateTags = function duplicateTags(section: PanelSection): string[] {
    const seen = new Set<string>();
    const twice: string[] = [];
    for (const panel of section.panels) {
        if (panel.tag !== null && seen.has(panel.tag)) {
            twice.push(panel.tag);
        }
        if (panel.tag !== null) {
            seen.add(panel.tag);
        }
    }
    return twice;
};

export const validatePanels = function validatePanels(page: string, sections: readonly PanelSection[]): GraphFinding[] {
    return sections.flatMap((section) => [
        ...section.panels.flatMap((panel) =>
            tagFindings(section, panel).map((message) => ({ message, page, section: section.id })),
        ),
        ...duplicateTags(section).map((tag) => ({ message: PANEL_TAG_TWICE + tag, page, section: section.id })),
    ]);
};

export const panelCount = function panelCount(sections: readonly PanelSection[]): number {
    return sections.reduce((sum, section) => sum + section.panels.length, 0);
};
```
