babel-plugin-spectypes

raw JSON →
2.1.11 verified Sat Apr 25 auth: no javascript

Babel plugin (v2.1.11) that compiles spectypes validators at build time, producing fast, eval-free runtime validation for TypeScript/JavaScript. Current stable version is 2.1.11 with bi-monthly releases. Key differentiators: compiles validators into simple if/else statements (no eval or new Function), can outperform ajv in benchmarks, detailed error messages with failure paths, TypeScript type inference, and extensive property-based testing. Requires @babel/core as a peer dependency and spectypes runtime package.

error Cannot find module 'spectypes'
cause Missing runtime dependency 'spectypes'.
fix
Run 'npm install spectypes' to install the runtime package.
error SyntaxError: Unexpected token import
cause Running code directly with Node.js without build step or using CommonJS require incorrectly.
fix
Ensure you are using a build step with Babel and Webpack/Rollup, or use CommonJS require correctly: const { array } = require('spectypes').
error ReferenceError: array is not defined
cause Forgetting to import validators from 'spectypes' or import path incorrect.
fix
Add 'import { array } from \"spectypes\"' at the top of your file.
error Error: Plugin babel-plugin-spectypes not found
cause The babel-plugin-spectypes package is not installed or not listed in Babel config correctly.
fix
Run 'npm install --save-dev babel-plugin-spectypes' and ensure your .babelrc or babel.config.js includes '\"plugins\": [\"babel-plugin-spectypes\"]'.
breaking Version 2.0.0 introduced major breaking changes; ensure your code is compatible before upgrading from v1.
fix Review the changelog and update your validator usage accordingly.
gotcha The plugin only transforms imports from the 'spectypes' package; custom validators or renamed imports require careful setup.
fix Ensure all validators are imported directly from 'spectypes' and that the Babel plugin is configured correctly.
deprecated Some older validators may be deprecated in future versions; check the documentation for updates.
fix Upgrade to the latest version and replace deprecated validators with their modern equivalents.
gotcha The plugin runs at build time and may not work correctly if used with other Babel transforms that modify imports before this plugin processes them.
fix Ensure babel-plugin-spectypes runs after any import transformation plugins, or use 'plugin ordering' to control execution order.
gotcha TypeScript users must ensure that Babel is configured to handle TypeScript (e.g., @babel/preset-typescript) for the plugin to process .ts files.
fix Add '@babel/preset-typescript' to your Babel presets if you are using TypeScript.
npm install babel-plugin-spectypes
yarn add babel-plugin-spectypes
pnpm add babel-plugin-spectypes

Shows installation, Babel configuration, and basic usage with array, number, and struct validators compiled by the plugin.

// Install: npm i spectypes && npm i -D babel-plugin-spectypes
// .babelrc
{
  "plugins": ["babel-plugin-spectypes"]
}

// source.ts
import { array, number, struct } from 'spectypes'

const NumberArray = array(number)

const User = struct({
  name: string(),
  age: number()
})

// The plugin transforms the variable declarations into compiled validators at build time.
// No runtime compilation needed.