{"id":13329,"library":"indicative-parser","title":"Indicative Schema Parser","description":"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.","status":"active","version":"8.0.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/poppinss/indicative-parser","tags":["javascript","indicative","validator","parser"],"install":[{"cmd":"npm install indicative-parser","lang":"bash","label":"npm"},{"cmd":"yarn add indicative-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add indicative-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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`.","wrong":"const { rulesParser } = require('indicative-parser');","symbol":"rulesParser","correct":"import { rulesParser } from 'indicative-parser';"},{"note":"These are TypeScript types used for type-checking the parsed schema structure. They should be imported as types, not as runtime values.","wrong":"import { SchemaLiteral } from 'indicative-parser';","symbol":"SchemaLiteral","correct":"import type { SchemaLiteral } from 'indicative-parser';"},{"note":"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.","wrong":"import { typedSchema } from 'indicative-parser';","symbol":"typedSchema","correct":"import { typedSchema } from '@indicative/validator';"}],"quickstart":{"code":"import { rulesParser, type SchemaLiteral, type SchemaObject, type ParsedRule } from 'indicative-parser';\n\ninterface UserData {\n  username: string;\n  email: string;\n  accountType: 'email' | 'social';\n  address?: { street?: string; zip?: number; };\n}\n\n// Define your validation schema string\nconst validationSchemaString: { [key: string]: string } = {\n  username: 'required|alpha|min:3|max:20',\n  email: 'required|email',\n  'account.type': 'required|in:email,social',\n  'address.street': 'string',\n  'address.zip': 'number|min:10000|max:99999',\n};\n\ntry {\n  // Parse the schema to get the optimized tree structure\n  const parsedSchema = rulesParser(validationSchemaString);\n  console.log('Successfully parsed schema tree:');\n  console.log(JSON.stringify(parsedSchema, null, 2));\n\n  // Example: Accessing specific parts of the parsed schema\n  const usernameRules = (parsedSchema.username as SchemaLiteral)?.rules;\n  if (usernameRules && usernameRules.length > 0) {\n    console.log(`\\nUsername rules found: ${usernameRules.map(r => r.name).join(', ')}`);\n  }\n\n  const accountTypeChildRules = ((parsedSchema.account as SchemaObject)?.children?.type as SchemaLiteral)?.rules;\n  if (accountTypeChildRules) {\n      console.log(`Account type rules: ${accountTypeChildRules.map(r => r.name).join(', ')}`);\n  }\n\n} catch (error) {\n  console.error('Error during schema parsing:', (error as Error).message);\n}\n\n// Demonstrating parsing an invalid rule (will throw an error)\nconst malformedSchema = {\n    productName: 'required|invalid_rule_name'\n};\n\ntry {\n    rulesParser(malformedSchema);\n} catch (error) {\n    console.error('\\nAttempted to parse a malformed schema (expected error):', (error as Error).message);\n}\n","lang":"typescript","description":"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."},"warnings":[{"fix":"Migrate any usage of `typedSchema` to the `@indicative/validator` package. Ensure you are importing `typedSchema` from the correct new location.","message":"The `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.","severity":"breaking","affected_versions":">=8.0.0"},{"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.","message":"In 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.","severity":"gotcha","affected_versions":">=7.1.4"},{"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`.","message":"As 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.","severity":"gotcha","affected_versions":">=1.0.1"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"TypeError: rulesParser is not a function"},{"fix":"The `typedSchema` functionality was moved. You must now import it from the main `@indicative/validator` package: `import { typedSchema } from '@indicative/validator';`.","cause":"Attempting to import or use `typedSchema` directly from `indicative-parser` after upgrading to v8.0.0 or higher.","error":"TypeError: indicative_parser_1.typedSchema is not a function"},{"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).","cause":"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.","error":"Error: \"UNKNOWN_RULE: Invalid rule 'invalid_rule_name'\""}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}