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
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
},
"dependencies": {
"ajv": "^8.20.0",
"ajv-formats": "^3.0.1",
"chokidar": "^5.0.0",
"commander": "^15.0.0",
"fast-glob": "^3.3.3",
Expand Down
4 changes: 4 additions & 0 deletions src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { basename, dirname, extname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { AnySchemaObject, ValidateFunction } from 'ajv';
import Ajv2020 from 'ajv/dist/2020';
import addFormats from 'ajv-formats';
import JSON5 from 'json5';
import { SCHEMA_URI_SCHEME } from '../constants';
import { shortenPluginName } from '../plugins/util';
Expand Down Expand Up @@ -272,6 +273,9 @@ function compileValidator(
schemaRefRegistry: Map<string, AnySchemaObject>,
): ValidateFunction {
const ajv = new Ajv2020();
// ajv-formats ships its own nested ajv type declaration; runtime instance is compatible.
addFormats(ajv as unknown as Parameters<typeof addFormats>[0]);
// Custom format overrides take precedence over ajv-formats defaults (e.g. strict ISO date-time).
ajv.addFormat('path', (value: string) => value.length > 0 && !value.includes('\0'));
ajv.addFormat('date', isDate);
ajv.addFormat('date-time', (value: string) => isDateTime(value, true));
Expand Down
36 changes: 35 additions & 1 deletion tests/validate/format-validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { describe, expect, it } from 'bun:test';
import { afterEach, describe, expect, it } from 'bun:test';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createValidator } from '../../src/schema/schema';

Expand Down Expand Up @@ -50,3 +52,35 @@ describe('format: "wikilink"', () => {
expect(validateNode({ ...base, related: '[[]]' })).toBe(false);
});
});

describe('standard JSON Schema formats (ajv-formats)', () => {
const dirs: string[] = [];
afterEach(() => {
for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
});

it('compiles and validates standard formats like uri and email alongside custom formats', () => {
const dir = mkdtempSync(join(tmpdir(), 'sctx-fmt-'));
dirs.push(dir);
const schemaPath = join(dir, 'formats.json');
writeFileSync(
schemaPath,
JSON.stringify({
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: {
homepage: { type: 'string', format: 'uri' },
contact: { type: 'string', format: 'email' },
updated: { type: 'string', format: 'date-or-date-time' },
},
}),
);

const validate = createValidator(schemaPath);
expect(
validate({ homepage: 'https://example.com/docs', contact: 'user@example.com', updated: '2026-03-31T09:15' }),
).toBe(true);
expect(validate({ homepage: 'not a uri' })).toBe(false);
expect(validate({ contact: 'not-an-email' })).toBe(false);
});
});