Optimal is a JavaScript and TypeScript library designed for robust and type-safe schema definition, validation, and transformation of values. Currently at version 5.1.1, it provides a fluent, immutable API for constructing schemas that define object structures, configuration files, and validation fields. The library maintains a strong focus on performance and minimal footprint, boasting zero runtime dependencies and a small bundle size of just 5kB minified and gzipped. Its release cadence involves periodic major updates preceded by alpha versions, with minor patches addressing fixes. A key differentiator is its TypeScript-first approach, offering powerful inference and ensuring compile-time safety. It operates seamlessly in both Node.js environments (v12.17+) and modern browsers, providing features like recursive validation, automatic defaulting of missing fields, optional strictness for unknown fields, and support for complex logical operators (AND, OR, XOR) to combine schemas. This makes `optimal` suitable for defining and enforcing data contracts across various applications.
npm install optimalVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a complex object schema using `optimal`, including primitive types, arrays, default values, and custom validation predicates, then validates input data.
Update all module imports from `require('optimal')` to `import { ... } from 'optimal';`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` or `.mjs` file extension).Always assign the result of schema method calls back to a variable, e.g., `const newSchema = oldSchema.method();`.
Wrap multiple schema arguments in an array: `optimal.or([schema1, schema2])`.
Consult the official `optimal` v5 documentation (https://optimallib.dev) for updated usage patterns for defining object blueprints.
Replace `const optimal = require('optimal');` with `import { optimal, string } from 'optimal';` and ensure your project is configured for ESM.Examine the full error message, which typically indicates the specific field and the reason for validation failure. Adjust input data or schema definition.
Always capture the return value of schema methods: `const myStringSchema = string().notEmpty(); const myRequiredString = myStringSchema.required();`
No dependency data recorded yet.