Validation schema generation
createValidators accepts a list of files and project settings:
import path from 'node:path';
import { createValidators } from '@arfa-tools/generator/api-contract-check/builder';
const { result, deps } = createValidators(
[path.resolve('src/api/getUser.ts')],
{
tsconfigPath: 'tsconfig.json',
requestDeclaration: 'TypeRequest',
responseDeclaration: 'TypeResponse',
getDegraded: (names) => console.log(names),
}
);Input files must declare both root types locally using type or interface. They do not have to be exported. Files that declare neither the request nor the response type are skipped. If only one of the two types is declared, or no contracts are found (including an empty file list), the call fails with an error.
import type { TypeUser } from '../models/TypeUser';
type TypeRequest = { id: string; };
type TypeResponse = TypeUser;Types can reference named imports, including import type, re-exports, and export * from barrel files. Path aliases from tsconfig.json are supported.
Arguments
The first argument is input; the second is an object containing the remaining fields listed below.
ts | Absolute paths of the input files |
ts | The path to |
ts | The name of the local request type declaration, such as |
ts | The name of the local response type declaration, such as |
ts | Reads source files by absolute path. Returning |
ts | Receives sorted, unique names of properties or root declarations that fell back to |
Result
ts | Validation schemas for the contracts found. |
ts | A reverse dependency map: imported file → affected entry files. All paths in keys and values are absolute |
In-memory files
createValidators reads files from disk, from memory, or from both sources. Here is an example of reading contracts from memory:
const entryFilePath = '/virtual/api/getUser.ts';
const memory: Record<string, string> = {
[entryFilePath]: `
import type { TypeUser } from '../models/TypeUser';
type TypeRequest = { id: string };
type TypeResponse = TypeUser;
`,
'/virtual/models/TypeUser.ts': 'export type TypeUser = { name: string };',
};
const { result } = createValidators(
[entryFilePath],
{ ...options, readFile: (filePath) => memory[filePath] }
);This lets you perform multiple transformations and file operations (for example, deriving request and response types from OpenAPI while api-contract-check generates validation schemas on the fly) and save everything to disk in a single batch.