Gubu is an object shape validation utility for JavaScript and TypeScript, designed to provide a more intuitive and less verbose developer experience compared to alternatives like Joi or JSON-Schema. Currently stable at version 9.0.0, the library differentiates itself with a "Schema By Example" approach, where validation schemas closely mirror the actual data structure. This design simplifies reading and reasoning about validation rules. A key feature is its ability to deeply fill out objects with default values if properties are missing, differing from shallow merge operations like `Object.assign`. It's crucial for developers to note that Gubu deliberately mutates the input object to inject these defaults, a design choice to simplify internal logic and delegate cloning decisions to the calling code. Gubu functions effectively in both browser and Node.js environments and ships with full TypeScript support.
npm install gubuVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates defining a Gubu schema with optional fields (having defaults), required fields (using type constructors like `Boolean`), array validation, and deep object merging. It also showcases how Gubu mutates the input object and catches various validation errors.
If immutability is required, clone the input object before passing it to Gubu (e.g., using `structuredClone` or `lodash.cloneDeep`).
Carefully review the schema definition: use literal values for optional fields with defaults, and type constructors (Number, String, Boolean, Object, Array) for strictly required fields. If a required field needs a default, consult Gubu's documentation for specific modifiers (e.g., `.Default()` if available).
Thoroughly read the release notes for the new major version on the package's GitHub repository or npm page. Test your application comprehensively after upgrading.
Ensure all required fields are present in the object provided for validation, or modify the schema to make the field optional with a default if applicable.
Correct the type of the value in the input object to align with the schema's expectation for that specific field.
For ESM, use `import { Gubu } from 'gubu'`. For CommonJS, use `const { Gubu } = require('gubu')`.No dependency data recorded yet.