Registry / testing / suretype

suretype

JSON →
library3.3.1jsnpmunverified

Suretype is a ridiculously fast and type-safe JSON schema validator for TypeScript and JavaScript. Current stable version is 3.3.1 (pure ESM, Node >=14.13.1 or >=16.0.0). It compiles validator functions using Ajv, extracts JSON Schema, and deduces TypeScript types at compile-time. Key differentiators: ~70x faster than Joi, ~100x faster than Superstruct, ~2500x faster than Zod; supports full JSON Schema but nothing beyond; errors prettified with awesome-ajv-errors; integrates with typeconv for conversion to/from JSON Schema, Open API, TypeScript types, or GraphQL. Works in browsers.

npm install suretype
INSTALL
IMPORT
SIG · SURETYPE
S
suretype
testingjavascriptv3.3.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.

v
import { v } from 'suretype'
const { v } = require('suretype')
ESM-only since v3; cannot require() from CommonJS.
TypeOf
import type { TypeOf } from 'suretype'
import { TypeOf } from 'suretype'
TypeOf is a type-only export; use 'import type' for TypeScript.
compile
import { compile } from 'suretype'
import compile from 'suretype'
compile is a named export, not default.

Defines a user schema, compiles a validator, validates data, and logs result.

import { v, compile } from 'suretype'; const userSchema = v.object({ firstName: v.string().required(), lastName: v.string(), age: v.number().gte(21), }); type User = TypeOf<typeof userSchema>; // User is: { firstName: string; lastName?: string; age?: number; } const validate = compile(userSchema); const data = { firstName: 'Alice', age: 30 }; const result = validate(data); if (result.valid) { console.log('Valid user:', result.data); } else { console.error('Validation errors:', result.errors); }
Debug
Known issues
breakingVersion 3 is pure ESM; cannot be imported with require() from CommonJS.
fix
Use ES modules ("type": "module" in package.json) or dynamic import().
affects: >=3.0.0
breakingNode.js <14.13.1 is not supported; common patterns like require('suretype') will throw ERR_REQUIRE_ESM.
fix
Upgrade Node.js to ^14.13.1 || >=16.0.0.
affects: >=3.0.0
gotchaThe 'v' object is a singleton; modifying it globally may affect other validators.
fix
Avoid mutating 'v' directly; create local schemas instead.
affects: >=1.0.0
gotchaUnlike Joi or Zod, suretype only supports JSON Schema features; you cannot use custom validation logic like .custom() or .refine().
fix
Use Ajv's custom keyword extension if needed, but it's not directly exposed by suretype.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Using ESM import in a CommonJS project without proper configuration.
fix
Add "type": "module" to package.json or rename file to .mjs.
ERR_REQUIRE_ESM: require() of ES Module not supported
Trying to require('suretype') in a CommonJS module.
fix
Use dynamic import: const { v } = await import('suretype'); or switch to ES modules.
TypeError: v.object is not a function
Importing 'v' incorrectly (e.g., default import or destructuring from wrong path).
fix
Ensure you have the correct named import: import { v } from 'suretype';
Upgrade
Version history
3.3.1latest on npm
Audit
Dependencies
ajvrequiredUsed internally for fast validation compilation
Agent activity
10 hits · last 30 days
node
10
Resources
suretype — npm install suretype · libregistry