|
|
3 days ago | |
|---|---|---|
| .. | ||
| src | 35dec6a5bd Refactor NodeStack and NodePath (#275) | 1 year ago |
| test | e9d56c993f Rename Kinobi to Codama (#234) | 1 year ago |
| .gitignore | 586b3e6fb0 Add validators package (#7) | 1 year ago |
| .prettierignore | 760dda5775 Inline scripts in packages (#842) | 2 months ago |
| CHANGELOG.md | 36df852972 [1.x] Publish packages (#916) | 3 days ago |
| LICENSE | 1794ecdd3f Update license dates (#835) | 2 months ago |
| README.md | 35dec6a5bd Refactor NodeStack and NodePath (#275) | 1 year ago |
| package.json | 36df852972 [1.x] Publish packages (#916) | 3 days ago |
| tsconfig.declarations.json | 586b3e6fb0 Add validators package (#7) | 1 year ago |
| tsconfig.json | 760dda5775 Inline scripts in packages (#842) | 2 months ago |
| tsup.config.ts | 760dda5775 Inline scripts in packages (#842) | 2 months ago |
| vitest.config.mts | 760dda5775 Inline scripts in packages (#842) | 2 months ago |
This package offers a set of validation rules for Codama IDLs to ensure that they are correctly formatted.
pnpm install @codama/validators
[!NOTE] This package is included in the main
codamapackage. Meaning, you already have access to its content if you are installing Codama this way.> pnpm install codama > ``` ## Types ### `ValidationItem` A validation item describes a single piece of information — typically a warning or an error — about a node in the Codama IDL.ts type ValidationItem = {
// The level of importance of a validation item. level: 'debug' | 'trace' | 'info' | 'warn' | 'error'; // A human-readable message describing the issue or information. message: string; // The node that the validation item is related to. node: Node; // The path of nodes that led to the node above (including the node itself). path: NodePath;};
## Functions ### `getValidationItemsVisitor(visitor)` The `getValidationItemsVisitor` function returns a visitor that collects all validation items from a Codama IDL. Note that this visitor is still a work in progress and does not cover all validation rules.ts import { getValidationItemsVisitor } from '@codama/validators';
const validationItems = codama.accept(getValidationItemsVisitor());
### `throwValidatorItemsVisitor(visitor)`
The `throwValidatorItemsVisitor` function accepts a `Visitor<ValidationItemp[]>` and throws an error if any validation items above a certain level are found. By default, the level is set to `'error'` but a second argument can be passed to change it.
ts import { throwValidatorItemsVisitor, getValidationItemsVisitor } from '@codama/validators';
// Throw if any "error" items are found. codama.accept(throwValidatorItemsVisitor(getValidationItemsVisitor()));
// Throw if any "warn" or "error" items are found. codama.accept(throwValidatorItemsVisitor(getValidationItemsVisitor(), 'warn')); ```