diff --git a/bun.lock b/bun.lock index b20f945a..473b3971 100644 --- a/bun.lock +++ b/bun.lock @@ -6,6 +6,7 @@ "name": "ost-tools", "dependencies": { "ajv": "^8.20.0", + "ajv-formats": "^3.0.1", "chokidar": "^5.0.0", "commander": "^15.0.0", "fast-glob": "^3.3.3", diff --git a/package.json b/package.json index 8cc2e6be..4d639548 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/schema/schema.ts b/src/schema/schema.ts index f3cf4cf2..8e7b4016 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -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'; @@ -272,6 +273,9 @@ function compileValidator( schemaRefRegistry: Map, ): ValidateFunction { const ajv = new Ajv2020(); + // ajv-formats ships its own nested ajv type declaration; runtime instance is compatible. + addFormats(ajv as unknown as Parameters[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)); diff --git a/tests/validate/format-validation.test.ts b/tests/validate/format-validation.test.ts index 370751a0..881feb6c 100644 --- a/tests/validate/format-validation.test.ts +++ b/tests/validate/format-validation.test.ts @@ -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'; @@ -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); + }); +});