Registry / testing / create-validator-ts

create-validator-ts

JSON →
library6.0.1jsnpmunverified

Generates JSON Schema validators from TypeScript type definitions. Current version 6.0.1 ships ajv-based runtime validators that ensure type safety at runtime. Maintained actively with watch mode, caching, and custom code generation support. Differs from ts-json-schema-generator or typebox by producing standalone validator files alongside your types, enforcing single source of truth.

npm install create-validator-ts
INSTALL
IMPORT
SIG · CREATE-VALIDATOR-T
C
create-validator-ts
testingjavascriptv6.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

validateGetAPIRequestQuery
import { validateGetAPIRequestQuery } from './api-types.validator';
import validateGetAPIRequestQuery from './api-types.validator';
Generated validators are named exports, not default. Check generated file for exact names.
isGetAPIRequestQuery
import { isGetAPIRequestQuery } from './api-types.validator';
const { isGetAPIRequestQuery } = require('./api-types.validator');
ESM only; CommonJS require not supported in generated output.
Ajv
import Ajv from 'ajv';
import { Ajv } from 'ajv';
Ajv is a default export; named import causes runtime error.

Install package and peer dependency ajv, define TypeScript types, run CLI generator, then import and use generated validator function.

// Install yarn add create-validator-ts ajv // Create type file: src/api/api-types.ts export type GetAPIRequestQuery = { id: string }; export type GetAPIResponseBody = { ok: boolean }; // Generate validator // npx create-validator-ts "src/**/api-types.ts" // Use generated validator: src/api/api-types.validator.ts (auto-generated) import { validateGetAPIRequestQuery } from './api-types.validator'; const payload = { id: 'abc' }; try { const validated = validateGetAPIRequestQuery(payload); console.log('Valid:', validated); } catch (err) { console.error('Validation failed', err); }
Debug
Known issues
deprecatedGenerated validators throw generic Error instead of Ajv validation errors, making debugging hard.
fix
Catch errors and access the `errors` property; consider using custom generator to throw more detailed errors.
affects: >=1.0.0 <7.0.0
gotchaTypeScript types must be exported for the generator to process them. Non-exported types are ignored.
fix
Ensure all types used in validation are exported from the source file.
affects: >=1.0.0
gotchaGenerated validators use `removeAdditional: true` in Ajv config by default, silently dropping extra properties.
fix
To catch extra properties, modify generated code or use custom generator to change ajv options.
affects: >=1.0.0
breakingIn v5.0.0, the CLI input changed from files to glob patterns. Old `create-validator-ts file.ts` no longer works.
fix
Use glob pattern: `create-validator-ts "src/**/api-types.ts"` instead of listing files.
affects: >=5.0.0 <6.0.0
deprecatedGenerated validator file uses `@ts-nocheck` and disables eslint, bypassing type checks.
fix
Review generated code manually; consider submitting PR to remove these flags.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (intermediate value).validate is not a function
Imported the generated validator module incorrectly (named vs default export mismatch).
fix
Use named import: `import { validateMyType } from './types.validator';`
Check exact function name in generated file.
Error: invalid payload: GetAPIRequestQuery
Payload failed validation; Ajv errors not exposed by default.
fix
Catch error and log `error.errors` if available, or modify custom generator to add Ajv error details.
Cannot find module 'ajv'
Ajv peer dependency not installed.
fix
Install ajv: `npm install ajv` or `yarn add ajv`.
No files matched pattern 'src/**/api-types.ts'
Glob pattern does not match any files or wrong working directory.
fix
Verify pattern matches your TypeScript files: `ls src/**/api-types.ts`
Use `--cwd` option if needed.
create-validator-ts: command not found
CLI not installed globally or not referenced via npx.
fix
Use `npx create-validator-ts` or install globally: `npm install -g create-validator-ts`.
Upgrade
Version history
6.0.1latest on npm
Audit
Dependencies
ajvrequiredRequired runtime peer dependency for generated validators
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
create-validator-ts — npm install create-validator-ts · libregistry