Registry / serialization / indicative-parser

indicative-parser

JSON →
library8.0.0jsnpmunverified

indicative-parser is a foundational utility within the Indicative validation ecosystem, primarily responsible for optimizing schema processing. It achieves this by pre-compiling Indicative's concise validation schemas into an efficient, recursive tree structure. This tree, composed of `object`, `array`, and `literal` nodes, significantly boosts validation performance by providing a pre-parsed, executable representation of the rules, thus avoiding redundant parsing during runtime. The current stable version is 8.0.0. The project maintains an active development pace with incremental updates and major version changes for significant architectural shifts, such as the recent migration of the 'typed schema' feature in v8.0.0 to the main `indicative` repository. Its key differentiator is providing a high-performance parsing layer for Indicative's declarative rule definitions.

npm install indicative-parser
INSTALL
IMPORT
SIG · INDICATIVE-PARSER
I
indicative-parser
serializationjavascriptv8.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rulesParser
import { rulesParser } from 'indicative-parser';
const { rulesParser } = require('indicative-parser');
The library primarily uses ES Modules. For CommonJS, you might need transpilation or a dynamic import, though a direct `require` might work if your bundler supports it. Always prefer `import`.
SchemaLiteral
import type { SchemaLiteral } from 'indicative-parser';
import { SchemaLiteral } from 'indicative-parser';
These are TypeScript types used for type-checking the parsed schema structure. They should be imported as types, not as runtime values.
typedSchema
import { typedSchema } from '@indicative/validator';
import { typedSchema } from 'indicative-parser';
As of v8.0.0, the `typedSchema` functionality was removed from `indicative-parser` and moved to the main `@indicative/validator` package. Attempting to import it from `indicative-parser` will result in a runtime error.

This quickstart demonstrates how to parse a validation schema string into Indicative's optimized tree structure using `rulesParser`. It logs the resulting tree and shows how to access specific parsed rules, including error handling for invalid schemas.

import { rulesParser, type SchemaLiteral, type SchemaObject, type ParsedRule } from 'indicative-parser'; interface UserData { username: string; email: string; accountType: 'email' | 'social'; address?: { street?: string; zip?: number; }; } // Define your validation schema string const validationSchemaString: { [key: string]: string } = { username: 'required|alpha|min:3|max:20', email: 'required|email', 'account.type': 'required|in:email,social', 'address.street': 'string', 'address.zip': 'number|min:10000|max:99999', }; try { // Parse the schema to get the optimized tree structure const parsedSchema = rulesParser(validationSchemaString); console.log('Successfully parsed schema tree:'); console.log(JSON.stringify(parsedSchema, null, 2)); // Example: Accessing specific parts of the parsed schema const usernameRules = (parsedSchema.username as SchemaLiteral)?.rules; if (usernameRules && usernameRules.length > 0) { console.log(`\nUsername rules found: ${usernameRules.map(r => r.name).join(', ')}`); } const accountTypeChildRules = ((parsedSchema.account as SchemaObject)?.children?.type as SchemaLiteral)?.rules; if (accountTypeChildRules) { console.log(`Account type rules: ${accountTypeChildRules.map(r => r.name).join(', ')}`); } } catch (error) { console.error('Error during schema parsing:', (error as Error).message); } // Demonstrating parsing an invalid rule (will throw an error) const malformedSchema = { productName: 'required|invalid_rule_name' }; try { rulesParser(malformedSchema); } catch (error) { console.error('\nAttempted to parse a malformed schema (expected error):', (error as Error).message); }
Debug
Known issues
breakingThe `typedSchema` feature, previously available in `indicative-parser`, has been completely removed in v8.0.0. This functionality is now integrated into the main `@indicative/validator` package.
fix
Migrate any usage of `typedSchema` to the `@indicative/validator` package. Ensure you are importing `typedSchema` from the correct new location.
affects: >=8.0.0
gotchaIn v7.1.4, the `readonly __opaque__` attribute was removed from opaque types in typed schemas. This change made the `props` returned by typed schemas mutable, altering a previous immutability guarantee.
fix
Review code that interacts with the `props` of typed schemas from versions before 7.1.4. If you relied on `props` being immutable, you may need to implement defensive copying or adjust logic for mutable data.
affects: >=7.1.4
gotchaAs of v1.0.1, `indicative-parser` automatically converts `snake_case` rule names (e.g., `is_string`) to `camelCase` (e.g., `isString`) during parsing. This is to ensure compatibility with internal Indicative rule handling.
fix
Be aware that rule names will be transformed. If you have custom rules or logic that relies on exact `snake_case` rule names after parsing, you will need to adjust your code to expect `camelCase`.
affects: >=1.0.1
gotchaThe parser's concept of a `literal` node type does not correspond to JavaScript literal values. A `literal` node in `indicative-parser` signifies a leaf node in the schema tree, meaning it has no further nested children.
fix
Understand that `literal` in this context refers to the structural role within the parsed schema tree, not the data type of the value being validated. Avoid conflating parser node types with JavaScript data types.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: rulesParser is not a function
Attempting to use CommonJS `require` syntax (`const { rulesParser } = require('indicative-parser');`) in an environment configured for ES Modules, or vice-versa, or using an outdated Node.js version that doesn't support ESM.
fix
For ES Module environments, use `import { rulesParser } from 'indicative-parser';`. If you are in a CommonJS environment and face this, ensure your project's `package.json` correctly defines `type: 'commonjs'` or that your bundler is configured to handle ESM imports correctly.
TypeError: indicative_parser_1.typedSchema is not a function
Attempting to import or use `typedSchema` directly from `indicative-parser` after upgrading to v8.0.0 or higher.
fix
The `typedSchema` functionality was moved. You must now import it from the main `@indicative/validator` package: `import { typedSchema } from '@indicative/validator';`.
Error: "UNKNOWN_RULE: Invalid rule 'invalid_rule_name'"
The schema contains a rule name that is not recognized by Indicative. This typically occurs with typos, custom rules not registered, or rules removed in newer versions.
fix
Check the spelling of your rule names against the official Indicative documentation. If it's a custom rule, ensure it's properly defined and registered with your Indicative validator instance (though `indicative-parser` itself doesn't register rules, it expects them to be known to the validator that will consume its output).
Upgrade
Version history
8.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
indicative-parser — npm install indicative-parser · libregistry