Base CLI Schema

raw JSON →
0.1.19 verified Thu Apr 23 auth: no javascript abandoned

This package provides a schema definition and normalization utility specifically designed for the `base-cli` plugin. Its primary function is to process and normalize command-line argument values (`argv`) before they are consumed by `cli.process()` within a `base` application. It allows for defining custom fields with specified types (e.g., 'string') and provides a `normalize` function that applies these definitions, including custom normalizer functions, to an `argv` object. The package is currently at version 0.1.19 and was last published in May 2016. Given its age and low version number, the release cadence is effectively inactive, indicating it is no longer actively maintained. Key differentiators include its tight integration with the `base` framework and its focused purpose of defining and applying schemas for CLI arguments, offering a structured approach to argument normalization within the `base` ecosystem, unlike more general-purpose validation libraries.

error ReferenceError: require is not defined in ES module scope
cause Attempting to use `require('base-cli-schema')` (a CommonJS module) within an ES Module file (`.mjs` or `type: "module"` in `package.json`).
fix
Change your file to a CommonJS module (.js without type: "module") or use a tool like createRequire from the module module if you must mix ESM and CJS (e.g., import { createRequire } from 'module'; const require = createRequire(import.meta.url); const cliSchema = require('base-cli-schema');). Ideally, migrate to an ESM-compatible alternative.
error TypeError: app.use is not a function
cause The `base` instance (`app`) was not correctly initialized or the `base-cli` plugin was not properly `require`d and passed to `app.use()`.
fix
Ensure const Base = require('base'); and const cli = require('base-cli'); are present, and that app.use(cli()); is called on your new Base() instance before using cliSchema.
breaking This package, including its core dependency `base`, is CommonJS-only. It does not support ES Modules (ESM) and will cause runtime errors if directly imported or `require`d within an ESM context without specific transpilation or loader configurations.
fix Use a CommonJS environment or project. For ESM projects, consider alternative, more modern CLI argument parsing and schema validation libraries. If absolutely necessary, configure a CommonJS-to-ESM wrapper or bundler like Webpack/Rollup with appropriate plugins (e.g., `@rollup/plugin-commonjs`).
gotcha The package is effectively abandoned, with its last publish in 2016. This means there will be no new features, bug fixes, or security updates. It may not be compatible with newer Node.js versions or ecosystem changes.
fix Evaluate if the functionality can be replaced by a more actively maintained library or custom code. If continued use is critical, ensure thorough testing against your specific Node.js version and dependencies. Be aware of potential unpatched vulnerabilities.
gotcha Heavy reliance on the `base` framework, which itself is an older, less commonly used architecture in modern Node.js development. Understanding `base`'s plugin system is crucial for effective use of `base-cli-schema`.
fix Familiarize yourself with the `base` framework's documentation. Ensure all necessary `base` plugins (like `base-cli`) are correctly installed and used with `app.use()`.
npm install base-cli-schema
yarn add base-cli-schema
pnpm add base-cli-schema

Demonstrates initializing a `base` application, attaching `base-cli`, creating a `cliSchema` instance, defining a custom field with a normalizer, and using it to process a mock argv object.

const argv = require('minimist')(process.argv.slice(2));
const Base = require('base');
const cliSchema = require('base-cli-schema');
const cli = require('base-cli'); // Assuming base-cli is also required for app.use(cli())

const app = new Base();
app.use(cli()); // Attach the cli plugin to the base application

const schema = cliSchema(app);

// Define a custom field for demonstration
schema.field('foo', 'string', {
  normalize: function(val, key, config, schema) {
    // Example normalization: always return 'bar' for 'foo'
    return val ? val.toUpperCase() : 'bar';
  }
});

// Simulate argv input, e.g., 'node your-script.js --foo=test --other=value'
const mockArgv = { foo: 'hello', other: 'world' };

const obj = schema.normalize(mockArgv);
console.log(obj);
// Expected output: { foo: 'HELLO', other: 'world' } (if --foo=hello was passed)
// or { foo: 'BAR' } if --foo was not passed and 'bar' is default/fallback