Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/validate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { VerdictCacheMode } from '../judge/types';
import { readSpace } from '../read/read-space';
import { evaluateRules, type FailedNodes, type RuleEvaluationResult } from '../rule/gating';
import { filterRules, parseRuleFilter, type RuleFilter } from '../rule/rule-filter';
import { extractEntityInfo, typeValidatorFor } from '../schema/schema';
import { extractEntityInfo, resolveNodeType, typeValidatorFor } from '../schema/schema';
import { createSpaceContext } from '../space-context';
import { buildSpaceGraph, computeHierarchyDepths } from '../space-graph';
import type {
Expand Down Expand Up @@ -202,8 +202,9 @@ export async function validateNodes(
for (const node of nodes) {
// Check the type first: an unknown type reports one error instead of one per oneOf branch.
const nodeType = (node.schemaData as Record<string, unknown>).type as string | undefined;
const canonicalType = nodeType !== undefined ? resolveNodeType(nodeType, metadata.typeAliases) : undefined;
const at = node.source ? { source: node.source } : {};
if (nodeType !== undefined && validTypes.length > 0 && !validTypes.includes(nodeType)) {
if (canonicalType !== undefined && validTypes.length > 0 && !validTypes.includes(canonicalType)) {
const sortedTypes = [...validTypes].sort();
collected.add({
kind: 'schema',
Expand All @@ -217,15 +218,20 @@ export async function validateNodes(
}

// A valid type is checked against its own branch only, so errors come from that type alone.
const validate = (nodeType !== undefined && typeValidatorFor(schemaValidator, nodeType)) || schemaValidator;
if (validate(node.schemaData)) {
const validate =
(canonicalType !== undefined && typeValidatorFor(schemaValidator, canonicalType)) || schemaValidator;
const dataToValidate =
canonicalType !== undefined && canonicalType !== nodeType
? { ...(node.schemaData as Record<string, unknown>), type: canonicalType }
: node.schemaData;
if (validate(dataToValidate)) {
validCount++;
} else {
for (const { message, dedupeKey } of formatErrors(
validate.errors || [],
schema,
schemaRefRegistry,
node.schemaData as Record<string, unknown>,
dataToValidate as Record<string, unknown>,
)) {
collected.add({
kind: 'schema',
Expand Down
27 changes: 27 additions & 0 deletions tests/validate/validate-views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,31 @@ describe('validateFile with embedded nodes', () => {
'guide.md#Intro:rule:section-clear',
]);
});

it('validates nodes whose type is a $metadata.aliases alias against the canonical oneOf branch', async () => {
const dir = mkdtempSync(join(tmpdir(), 'sctx-val-alias-'));
dirs.push(dir);
const spaceDir = join(dir, 'wiki');
mkdirSync(spaceDir);
// knowledge_wiki.json aliases `study` -> `source`, whose schema branch has `type: { const: "source" }` and requires `url`
writeFileSync(
join(spaceDir, 'valid-study.md'),
'---\ntype: study\ntitle: Field Study\nurl: https://example.com/study\n---\n\nDetails.\n',
);
writeFileSync(join(spaceDir, 'missing-url.md'), '---\ntype: study\ntitle: Incomplete Study\n---\n');
writeFileSync(join(spaceDir, 'unknown-type.md'), '---\ntype: bogus\ntitle: Unknown\n---\n');

const config: Config = {
spaces: [{ name: 'wiki', path: spaceDir, schema: 'knowledge_wiki.json' }],
};
const context = createSpaceContext('wiki', config, { configDir: dir });
const result = await validateSpace(context);

const schemaIssues = result.issues.filter((i) => i.kind === 'schema');
expect(result.validCount).toBe(1);
expect(schemaIssues.map((i) => `${i.nodeId}:${i.message}`).sort()).toEqual([
expect.stringContaining("missing-url.md:root: must have required property 'url'"),
expect.stringContaining('unknown-type.md:Invalid type "bogus"'),
]);
});
});