# core/validators/check.validator.ts

> 48 lines of code and 12 definitions.

Tree: GovLab Context
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/context#file-context-core-validators-check-validator-ts
Source text: https://banes-lab.com/source/context/core/validators/check.validator.ts.txt

Listed in [core/validators](https://banes-lab.com/api/source/context/core/validators.md), after [core/validators/architecture.validator.ts](https://banes-lab.com/source/context/core/validators/architecture.validator.ts.md) and before [core/validators/concept.validator.ts](https://banes-lab.com/source/context/core/validators/concept.validator.ts.md).

## Definitions

- `checkDeclarationDefectsOf` (lexical_declaration, line 29, exported)
- `ARCH_PREFIX` (lexical_declaration, line 8)
- `LEX_PREFIX` (lexical_declaration, line 9)
- `INVARIANT_PREFIX` (lexical_declaration, line 10)
- `isAntiPattern` (lexical_declaration, line 12)
- `isPositive` (lexical_declaration, line 16)
- `record` (lexical_declaration, line 18)
- `OF_KIND` (lexical_declaration, line 24)
- `resolves` (lexical_declaration, line 33, exported)
- `resolved` (lexical_declaration, line 37, exported)
- `declaringChecksOf` (lexical_declaration, line 44, exported)
- `declaring` (lexical_declaration, line 47, exported)

## Contained in

- [core/validators](https://banes-lab.com/anatomy/context/folder-context-core-validators.md)

## Used by

- [core/analyzers/check.analyzer.ts](https://banes-lab.com/source/context/core/analyzers/check.analyzer.ts.md)

## Linked from

- [core/analyzers](https://banes-lab.com/anatomy/context/folder-context-core-analyzers.md)

## Source

```typescript
import type { CheckDeclarationDefect, DeclarationField, DeclaredCheck } from "#types/check.types";
import { DECLARATION_FIELDS, INVARIANT_KIND, RECORD_KEY_SEPARATOR } from "#configuration/constants/check.constants";
import { COLLECTIONS } from "#configuration/constants/ontology.constants";
import type { Faces } from "#types/context.types";
import { NEGATIVE_KIND } from "#configuration/constants/architecture.constants";
import { collectionRefResolver } from "#core/resolvers/reference.resolver";

const ARCH_PREFIX = `${COLLECTIONS.architecture}${RECORD_KEY_SEPARATOR}`;
const LEX_PREFIX = `${COLLECTIONS.lexicon}${RECORD_KEY_SEPARATOR}`;
const INVARIANT_PREFIX = `${COLLECTIONS.reasoning}${RECORD_KEY_SEPARATOR}${INVARIANT_KIND}${RECORD_KEY_SEPARATOR}`;

const isAntiPattern = function isAntiPattern(faces: Faces, ref: string): boolean {
    return ref.startsWith(ARCH_PREFIX) && faces.arch.get(ref.slice(ARCH_PREFIX.length))?.type === NEGATIVE_KIND;
};

const isPositive = function isPositive(faces: Faces, ref: string): boolean {
    if (ref.startsWith(ARCH_PREFIX)) {
        const record = faces.arch.get(ref.slice(ARCH_PREFIX.length)) ?? null;
        return record !== null && record.type !== NEGATIVE_KIND;
    }
    return ref.startsWith(LEX_PREFIX) || ref.startsWith(INVARIANT_PREFIX);
};

const OF_KIND: Readonly<Record<DeclarationField, (faces: Faces, ref: string) => boolean>> = {
    detects: isAntiPattern,
    enforces: isPositive,
};

export const checkDeclarationDefectsOf = function checkDeclarationDefectsOf(
    faces: Faces,
    checks: readonly DeclaredCheck[],
): CheckDeclarationDefect[] {
    const resolves = collectionRefResolver(faces);
    return checks.flatMap((declared) =>
        DECLARATION_FIELDS.flatMap((field) =>
            declared[field].flatMap((ref) => {
                const resolved = resolves(ref);
                return resolved && OF_KIND[field](faces, ref) ? [] : [{ check: declared.check, field, ref, resolved }];
            }),
        ),
    );
};

export const declaringChecksOf = function declaringChecksOf(
    checks: readonly DeclaredCheck[],
): ReadonlyMap<string, readonly string[]> {
    const declaring = new Map<string, string[]>();
    for (const declared of checks) {
        for (const ref of DECLARATION_FIELDS.flatMap((field) => declared[field])) {
            declaring.set(ref, [...(declaring.get(ref) ?? []), declared.check]);
        }
    }
    return declaring;
};
```
