|
| 1 | +--- |
| 2 | +seo: |
| 3 | + title: Lint Open-RPC with Redocly CLI |
| 4 | + description: Use the Redocly CLI to enforce basic validation, configure rules, or even build custom plugins for Open-RPC. |
| 5 | +--- |
| 6 | + |
| 7 | +# Lint Open-RPC with Redocly CLI |
| 8 | + |
| 9 | +{% admonition type="info" name="Experimental Open-RPC support" %} |
| 10 | +This feature is at an early stage, please use with caution and send us lots of feedback! |
| 11 | +{% /admonition %} |
| 12 | + |
| 13 | +In addition to providing lint functionality for multiple OpenAPI formats, Redocly CLI also has support for Open-RPC. |
| 14 | +Redocly CLI supports the following linting approaches with Open-RPC documents: |
| 15 | + |
| 16 | +- Open-RPC document validation. |
| 17 | +- Supported versions: |
| 18 | + - [Open-RPC 1.x](https://spec.open-rpc.org/) |
| 19 | +- Built-in rules for checking common standards requirements (see the [list of Open-RPC rules](#open-rpc-rules)). |
| 20 | +- [Configurable rules](../rules/configurable-rules.md) so that you can build your own rules following common patterns |
| 21 | +- [Custom plugins](../custom-plugins/index.md) for advanced users that need additional functionality |
| 22 | + |
| 23 | +## Lint an existing Open-RPC file |
| 24 | + |
| 25 | +Redocly CLI takes its settings from a `redocly.yaml` configuration file. |
| 26 | +Below is an example of a simple configuration file for validating an Open-RPC file is in the expected format: |
| 27 | + |
| 28 | +```yaml |
| 29 | +rules: |
| 30 | + struct: error |
| 31 | +``` |
| 32 | +
|
| 33 | +The empty `extends` element instructs Redocly CLI not to use any existing rulesets, but to display an error if the `struct` rule finds any problem. |
| 34 | +This rule checks that the document structure matches the Open-RPC specification. |
| 35 | + |
| 36 | +With this configuration file, and your Open-RPC description file, run the linting command: |
| 37 | + |
| 38 | +```sh |
| 39 | +redocly lint open-rpc.json |
| 40 | +``` |
| 41 | + |
| 42 | +The output describes any structural problems with the document, or reports that it is valid. |
| 43 | + |
| 44 | +## Open-RPC rules |
| 45 | + |
| 46 | +To expand the linting checks for an Open-RPC description, start by enabling some of the built-in rules. |
| 47 | +The currently supported rules are: |
| 48 | + |
| 49 | +- `no-unresolved-refs`: Every `$ref` must exist. |
| 50 | +- `no-unused-components`: All components must be used. |
| 51 | +- `spec-no-duplicated-method-params`: The list of parameters must not include duplicated parameters. |
| 52 | +- `spec-no-required-params-after-optional`: Required parameters must be positioned before optional parameters. |
| 53 | +- `info-contact`: Contact section is defined under `info`. |
| 54 | +- `info-license`: License section is defined under `info`. |
| 55 | + |
| 56 | +We expect the list to expand over time, so keep checking back - and let us know if you have any requests by [opening an issue on the GitHub repo](https://github.com/Redocly/redocly-cli/issues). |
| 57 | + |
| 58 | +To use a rule in your own linting setup, add the rule name to the `rules` configuration section, and declare the severity level (either `error`, `warn` or `off`). |
| 59 | +Here's an example of a rules block where a missing contact section causes a warning, and a tag without a description triggers an error: |
| 60 | + |
| 61 | +```yaml |
| 62 | +rules: |
| 63 | + info-contact: warn |
| 64 | + info-license: error |
| 65 | +``` |
| 66 | + |
| 67 | +Pick and mix the available rules until you have the setup that fits your situation. |
| 68 | + |
| 69 | +## Configurable rule example |
| 70 | + |
| 71 | +Redocly CLI also offers [configurable rules](../rules/configurable-rules.md) that allow you to set assertions about the API description being linted. This functionality works for Open-RPC too. |
| 72 | +The following example shows a configurable rule that displays a warning if the `title` field is not present in the `info` block: |
| 73 | + |
| 74 | +```yaml |
| 75 | +rules: |
| 76 | + rule/info-title: |
| 77 | + subject: |
| 78 | + type: Info |
| 79 | + property: title |
| 80 | + assertions: |
| 81 | + defined: true |
| 82 | + severity: warn |
| 83 | +``` |
| 84 | + |
| 85 | +With the extensive configurable rules options available, there are many opportunities to make sure that your Open-RPC spec conforms with expectations. We'd also love to see what you're building - it helps us know how things are going! |
| 86 | + |
| 87 | +{% admonition type="info" name="Custom plugins" %} |
| 88 | +To create your own plugins, see the [custom plugins documentation](../custom-plugins/index.md). |
| 89 | + |
| 90 | +{% /admonition %} |
0 commit comments